123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- using UnityEngine;
- using System.Collections.Generic;
- using UnityEngine.UI;
- [AddComponentMenu("UI/Effects/Gradient")]
- public class Gradient : BaseMeshEffect
- {
- public enum Type
- {
- Vertical,
- Horizontal
- }
- [SerializeField]
- public Type GradientType = Type.Vertical;
- [SerializeField]
- [Range(-1.5f, 1.5f)]
- public float Offset = 0f;
- [SerializeField]
- public Color32 StartColor = Color.white;
- [SerializeField]
- public Color32 EndColor = Color.black;
- 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.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 = Color32.Lerp(EndColor, StartColor, (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 = Color32.Lerp(EndColor, StartColor, (v.position.x - fLeftX) * fUIElementWidth - Offset);
- helper.SetUIVertex(v, i);
- }
- }
- break;
- default:
- break;
- }
- }
- }
|