【Model&Animation】
1、FBX文件是一个完整的模型,通常内含Mesh,Material,Texture,Animation,即内含构成一个完成GameObject所需要的一切组件。可以通过以下代码来引用。
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 }
2、通过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 }
3、可以使用方法animation.animation["clip名称"].length来获取播放的时间。
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 }
4、使用OpenGL。必须在OnPostRender()中才能使用。
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 }
5、使用GL绘制鼠标轨迹实例。
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 }
6、使用GL绘制方形。
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 }
7、使用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 }
8、MeshFilter指定使用哪个Mesh,MeshRender指定使用哪个Material以及渲染属性。Mesh包含VertexMesh和NormalMesh。