unity3d Text漸變色

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TextChange : BaseMeshEffect
{
    public Color32 TopColor = Color.white;
    public Color32 BottomColor = Color.black;

    public override void ModifyMesh(VertexHelper vh)
    {
        if (!IsActive())
        {
            return;
        }
        int count = vh.currentVertCount;
        if (count == 0) return;
        List<UIVertex> vers = new List<UIVertex>();
        for(int i = 0; i < count; i ++)
        {
            UIVertex ver = new UIVertex();
            vh.PopulateUIVertex(ref ver, i);
            vers.Add(ver);
        }
        float topY = vers[0].position.y;
        float bottomY = vers[0].position.y;
        for(int i = 1; i < count; i++)
        {
            float y = vers[i].position.y;
            if(y > topY)
            {
                topY = y;
            }else if(y < bottomY)
            {
                bottomY = y;
            }
        }
        float height = topY - bottomY;

        for(int i = 1; i < count; i++)
        {
            UIVertex v = vers[i];
            Color32 c = Color32.Lerp(BottomColor, TopColor, (v.position.y - bottomY)/height);
            v.color = c;
            vh.SetUIVertex(v, i);
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章