unity3D中 material中tiling和offset屬性解釋


貼圖有可能是多行多列的一些圖案組成的。當我們需要一幀,一幀的播放時候。也就是幀序列動畫,

我們就需要用到tiling和offset兩個屬性,

默認圖片的左下角為坐標圓點即:(0,0)

tiling是圖片的大小,offset是偏移量

來看看一些例子:

 

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class animspite : MonoBehaviour
 5 {
 6 
 7 
 8     public int totolFrame;//總幀數,即多少幀
 9     public int fbs;//幀速度 即 1秒運行多少幀
10     public int rowNumber; //幾行
11     public int colNumber; //幾列
12     public bool isDes = false; //是否播放一次就銷毀對象
13     // Use this for initialization
14     void Start()
15     {
16         //判斷當前平台
17         #if UNITY_EDITOR
18                 Debug.Log("Unity Editor");
19         #endif
20 
21         #if UNITY_IPHONE
22                 Debug.Log("Iphone");
23         #endif
24 
25         #if UNITY_STANDALONE_OSX
26                 Debug.Log("Stand Alone OSX");
27         #endif
28 
29         #if UNITY_STANDALONE_WIN
30                 Debug.Log("Stand Alone Windows");
31         #endif
32     }
33 
34     // Update is called once per frame
35     void Update()
36     {
37         int index = (int)(Time.time * fbs);
38 
39         index = index % totolFrame;
40 
41         float sizeX = 1.0f / colNumber;
42         float sizeY = 1.0f / rowNumber;
43         Vector2 size = new Vector2(sizeX, sizeY);
44 
45         float uIndex = index % colNumber;
46         float vIndex = index / colNumber;
       //int vIndex = index % rowNumber;
47 48 float offsetX = uIndex * size.x; 49 float offsetY = (1.0f - size.y) - (vIndex * size.y); 50 //offsetY = 1.0f * vIndex / rowNumber; 51 52 Vector2 offset = new Vector2(offsetX, offsetY); 53 54 transform.renderer.material.mainTextureScale = size; 55 transform.renderer.material.mainTextureOffset = offset; 56 57 58 if (isDes) 59 { 60 if (Time.time > 1) 61 { 62 Destroy(this.gameObject); 63 } 64 } 65 } 66 }

 

 
         
 1  //--------------更直觀的分割線----------------------
 2         int currentFame = (int)(Time.time * fbs); //算出當前幀
      if (assing != 0) currentFame = assing;//當指定assing值時,即只播放指定幀的動畫
3 Vector2 size = new Vector2(); //算出當前幀,圖片的坐標 4 size.x = 1.0f / colNumber; 5 size.y = 1.0f / rowNumber; 6 7 //算出當前幀,圖片分別在x。y軸上的坐標 8 float xFrame = currentFame % colNumber; 9 float yFrmae = currentFame / colNumber; 10 11 Vector2 offset = new Vector2(); //算出當前幀,圖片的偏移量 12 offset.x = xFrame * size.x; 13 offset.y = 1.0f - size.y - (yFrmae * size.y); 14 15 //賦值 16 transform.renderer.material.mainTextureScale = size; 17 transform.renderer.material.mainTextureOffset = offset;
 
         

 

 1 /*參考自網絡*/
 2     public int rowCount = 4; //圖片的中幀圖片的行數
 3     public int colCount = 4; //圖片的中幀圖片的列數
 4     public int rowStart = 0; //開始播放的行下標
 5     public int colStart = 0; //開始播放的列下標
 6     public int totalCells = 4; //幀圖片的總數
 7     public int fps = 10;  //播放速度
 8     //Update
 9     void Update()
10     {
11         SetSpriteAnimation(colCount, rowCount, rowStart, colStart, totalCells, fps);
12     }
13 
14     //SetSpriteAnimation
15     void SetSpriteAnimation(int colCount, int rowCount, int rowStart, int colStart, int totalCells, int fps)
16     {
17         //因為Time.time的為游戲運行的時間,所以index=1,2,3,4...
18         int index = (int)(Time.time * fps);
19         index = index % totalCells;
20         float sizeX = 1.0f / colCount;
21         float sizeY = 1.0f / rowCount;
22         Vector2 size = new Vector2(sizeX, sizeY);
23         var uIndex = index % colCount;
24         var vIndex = index / colCount;
25         float offsetX = (uIndex + colStart) * size.x;
26         //V軸位於圖片的下方,因此V要翻轉下
27         float offsetY = (1.0f - size.y) - (vIndex + rowStart) * size.y; 
28         Vector2 offset = new Vector2(offsetX, offsetY);
29         renderer.material.SetTextureOffset("_MainTex", offset);
30         renderer.material.SetTextureScale("_MainTex", size);
31     }

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM