Unity3D之UV动画
1 using UnityEngine; 2 using System.Collections; 3 public enum OffsetDirection 4 { 5 none, 6 X, 7 Y, 8 XY, 9 negativeX, 10 negativeY 11 } 12 13 public class UVoffset : MonoBehaviour { 14 public Material mat_UVMaterial; 15 public float scrollSpeed = 1.0f; 16 public OffsetDirection dir = OffsetDirection.none; 17 private float offsetX = 0.0f; 18 private float offsetY = 0.0f; 19 20 void Start() 21 { 22 if (renderer.materials.Length == 1) //代表有一种材质 23 { 24 mat_UVMaterial = renderer.material; 25 } 26 else if(mat_UVMaterial==null) 27 { 28 Debug.LogError("存在多个材质球,请选择一个材质球进行UV变换"); 29 } 30 } 31 32 // Update is called once per frame 33 void Update () { 34 switch(dir) 35 { 36 case OffsetDirection.none: 37 offsetX = 0; 38 offsetY = 0; 39 break; 40 case OffsetDirection.X: 41 offsetX += scrollSpeed * Time.deltaTime; 42 offsetY = 0; 43 break; 44 case OffsetDirection.Y: 45 offsetX = 0; 46 offsetY += scrollSpeed * Time.deltaTime; 47 break; 48 case OffsetDirection.XY: 49 offsetX += scrollSpeed * Time.deltaTime; 50 offsetY += scrollSpeed * Time.deltaTime; 51 break; 52 case OffsetDirection.negativeX: 53 offsetX -= scrollSpeed * Time.deltaTime; 54 break; 55 case OffsetDirection.negativeY: 56 offsetY -= scrollSpeed * Time.deltaTime; 57 break; 58 } 59 mat_UVMaterial.SetTextureOffset("_MainTex", new Vector2(offsetX, offsetY)); 60 } 61 }