代码改变世界

unity3D中 material中tiling和offset属性解释

  糯米粥  阅读(5857)  评论(0编辑  收藏  举报

贴图有可能是多行多列的一些图案组成的。当我们需要一帧,一帧的播放时候。也就是帧序列动画,

我们就需要用到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     }
复制代码

 



复制代码
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
历史上的今天:
2013-12-08 反射原理
'
点击右上角即可分享
微信分享提示