阅读背景:

UGUI实现文字/图片颜色渐变

来源:互联网 

/*
最后修正时光:
主题:
功效:
著作:
*/
using FZY;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using System;

[RequireComponent(typeof(Graphic))]
public class NewBehaviourScript : BaseMeshEffect
{
    [SerializeField]
    private Color _topColor = Color.white;//顶部色彩
    [SerializeField]
    private Color _buttomColor = Color.white;//底部色彩
    [SerializeField, Range(-1f, 1f)]
    private float _colorScale = 0f; //色彩过度的比例

    public override void ModifyMesh(VertexHelper vh)
    {
        //断定当前对象是不是为激活状况
        if (!IsActive()) return;

        List<UIVertex> vList = new List<UIVertex>();
        vh.GetUIVertexStream(vList);
        ModifyVertices(vList);
        vh.Clear();
        vh.AddUIVertexTriangleStream(vList);
    }

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

        //记载初始顶点的纵坐标
        float topPosY = vList[0].position.y;
        float buttomPosY = vList[0].position.y;

        //遍历所有顶点,找出最上与下的,用来下面做色彩差值
        for (int i = 0; i < vList.Count; i++)
        {
            UIVertex temp = vList[i];
            topPosY = Mathf.Max(topPosY, temp.position.y);
            buttomPosY = Mathf.Min(buttomPosY, temp.position.y);
        }

        //盘算全部文本组件顶点的垂直间距
        float intervalHeight = topPosY - buttomPosY;

        //开端修正顶点
        for (int i = 0; i < vList.Count; i++)
        {
            UIVertex temp = vList[i];
            //色彩差值  色彩不遭到源字体色彩的影响
            temp.color = Color.Lerp(_buttomColor, _topColor, (intervalHeight > 0 ? (temp.position.y - buttomPosY) / intervalHeight : 0) + _colorScale);
            
            //如果想让色彩遭到源字体色彩的影响,可以应用下面这句
            //temp.color=temp.color* Color.Lerp(_buttomColor, _topColor, (intervalHeight > 0 ? (temp.position.y - buttomPosY) / intervalHeight : 0) + _colorScale);
            
            //再赋值会本来的顶点,抛回去绘制
            vList[i] = temp;
        }
    }
}/*
最后修正时光:
主题:
功效:
著作:
*/
using FZY;
using Syste



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: