给模型偏移纹理
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 }