思路:
因为ModifyMesh方法里传进来的VertexHelper实例对象中 Text的顶点是有序的,只要计算出最高顶点和最低顶点间距离(插值计算时需要用)也就是渐变的高度,然后遍历所有顶点,根据顶部颜色、底部颜色、当前坐标距离底部的高度/总的渐变高度,作插值计算,更新顶点的颜色
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[AddComponentMenu("UI/Effect/VerticalGradient")]
[RequireComponent(typeof(UnityEngine.UI.Text))]
public class VerticalGradient : BaseMeshEffect
{
public Color32 topColor = new Color32(255, 255, 255, 255);
public Color32 bottomColor = new Color32(0, 0, 0, 255);
public override void ModifyMesh(VertexHelper vh)
{
if (!IsActive() || vh.currentVertCount < 4)
{
return;
}
//获取所有顶点 这里vertexList里存的是 4个顶点组成的2个三角面的 6个顶点也就是单个文字4