unity3D中 material中tiling和offset属性解释

unity3D中 material中tiling和offset属性解释,第1张

unity3D中 material中tiling和offset属性解释

贴图有可能是多行多列的一些图案组成的。


当我们需要一帧,一帧的播放时候。


也就是帧序列动画,

我们就需要用到tiling和offset两个属性,

默认图片的左下角为坐标圆点即:(0,0)

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

来看看一些例子:

 using UnityEngine;
using System.Collections; public class animspite : MonoBehaviour
{ public int totolFrame;//总帧数,即多少帧
public int fbs;//帧速度 即 1秒运行多少帧
public int rowNumber; //几行
public int colNumber; //几列
public bool isDes = false; //是否播放一次就销毁对象
// Use this for initialization
void Start()
{
//判断当前平台
#if UNITY_EDITOR
Debug.Log("Unity Editor");
#endif #if UNITY_IPHONE
Debug.Log("Iphone");
#endif #if UNITY_STANDALONE_OSX
Debug.Log("Stand Alone OSX");
#endif #if UNITY_STANDALONE_WIN
Debug.Log("Stand Alone Windows");
#endif
} // Update is called once per frame
void Update()
{
int index = (int)(Time.time * fbs); index = index % totolFrame; float sizeX = 1.0f / colNumber;
float sizeY = 1.0f / rowNumber;
Vector2 size = new Vector2(sizeX, sizeY); float uIndex = index % colNumber;
float vIndex = index / colNumber;
       //int vIndex = index % rowNumber; float offsetX = uIndex * size.x;
float offsetY = (1.0f - size.y) - (vIndex * size.y);
//offsetY = 1.0f * vIndex / rowNumber; Vector2 offset = new Vector2(offsetX, offsetY); transform.renderer.material.mainTextureScale = size;
transform.renderer.material.mainTextureOffset = offset; if (isDes)
{
if (Time.time > )
{
Destroy(this.gameObject);
}
}
}
}


  //--------------更直观的分割线----------------------
int currentFame = (int)(Time.time * fbs); //算出当前帧
      if (assing != 0) currentFame = assing;//当指定assing值时,即只播放指定帧的动画
Vector2 size = new Vector2(); //算出当前帧,图片的坐标
size.x = 1.0f / colNumber;
size.y = 1.0f / rowNumber; //算出当前帧,图片分别在x。


y轴上的坐标
float xFrame = currentFame % colNumber;
float yFrmae = currentFame / colNumber; Vector2 offset = new Vector2(); //算出当前帧,图片的偏移量
offset.x = xFrame * size.x;
offset.y = 1.0f - size.y - (yFrmae * size.y); //赋值
transform.renderer.material.mainTextureScale = size;
transform.renderer.material.mainTextureOffset = offset;



 /*参考自网络*/
public int rowCount = ; //图片的中帧图片的行数
public int colCount = ; //图片的中帧图片的列数
public int rowStart = ; //开始播放的行下标
public int colStart = ; //开始播放的列下标
public int totalCells = ; //帧图片的总数
public int fps = ; //播放速度
//Update
void Update()
{
SetSpriteAnimation(colCount, rowCount, rowStart, colStart, totalCells, fps);
} //SetSpriteAnimation
void SetSpriteAnimation(int colCount, int rowCount, int rowStart, int colStart, int totalCells, int fps)
{
//因为Time.time的为游戏运行的时间,所以index=1,2,3,4...
int index = (int)(Time.time * fps);
index = index % totalCells;
float sizeX = 1.0f / colCount;
float sizeY = 1.0f / rowCount;
Vector2 size = new Vector2(sizeX, sizeY);
var uIndex = index % colCount;
var vIndex = index / colCount;
float offsetX = (uIndex + colStart) * size.x;
//V轴位于图片的下方,因此V要翻转下
float offsetY = (1.0f - size.y) - (vIndex + rowStart) * size.y;
Vector2 offset = new Vector2(offsetX, offsetY);
renderer.material.SetTextureOffset("_MainTex", offset);
renderer.material.SetTextureScale("_MainTex", size);
}
					
										


					

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/586247.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-12
下一篇 2022-04-12

发表评论

登录后才能评论

评论列表(0条)

保存