1 /* 2 * 通过改变Mesh组件的Color来改变颜色 3 * */ 4 using UnityEngine; 5 using System.Collections; 6 7 public class ColorChangerVertex : MonoBehaviour 8 { 9 10 Mesh mesh; 11 //定义一个颜色数组,Color数组中有4个数,RGBA 12 Color[] meshColors; 13 14 void Start() { 15 mesh = GetComponent<MeshFilter>().mesh; 16 meshColors = new Color[mesh.vertices.Length]; 17 } 18 19 // Update is called once per frame 20 void Update() { 21 //让RGB随关卡加载时间改变Time.timeSinceLevelLoad 22 for (int i=0; i<meshColors.Length; ++i) { 23 float offset = mesh.vertices[i].magnitude; 24 float r = Mathf.Abs(Mathf.Sin(Time.timeSinceLevelLoad + offset)); 25 float g = Mathf.Abs(Mathf.Sin(Time.timeSinceLevelLoad * 0.45f + offset)); 26 float b = Mathf.Abs(Mathf.Sin(Time.timeSinceLevelLoad * 1.2f + offset)); 27 Color newColor = new Color(r,g,b); 28 29 meshColors [i] = newColor; 30 } 31 mesh.colors = meshColors; 32 33 } 34 35 }
该脚本展示如何使用Unity3d通过修改Mesh组件的颜色属性实现物体颜色的动态变化。具体方法是利用Time.timeSinceLevelLoad计算颜色的RGB值,并将其应用到Mesh上。
1452

被折叠的 条评论
为什么被折叠?



