unity3D中 material中tiling和offset属性解释
生活随笔
收集整理的这篇文章主要介绍了
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 }
总结
以上是生活随笔为你收集整理的unity3D中 material中tiling和offset属性解释的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 简单几招模拟网络超时情况
- 下一篇: C++中UINT32和INT32以及in