您的位置:首页 > 移动开发 > Unity3D

Unity3D 给模型偏移纹理

2015-06-24 14:38 162 查看
给模型偏移纹理

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Renderer))]
public class ModelTextureAnimation : MonoBehaviour {

//材质索引
public int MaterialIndex;

//材质主纹理名字
public string TextureName = "_MainTex";

//行数
public int Column = 1;

//列数
public int Row = 1;

//每帧间隔
public float Inverval = 0.333f;

//当前帧索引
public int Index = 0;

private Renderer mRenderer;
private Material mMaterial;

// Use this for initialization
void Start () {
mRenderer = renderer;
mMaterial = mRenderer.sharedMaterials[MaterialIndex];

mMaterial.SetTextureScale(TextureName, new Vector2(1.0f / Column, 1.0f / Row));

StartCoroutine(Animate());
}

IEnumerator Animate()
{
SetTextureOffset();
while(true)
{
yield return new WaitForSeconds(Inverval);

if (true == mRenderer.active)
{
Index++;
Index %= (Column * Row);
SetTextureOffset();
}
}
}

//根据帧数算出Offset
void SetTextureOffset()
{
float x = (1.0f / Column) * (Index % Column);
float y = (1.0f / Row) * (Index / Column);
mMaterial.SetTextureOffset(TextureName, new Vector2(x,y));
}

// Update is called once per frame
#if UNITY_EDITOR
void Update () {
mMaterial.SetTextureScale(TextureName, new Vector2(1.0f / Column, 1.0f / Row));
}
#endif
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: