【Unity】Unity中使一个文本Text有多种颜色,上面和下面颜色不同且渐变过渡

下面方法可以达到给文本添加不同颜色,可以上下左右颜色各不一样,且渐变过渡,自由配置
达到的效果是这样的
在这里插入图片描述
其配置是这样的,在对应的文本组件下面加上这个脚本
在这里插入图片描述

直接上代码 控制文本颜色的

/*
uGui-Effect-Tool
Copyright (c) 2016 WestHillApps (Hironari Nishioka)
This software is released under the MIT License.
http://opensource.org/licenses/mit-license.php
*/
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;

namespace UiEffect
{
    [AddComponentMenu("UI/Effects/Gradient Color"), RequireComponent(typeof(Graphic))]
    public class GradientColor : BaseMeshEffect
    {
        private const int ONE_TEXT_VERTEX = 6;

        [SerializeField]
        private Color m_colorTop = Color.white;
        [SerializeField]
        private Color m_colorBottom = Color.white;
        [SerializeField]
        private Color m_colorLeft = Color.white;
        [SerializeField]
        private Color m_colorRight = Color.white;
        [SerializeField, Range(-1f, 1f)]
        private float m_gradientOffsetVertical = 0f;
        [SerializeField, Range(-1f, 1f)]
        private float m_gradientOffsetHorizontal = 0f;
        [SerializeField]
        private bool m_splitTextGradient = false;

        public Color colorTop { get { return m_colorTop; } set { if (m_colorTop != value) { m_colorTop = value; Refresh(); } } }
        public Color colorBottom { get { return m_colorBottom; } set { if (m_colorBottom != value) { m_colorBottom = value; Refresh(); } } }
        public Color colorLeft { get { return m_colorLeft; } set { if (m_colorLeft != value) { m_colorLeft = value; Refresh(); } } }
        public Color colorRight { get { return m_colorRight; } set { if (m_colorRight != value) { m_colorRight = value; Refresh(); } } }
        public float gradientOffsetVertical { get { return m_gradientOffsetVertical; } set { if (m_gradientOffsetVertical != value) { m_gradientOffsetVertical = Mathf.Clamp(value, -1f, 1f); Refresh(); } } }
        public float gradientOffsetHorizontal { get { return m_gradientOffsetHorizontal; } set { if (m_gradientOffsetHorizontal != value) { m_gradientOffsetHorizontal = Mathf.Clamp(value, -1f, 1f); Refresh(); } } }
        public bool splitTextGradient { get { return m_splitTextGradient; } set { if (m_splitTextGradient != value) { m_splitTextGradient = value; Refresh(); } } }

        public override void ModifyMesh(VertexHelper vh)
        {
            if (IsActive() == false)
            {
                return;
            }

            List<UIVertex> vList = UiEffectListPool<UIVertex>.Get();

            vh.GetUIVertexStream(vList);

            ModifyVertices(vList);

            vh.Clear();
            vh.AddUIVertexTriangleStream(vList);

            UiEffectListPool<UIVertex>.Release(vList);
        }

        private void ModifyVertices(List<UIVertex> vList)
        {
            if (IsActive() == false || vList == null || vList.Count == 0)
            {
                return;
            }

            float minX = 0f, minY = 0f, maxX = 0f, maxY = 0f, width = 0f, height = 0;

            UIVertex newVertex;
            for (int i = 0; i < vList.Count; i++)
            {
                if (i == 0 || (m_splitTextGradient && i % ONE_TEXT_VERTEX == 0))
                {
                    minX = vList[i].position.x;
                    minY = vList[i].position.y;
                    maxX = vList[i].position.x;
                    maxY = vList[i].position.y;

                    int vertNum = m_splitTextGradient ? i + ONE_TEXT_VERTEX : vList.Count;

                    for (int k = i; k < vertNum; k++)
                    {
                        if (k >= vList.Count)
                        {
                            break;
                        }
                        UIVertex vertex = vList[k];
                        minX = Mathf.Min(minX, vertex.position.x);
                        minY = Mathf.Min(minY, vertex.position.y);
                        maxX = Mathf.Max(maxX, vertex.position.x);
                        maxY = Mathf.Max(maxY, vertex.position.y);
                    }

                    width = maxX - minX;
                    height = maxY - minY;
                }

                newVertex = vList[i];

                Color colorOriginal = newVertex.color;
                Color colorVertical = Color.Lerp(m_colorBottom, m_colorTop, (height > 0 ? (newVertex.position.y - minY) / height : 0) + m_gradientOffsetVertical);
                Color colorHorizontal = Color.Lerp(m_colorLeft, m_colorRight, (width > 0 ? (newVertex.position.x - minX) / width : 0) + m_gradientOffsetHorizontal);

                newVertex.color = colorOriginal * colorVertical * colorHorizontal;

                vList[i] = newVertex;
            }
        }

        private void Refresh()
        {
            if (graphic != null)
            {
                graphic.SetVerticesDirty();
            }
        }
    }
}

上面代码依赖 UiEffectListPool,该脚本在这里

/*
Original Code is here.
https://bitbucket.org/Unity-Technologies/ui/src/f0b47d48183738c2eb00e90ede60c38448f359b5/UnityEngine.UI/UI/Core/Utility/ListPool.cs?at=5.4&fileviewer=file-view-default
*/
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

namespace UiEffect
{
    public static class UiEffectListPool<T>
    {
        // Object pool to avoid allocations.
        private static readonly UiEffectObjectPool<List<T>> s_ListPool = new UiEffectObjectPool<List<T>>(null, l => l.Clear());

        public static List<T> Get()
        {
            return s_ListPool.Get();
        }

        public static void Release(List<T> toRelease)
        {
            s_ListPool.Release(toRelease);
        }
    }

    public class UiEffectObjectPool<T> where T : new()
    {
        private readonly Stack<T> m_Stack = new Stack<T>();
        private readonly UnityAction<T> m_ActionOnGet;
        private readonly UnityAction<T> m_ActionOnRelease;

        public int countAll { get; private set; }
        public int countActive { get { return countAll - countInactive; } }
        public int countInactive { get { return m_Stack.Count; } }

        public UiEffectObjectPool(UnityAction<T> actionOnGet, UnityAction<T> actionOnRelease)
        {
            m_ActionOnGet = actionOnGet;
            m_ActionOnRelease = actionOnRelease;
        }

        public T Get()
        {
            T element;
            if (m_Stack.Count == 0)
            {
                element = new T();
                countAll++;
            }
            else
            {
                element = m_Stack.Pop();
            }
            if (m_ActionOnGet != null)
                m_ActionOnGet(element);
            return element;
        }

        public void Release(T element)
        {
            if (m_Stack.Count > 0 && ReferenceEquals(m_Stack.Peek(), element))
                Debug.LogError("Internal error. Trying to destroy object that is already released to pool.");
            if (m_ActionOnRelease != null)
                m_ActionOnRelease(element);
            m_Stack.Push(element);
        }
    }
}

Over~
创作不易,看到这了,记得点赞哦~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值