unity text颜色渐变

下面的组件做一个记录,可以使ui自己设置单个文字上下颜色,最终效果为,仿照艺术字

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

[AddComponentMenu("UI/Effect/UIColorGradient")]
public class UIColorGradient : BaseMeshEffect
{
    public static List<UIVertex> helpList = new List<UIVertex>();

    private bool isText;
    private bool isCheck;

    [SerializeField]
    private Color32 topColor = Color.yellow;
    [SerializeField]
    private Color32 bottomColor = Color.red;


  
    protected override void OnEnable()
    {
        base.OnEnable();
        if (this.gameObject.GetComponent<Text>() != null)
            isText = true;
        else
           isText = false;
    }


  


    private void ModifyMeshText(VertexHelper vertexHelper)
    {
        helpList.Clear();
        vertexHelper.GetUIVertexStream(helpList);
        int count = helpList.Count;
        if (count > 0)
        {
            for (int i = 0; i < helpList.Count;)
            {
                float bottomY = helpList[i].position.y;
                float topY = bottomY;
                float dis = 1f;
                for (int k = 1; k < 6; k++)
                {
                    float y = helpList[k + i].position.y;
                    if (y > topY)
                    {
                        topY = y;
                    }
                    else if (y < bottomY)
                    {
                        bottomY = y;
                    }
                }
                dis = topY - bottomY;
                for (int k = 0; k < 6; k++)
                {
                    UIVertex vertText = helpList[k + i];
                    vertText.color = Color32.Lerp(bottomColor, topColor, (vertText.position.y - bottomY) / dis);
                    helpList[k + i] = vertText;
                }
                i += 6;
            }
            vertexHelper.Clear();
            vertexHelper.AddUIVertexTriangleStream(helpList);
            helpList.Clear();
        }
    }

    public override void ModifyMesh(VertexHelper vertexHelper)
    {
        if (!IsActive())
        {
            return;
        }

        if(isText)
        {
            ModifyMeshText(vertexHelper);
            return;
        }

        helpList.Clear();
        vertexHelper.GetUIVertexStream(helpList);
        int count = helpList.Count;
        if (count > 0)
        {
            Vector3 pos = helpList[0].position;

            float bottomY = pos.y;
            float topY = pos.y;

            for (int i = 1; i < count; i++)
            {
                float y = helpList[i].position.y;
                if (y > topY)
                {
                    topY = y;
                }
                else if (y < bottomY)
                {
                    bottomY = y;
                }
            }
            float uiElementHeight = topY - bottomY;
          
            for (int i = 0; i < count; i++)
            {
                UIVertex uIVertex = helpList[i];
                Color32 color32 = Color32.Lerp(bottomColor, topColor, (uIVertex.position.y - bottomY) / uiElementHeight);
                uIVertex.color = color32;
                helpList[i] = uIVertex;
            }
            vertexHelper.Clear();
            vertexHelper.AddUIVertexTriangleStream(helpList);
        }
        helpList.Clear();
    }
}
 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值