Unity2017 UGUI开发 之 字体渐变

Unity2017 + UGUI开发游戏的时候,我们需要实现一些功能。

在NGUI下,我们很容易就可以实现,因为在NGUI里有提供大量工具。在UGUI里没有提供这些工具怎么办?

没有就自己写呗!当然如果网上有现成的代码,我们拿来就可以用!

下面给大家分享一段 UGUI字体渐变的源码

using System.Collections.Generic;


public enum Type
{
    Horizontal,
    Vertical
}


public enum Blend
{
    Override,
    Add,
    Multiply
}


namespace UnityEngine.UI
{
    [AddComponentMenu("UI/Effects/UGUI_Gradient")]
    public class Gradient : BaseMeshEffect
    {
        [SerializeField]
        Type _gradientType;


        [SerializeField]
        Blend _blendMode = Blend.Multiply;


        [SerializeField]
        [Range(-1, 1)]
        float _offset = 0f;


        [SerializeField]
        UnityEngine.Gradient _effectGradient = new UnityEngine.Gradient()
        { colorKeys = new GradientColorKey[] { new GradientColorKey(Color.black, 0), new GradientColorKey(Color.white, 1) } };


        #region Properties
        public Blend BlendMode
        {
            get { return _blendMode; }
            set { _blendMode = value; }
        }


        public UnityEngine.Gradient EffectGradient
        {
            get { return _effectGradient; }
            set { _effectGradient = value; }
        }


        public Type GradientType
        {
            get { return _gradientType; }
            set { _gradientType = value; }
        }


        public float Offset
        {
            get { return _offset; }
            set { _offset = value; }
        }
        #endregion


        public override void ModifyMesh(VertexHelper helper)
        {
            if (!IsActive() || helper.currentVertCount == 0)
                return;


            List<UIVertex> _vertexList = new List<UIVertex>();


            helper.GetUIVertexStream(_vertexList);


            int nCount = _vertexList.Count;
            switch (GradientType)
            {
                case Type.Horizontal:
                    {
                        float left = _vertexList[0].position.x;
                        float right = _vertexList[0].position.x;
                        float x = 0f;
                        for (int i = nCount - 1; i >= 1; --i)
                        {
                            x = _vertexList[i].position.x;


                            if (x > right) right = x;
                            else if (x < left) left = x;
                        }
                        float width = 1f / (right - left);
                        UIVertex vertex = new UIVertex();


                        for (int i = 0; i < helper.currentVertCount; i++)
                        {
                            helper.PopulateUIVertex(ref vertex, i);


                            vertex.color = BlendColor(vertex.color, EffectGradient.Evaluate((vertex.position.x - left) * width - Offset));


                            helper.SetUIVertex(vertex, i);
                        }
                    }
                    break;


                case Type.Vertical:
                    {
                        float bottom = _vertexList[0].position.y;
                        float top = _vertexList[0].position.y;
                        float y = 0f;
                        for (int i = nCount - 1; i >= 1; --i)
                        {
                            y = _vertexList[i].position.y;


                            if (y > top) top = y;
                            else if (y < bottom) bottom = y;
                        }
                        float height = 1f / (top - bottom);
                        UIVertex vertex = new UIVertex();


                        for (int i = 0; i < helper.currentVertCount; i++)
                        {
                            helper.PopulateUIVertex(ref vertex, i);


                            vertex.color = BlendColor(vertex.color, EffectGradient.Evaluate((vertex.position.y - bottom) * height - Offset));


                            helper.SetUIVertex(vertex, i);
                        }
                    }
                    break;
            }
        }


        Color BlendColor(Color colorA, Color colorB)
        {
            switch (BlendMode)
            {
                default: return colorB;
                case Blend.Add: return colorA + colorB;
                case Blend.Multiply: return colorA * colorB;
            }
        }
    }
}

这个可以水平,垂直渐变    颜色可以叠加  看代码就知道了。

如果你觉得你的方法比这个更好,欢迎留言讨论。

感兴趣的朋友可以加群一起讨论,扫二维码加群

王银太一,王银太二 

微信 QQ:1160364001

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王 银

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值