简略的帧动画
本人也是個0基礎的開發人員,這里僅僅是作為一個學習的總結,假設哪里的理解錯了,歡迎糾正。
幀動畫的實現能夠用第三方的插件,類似2DToolkit,能夠非常easy的實現幀動畫,但有時還是不要太過于依賴第三方的插件。有時間還是得自己研究怎樣實現,不能一直穿著別人的內褲啊~
這個是本次項目的終于效果圖:
關于在Unity3d下怎樣用代碼生成面,這里雨松MOMO已經有相關的博文:
http://www.xuanyusong.com/archives/780
理解下一張Material的坐標系,這里用了網上找的一張圖片:
每一個Material都有自己的這個坐標系。
而假設須要把一張紋理貼到一個面上,則須要把貼圖上的UV坐標(UV坐標就是上面的坐標系)相應到面的每一個頂點上,如圖(又是網上的圖片):
比方有以下一張圖片:
圖片總寬高為:192x152,單張精靈的寬高為48x38;
假設我們僅僅想顯示左上角的1張圖片,代碼例如以下
using UnityEngine; using System.Collections;public class ViewImage : MonoBehaviour {public int widthCount , heightCount;//圖片的寬高比例//public float width = 48,height = 38;//素材貼圖//public Material material;//頂點數//private int verticesCount = 4;private Vector2 size;private Mesh mesh;public int frameIndex = 0;private MeshRenderer meshRenderer;// Use this for initializationvoid Start () {initFace();}/// <summary>/// 初始化一個面/// </summary>private void initFace(){//得到MeshFilter對象//MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>();if(meshFilter == null){//為null時,自己主動加入//meshFilter = gameObject.AddComponent<MeshFilter>();meshRenderer = gameObject.AddComponent<MeshRenderer>();meshRenderer.sharedMaterial = material;}//得到相應的網格對象//mesh = meshFilter.mesh;//三角形頂點的坐標數組//Vector3[] vertices = new Vector3[verticesCount];//得到三角形的數量//int trianglesCount = verticesCount - 2;//三角形頂點數組//int[] triangles = new int[verticesCount *3]; float tmpWidth = 1.0f;float tmpHeight = 1.0f ;vertices[0] = new Vector3(0,0,0);vertices[1] = new Vector3(0,tmpHeight,0);vertices[2] = new Vector3(tmpWidth,0,0);vertices[3] = new Vector3(tmpWidth,tmpHeight,0); mesh.vertices = vertices;/** * 假設以下的頂點連線看不明確,看這里triangles[0] = 0;triangles[1] = 1;triangles[2] = 2;triangles[3] = 1;triangles[4] = 3;triangles[5] = 2;*///起始三角形頂點//int start = 0;//結束三角形的頂點//int end = 2;for(int i = start; i <end; i++){for(int j = 0; j < 3; j++){if( i%2 ==0){triangles[3*i + j] = i +j;}else{triangles[3*i + j] = i + 2-j;}}}mesh.triangles = triangles; Vector3 localScale = new Vector3(width,height,1.0f);transform.localScale = localScale;size = new Vector2 (1.0f / widthCount , 1.0f / heightCount);setUVPosition(frameIndex,mesh);}//設置uv坐標private void setUVPosition(int index,Mesh mesh){//得到相對于圖片的行列坐標//int uIndex = index % widthCount; //列坐標//int vIndex = index / heightCount; //行坐標////左下角的坐標點;//Vector2 vertices0 = new Vector2 (uIndex * size.x, 1.0f - size.y - vIndex * size.y);//左上角坐標//Vector2 vertices1 = new Vector2(vertices0.x , vertices0.y + size.y);//右下角坐標//Vector2 vertices2 = new Vector2(vertices0.x + size.x , vertices0.y );//右上角坐標//Vector2 vertices3 = new Vector2(vertices0.x + size.x , vertices0.y + size.y);mesh.uv = new Vector2[]{vertices0 , vertices1 , vertices2 , vertices3};} }
代碼上面都有凝視,這里關于用圖片的素材球的Shader說明下。一開始我用Transparent/Diffuse,可是在執行時,切換uv坐標時控制臺打印了Shader wants normals, but the mesh ?doesn't have them 這個消息。Google下也不知道為什么,最后用Unlit/Transparent就沒有打印了。所以哪位大神知道原因告訴我下~~
繼續,如今已經能夠從一張大貼圖中顯示當中的一張圖片了,接下來整理成動畫就是把這些幀圖片整合在一起,在Update里頭切換就KO了,代碼例如以下:
//控制動畫幀數//public int[] aniArray; void Update(){if(Time.frameCount % 8 == 0){int len = aniArray.Length;int curIndex = aniArray[frameIndex];setUVPosition(curIndex,mesh);frameIndex ++;frameIndex %= len;}}
以下是項目的一張截圖,用紅線圈起來的部分是動畫幀的下標數組。
WidgetCount:大貼圖x方向上有多少張圖片。
HeightCount:大貼圖y方向上有多少張圖片。
Width:分隔的小貼圖的寬度。
Height:f分隔的小貼圖的高度。
FrameIndex:當前播放到第幾幀的下標。
Ani Array:幀動畫的下標集合數組。
O了。這篇事實上好早就在寫。但一直沒去完畢..今天光棍就順便完畢掉~
轉載于:https://www.cnblogs.com/clnchanpin/p/6888406.html
總結
- 上一篇: 【Prince2科普】Prince2的七
- 下一篇: 独立成分分析(Independent c