using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void Start() {
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
Color[] colors = new Color[vertices.Length];
int i = 0;
while (i < vertices.Length) {
colors[i] = Color.Lerp(Color.red, Color.green, vertices[i].y);
i++;
}
mesh.colors = colors;
}
}
以上是手册上的源码,代码肯定是没有问题的,但是运行后无效。细细一想,这可能与所选的材质和着色器有关,于是更换了一些材质和着色器,确实是有几个有效,其余的无效。
仓促的猜测下原因,留待日后认证:colors是顶点的颜色数组,但是这些颜色会被材质本身的颜色覆盖掉,所以会无效。如果选择 Sprites-Default 或者 Default-Particle 材质就是有效,可能这两个材质会保留顶点的颜色。着色器选择 Particles下面的 , Sprites/Default等也有效。