using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; [AddComponentMenu("UI/Effects/Gradient")] public class Gradient : BaseMeshEffect { public Color32 topColor = Color.white; public Color32 bottomColor = Color.black; public override void ModifyMesh(VertexHelper vh) { if (!IsActive()) return; List<UIVertex> list = new List<UIVertex>(); vh.GetUIVertexStream(list); int listCount = list.Count; float bY = list[0].position.y; float tY = list[0].position.y; for (int i = 0; i < listCount; i++) { if (list[i].position.y > tY) { tY = list[i].position.y; } else if (list[i].position.y < bY) { bY = list[i].position.y; } } float h = tY - bY; UIVertex v = new UIVertex(); for (int j = 0;j < vh.currentVertCount; j++) { vh.PopulateUIVertex(ref v, j); v.color = Color32.Lerp(bottomColor, topColor, (v.position.y - bY) / h); vh.SetUIVertex(v,j); } } }
GetUIVertexStream()是获取到网格三角形中的所有顶点,因为三角形会有相交的,所以这里获取的顶点是有重复的。