Gradient 颜色渐变

 

 

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

namespace ExtraFoundation.Components
{
    [AddComponentMenu("UI/Effects/Gradient")]
    public class UIGradient : BaseMeshEffect
    {
        #region Public Declarations
        public enum Type
        {
            Vertical,
            Horizontal
        }
        #endregion

        #region Public Properties
        public Type GradientType = Type.Vertical;
        [Range(-1f, 1f)]
        public float Offset = 0f;
        public Gradient gradient;
        #endregion

        #region Public Methods
        public override void ModifyMesh(VertexHelper helper)
        {
            print("sadasd");
            if (!IsActive() || helper.currentVertCount == 0)
            {
                return;
            }

            vertexList.Clear();
            helper.GetUIVertexStream(vertexList);
            print(vertexList.Count);
            int nCount = vertexList.Count;
            switch (GradientType)
            {
                case Type.Vertical:
                    {
                        float fBottomY = vertexList[0].position.y;
                        float fTopY = vertexList[0].position.y;
                        float fYPos = 0f;

                        for (int i = nCount - 1; i >= 1; --i)
                        {
                            fYPos = vertexList[i].position.y;
                            if (fYPos > fTopY)
                                fTopY = fYPos;
                            else if (fYPos < fBottomY)
                                fBottomY = fYPos;
                        }

                        float fUIElementHeight = 1f / (fTopY - fBottomY);
                        UIVertex v = new UIVertex();

                        for (int i = 0; i < helper.currentVertCount; i++)
                        {
                            helper.PopulateUIVertex(ref v, i);
                            v.color = gradient.Evaluate((v.position.y - fBottomY) *
                             fUIElementHeight - Offset);
                            helper.SetUIVertex(v, i);
                        }
                    }
                    break;
                case Type.Horizontal:
                    {
                        float fLeftX = vertexList[0].position.x;
                        float fRightX = vertexList[0].position.x;
                        float fXPos = 0f;

                        for (int i = nCount - 1; i >= 1; --i)
                        {
                            fXPos = vertexList[i].position.x;
                            if (fXPos > fRightX)
                                fRightX = fXPos;
                            else if (fXPos < fLeftX)
                                fLeftX = fXPos;
                        }

                        float fUIElementWidth = 1f / (fRightX - fLeftX);
                        UIVertex v = new UIVertex();

                        for (int i = 0; i < helper.currentVertCount; i++)
                        {
                            helper.PopulateUIVertex(ref v, i);
                            v.color = gradient.Evaluate((v.position.x - fLeftX) *
                             fUIElementWidth - Offset);
                            helper.SetUIVertex(v, i);
                        }

                    }
                    break;
                default:
                    break;
            }
        }
       
        //时间
        public void Extrue_ed(float value)
        {

            print("value" + value);
            image.color = gradient.Evaluate(value);

           
        }
        #endregion
       
        #region Internal Fields
        private List<UIVertex> vertexList = new List<UIVertex>();
        public Image image;
        public Scrollbar demo;
        protected override void Start()
        {
            demo.onValueChanged.AddListener(Extrue_ed);
        }
        #endregion
    }
   
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值