【Model&Animation】ide
一、FBX文件是一個完整的模型,一般內含Mesh,Material,Texture,Animation,即內含構成一個完成GameObject所須要的一切組件。能夠經過如下代碼來引用。oop
1 //動畫名稱 2 public const string ANIM_NAME0="idle"; 3 public const string ANIM_NAME1="run"; 4 public const string ANIM_NAME2="walk"; 5 public const string ANIM_NAME3="jump_pose"; 6 //模型對象 7 private GameObject obj = null; 8 9 void Start () 10 { 11 //獲取模型動畫 12 obj = GameObject.Find("Constructor"); 13 //設置動畫播放類型爲循環 14 obj.animation.wrapMode = WrapMode.Loop; 15 } 16 17 void Update () 18 { 19 //按鍵後播放不一樣動畫 20 if (Input.GetKeyDown (KeyCode.A)) 21 { 22 obj.animation.Play(ANIM_NAME0); 23 } 24 if (Input.GetKeyDown (KeyCode.B)) 25 { 26 obj.animation.Play(ANIM_NAME1); 27 } 28 if (Input.GetKeyDown (KeyCode.C)) 29 { 30 obj.animation.Play(ANIM_NAME2); 31 } 32 if (Input.GetKeyDown (KeyCode.D)) 33 { 34 obj.animation.Play(ANIM_NAME3); 35 } 36 }
二、經過Animation組件的AddClip()、PlayQueued()能夠實現動畫剪輯與合併。動畫
1 //模型對象 2 private GameObject obj = null; 3 4 void Start () 5 { 6 //獲得模型動畫 7 obj = GameObject.Find("man"); 8 9 } 10 11 void OnGUI() 12 { 13 if(GUILayout.Button("播放完整動畫")) 14 { 15 //這裏播放默認動畫,默認動畫及完整150幀動畫 16 obj.animation.Play(); 17 } 18 19 if(GUILayout.Button("切割動畫0-50幀")) 20 { 21 //切割動畫,播放第0幀到第50幀 22 PlayCuttingAnimation(obj,0,50); 23 } 24 25 if(GUILayout.Button("合併動畫0-50幀與100-150幀")) 26 { 27 //合併動畫,將第0幀到第50幀與第100幀到第150幀兩組動畫合併在一塊兒播放 28 PlayCombinedAnimation(obj,0,50,100,150); 29 } 30 } 31 32 public void PlayCuttingAnimation(GameObject manObject,int startFrame,int endFrame) 33 { 34 35 AnimationClip clip = manObject.animation.clip; 36 //添加一個剪輯,設置起始幀與結束幀 37 manObject.animation.AddClip(clip, "cutClip", startFrame, endFrame); 38 manObject.animation.Play("cutClip"); 39 } 40 41 public void PlayCombinedAnimation(GameObject manObject,int startFrame0,int EndFrame0,int startFrame1,int EndFrame1) 42 { 43 AnimationClip clip = manObject.animation.clip; 44 //添加兩個剪輯,設置起始幀與結束幀 45 manObject.animation.AddClip(clip,"startClip",startFrame0,EndFrame0,false); 46 manObject.animation.AddClip(clip,"endClip",startFrame1,EndFrame1,false); 47 //以隊列的形式播放這兩個剪輯,保證第一個動畫播放完畢在播放第二個動畫 48 manObject.animation.PlayQueued("startClip", QueueMode.PlayNow); 49 manObject.animation.PlayQueued("endClip", QueueMode.CompleteOthers); 50 }
三、可使用方法animation.animation["clip名稱"].length來獲取播放的時間。spa
1 void Start () 2 { 3 //獲得模型動畫 4 obj = GameObject.Find("man"); 5 //獲得動畫播放長度 6 animLegth = obj.animation.animation[ANIM_NAME].length; 7 } 8 9 void OnGUI() 10 { 11 //顯示信息 12 string show = "當前動畫長度:"+hSliderValue.ToString() +"(s)"+ " / " + animLegth.ToString()+"(s)"; 13 GUILayout.Label(show); 14 //計算拖動條拖動數值 15 hSliderValue = GUILayout.HorizontalSlider(hSliderValue, 0.0F, 5.0F,GUILayout.Width(200)); 16 //繪製動畫幀 17 PlaySilderAnimation(obj,hSliderValue); 18 19 } 20 21 public void PlaySilderAnimation(GameObject manObject,float times) 22 { 23 24 //播放動畫 25 if(!manObject.animation.IsPlaying(ANIM_NAME)) 26 { 27 manObject.animation.Play(ANIM_NAME); 28 } 29 //設置動畫時間 30 manObject.animation.animation[ANIM_NAME].time = times; 31 }
四、使用OpenGL。必須在OnPostRender()中才能使用。3d
1 //繪製線段在職 2 public Material material; 3 4 //此繪製方法由系統調用 5 void OnPostRender() 6 { 7 if (!material) 8 { 9 Debug.LogError("請給材質資源賦值"); 10 return; 11 } 12 //設置該材質通道,0爲默認。 13 material.SetPass(0); 14 //設置繪製2D圖像 15 GL.LoadOrtho(); 16 //標誌GL開始繪製,繪製類型爲線段 17 GL.Begin(GL.LINES); 18 //繪製線段0 19 DrawLine(0,0,200,100); 20 //繪製線段1 21 DrawLine(0,50,200,150); 22 //繪製線段2 23 DrawLine(0,100,200,200); 24 //結束繪製 25 GL.End(); 26 } 27 28 void DrawLine(float x1,float y1,float x2,float y2) 29 { 30 //繪製線段,須要座標點除以屏幕寬或高 31 GL.Vertex(new Vector3(x1/Screen.width, y1/Screen.height, 0)); 32 GL.Vertex(new Vector3(x2/Screen.width, y2/Screen.height, 0)); 33 }
五、使用GL繪製鼠標軌跡實例。code
1 private List<Vector3> lineInfo; 2 3 void Start() 4 { 5 //初始化鼠標線段鏈表 6 lineInfo = new List<Vector3>(); 7 } 8 9 void Update() 10 { 11 //將每次鼠標改變的位置儲存進鏈表 12 lineInfo.Add(Input.mousePosition); 13 } 14 void OnGUI() 15 { 16 GUILayout.Label("當前鼠標x軸位置:"+Input.mousePosition.x); 17 GUILayout.Label("當前鼠標y軸位置:"+Input.mousePosition.y); 18 } 19 20 //此繪製方法由系統調用 21 void OnPostRender() { 22 if (!material) 23 { 24 Debug.LogError("請給材質資源賦值"); 25 return; 26 } 27 //設置該材質通道,0爲默認。 28 material.SetPass(0); 29 //設置繪製2D圖像 30 GL.LoadOrtho(); 31 //標誌GL開始繪製,繪製類型爲線段 32 GL.Begin(GL.LINES); 33 //獲得鼠標點信息總數量 34 int size = lineInfo.Count; 35 //遍歷鼠標點的鏈表 36 for(int i =0; i< size-1; i++) 37 { 38 Vector3 start = lineInfo[i]; 39 Vector3 end = lineInfo[i+1]; 40 //繪製線 41 DrawLine(start.x,start.y,end.x,end.y); 42 } 43 44 //結束繪製 45 GL.End(); 46 } 47 48 void DrawLine(float x1,float y1,float x2,float y2) 49 { 50 //繪製線段,須要座標點除以屏幕寬或高 51 GL.Vertex(new Vector3(x1/Screen.width, y1/Screen.height, 0)); 52 GL.Vertex(new Vector3(x2/Screen.width, y2/Screen.height, 0)); 53 }
六、使用GL繪製方形。orm
1 //可用材質 2 public Material mat0; 3 public Material mat1; 4 public Material mat3; 5 6 void OnPostRender() { 7 8 //繪製正四邊方形 9 DrawRect(100,100,100,100,mat0); 10 DrawRect(250,100,100,100,mat1); 11 //繪製無規則四邊形 12 DrawQuads(15,5,10,115,95,110,90,10,mat3); 13 } 14 15 /** 16 繪製正四邊形 17 float x :X軸起始座標 18 float y :Y軸起始座標 19 float width :正四邊形的寬 20 float height :正四邊形的高 21 */ 22 void DrawRect(float x,float y,float width,float height,Material mat) 23 { 24 GL.PushMatrix(); 25 mat.SetPass(0); 26 GL.LoadOrtho(); 27 //繪製類型爲四邊形 28 GL.Begin(GL.QUADS); 29 30 GL.Vertex3(x/Screen.width, y/Screen.height, 0); 31 GL.Vertex3(x/Screen.width, (y + height)/Screen.height, 0); 32 GL.Vertex3((x+ width)/Screen.width, (y + height)/Screen.height, 0); 33 GL.Vertex3((x+ width)/Screen.width,y/Screen.height, 0); 34 35 GL.End(); 36 GL.PopMatrix(); 37 } 38 39 /** 40 繪製無規則四邊形 41 float x1 :起始點1,X1座標 42 float y1 :起始點1,Y1座標 43 float x2 :起始點2,X2座標 44 float y2 :起始點2,X2座標 45 float x3 :起始點3,X3座標 46 float y3 :起始點3,X3座標 47 float x4 :起始點4,X4座標 48 float y4 :起始點4,X4座標 49 */ 50 51 void DrawQuads(float x1,float y1,float x2,float y2,float x3,float y3,float x4, float y4,Material mat) 52 { 53 GL.PushMatrix(); 54 mat.SetPass(0); 55 GL.LoadOrtho(); 56 //繪製類型爲四邊形 57 GL.Begin(GL.QUADS); 58 59 GL.Vertex3(x1/Screen.width, y1/Screen.height, 0); 60 GL.Vertex3(x2/Screen.width, y2/Screen.height, 0); 61 GL.Vertex3(x3/Screen.width, y3/Screen.height, 0); 62 GL.Vertex3(x4/Screen.width, y4/Screen.height, 0); 63 64 GL.End(); 65 GL.PopMatrix(); 66 }
七、使用LineRender。對象
1 //線段對象 2 private GameObject LineRenderGameObject; 3 4 //線段渲染器 5 private LineRenderer lineRenderer; 6 7 //設置線段的頂點數,4個點肯定3條線段 8 private int lineLength = 4; 9 10 //記錄4個點,鏈接一條線段 11 private Vector3 v0 = new Vector3(1.0f,0.0f,0.0f); 12 private Vector3 v1 = new Vector3(2.0f,0.0f,0.0f); 13 private Vector3 v2 = new Vector3(3.0f,0.0f,0.0f); 14 private Vector3 v3 = new Vector3(4.0f,0.0f,0.0f); 15 16 void Start() 17 { 18 //得到線段遊戲對象 19 LineRenderGameObject = GameObject.Find ("ObjLine"); 20 //得到線渲染器組件 21 lineRenderer = (LineRenderer)LineRenderGameObject.GetComponent ("LineRenderer"); 22 //設置線的頂點數 23 lineRenderer.SetVertexCount(lineLength); 24 //設置線的寬度 25 lineRenderer.SetWidth(0.1f,0.1f); 26 } 27 28 29 void Update() { 30 31 //使用這4個頂點渲染3條線段 32 lineRenderer.SetPosition (0, v0); 33 lineRenderer.SetPosition (1, v1); 34 lineRenderer.SetPosition (2, v2); 35 lineRenderer.SetPosition (3, v3); 36 37 }
八、MeshFilter指定使用哪一個Mesh,MeshRender指定使用哪一個Material以及渲染屬性。Mesh包含VertexMesh和NormalMesh。blog