Unity Text字体颜色渐变

时间:2021-08-24
本文章向大家介绍Unity Text字体颜色渐变,主要包括Unity Text字体颜色渐变使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.把脚本挂在text上调节颜色即可,废话不多说,直接上

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// ************字体颜色渐变****************************挂在Text上调节颜色即可********************************
/// </summary>
[AddComponentMenu("UI/Effects/GradientText")]
public class GradientText : BaseMeshEffect
{
    [SerializeField]
    private Color32 topColor = Color.white;

    [SerializeField]
    private Color32 bottomColor = Color.black;

    public override void ModifyMesh(VertexHelper vh)
    {
        if (!IsActive() || vh.currentVertCount == 0)
            return;
        List<UIVertex> vertices = new List<UIVertex>();
        vh.GetUIVertexStream(vertices);
        float bottomY = vertices[0].position.y;
        float topY = vertices[0].position.y;
        for (int i = 1; i < vertices.Count; i++)
        {
            if (vertices[i].position.y > topY)
            {
                topY = vertices[i].position.y;
            }
            else if (vertices[i].position.y < bottomY)
            {
                bottomY = vertices[i].position.y;
            }
        }
        float uiElementHeight = topY - bottomY;
        UIVertex v = new UIVertex();
        for (int i = 0; i < vh.currentVertCount; i++)
        {
            vh.PopulateUIVertex(ref v, i);
            v.color = Color32.Lerp(bottomColor, topColor, (v.position.y - bottomY) / uiElementHeight);
            vh.SetUIVertex(v, i);
        }
    }
}

喜欢的话,点个赞吧,Thank You

原文地址:https://www.cnblogs.com/qq2351194611/p/15180156.html