网格的顶点颜色。
如果顶点颜色不可用,将返回一个空数组。
// Sets the vertex color to be red at the y=0 and green at y=1. // (Note that most built-in Shaders don't display vertex colors. Use one that does, such as a Particle Shader, to see vertex colors)
using UnityEngine;
public class ExampleClass : MonoBehaviour { void Start() { Mesh mesh = GetComponent<MeshFilter>().mesh; Vector3[] vertices = mesh.vertices;
// create new colors array where the colors will be created. Color[] colors = new Color[vertices.Length];
for (int i = 0; i < vertices.Length; i++) colors[i] = Color.Lerp(Color.red, Color.green, vertices[i].y);
// assign the array of colors to the Mesh. mesh.colors = colors; } }
为提高性能,请考虑使用 colors32。这将避免颜色中的字节到浮点数转换,并减少临时内存使用。