Created
November 1, 2020 05:20
-
-
Save st4rdog/346200f4c090648d65893f82456ff6b7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections.Generic; | |
using UnityEngine.UI; | |
namespace UIGradient | |
{ | |
[AddComponentMenu("UI/Effects/Gradient")] | |
public class Gradient : BaseMeshEffect | |
{ | |
public enum Type | |
{ | |
Vertical, | |
Horizontal | |
} | |
// TODO: Evaluate actual gradient instead | |
[SerializeField] public UnityEngine.Gradient gradientTest; | |
[SerializeField] public Type GradientType = Type.Vertical; | |
[SerializeField] | |
[Range(-1.5f, 1.5f)] | |
public float Offset = 0f; | |
[SerializeField] private Color32 StartColor = Color.white; | |
[SerializeField] private Color32 EndColor = Color.black; | |
public override void ModifyMesh(VertexHelper vh) | |
{ | |
if (!IsActive()) return; | |
List<UIVertex> list = new List<UIVertex>(); | |
vh.GetUIVertexStream(list); | |
ModifyVertices(list); | |
vh.Clear(); | |
vh.AddUIVertexTriangleStream(list); | |
} | |
public void ModifyVertices(List<UIVertex> _vertexList) | |
{ | |
if (!IsActive()) return; | |
int nCount = _vertexList.Count; | |
switch (GradientType) | |
{ | |
case Type.Vertical: | |
{ | |
if (_vertexList.Count != 0) | |
{ | |
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); | |
for (int i = nCount - 1; i >= 0; --i) | |
{ | |
UIVertex uiVertex = _vertexList[i]; | |
uiVertex.color = Color32.Lerp(EndColor, StartColor, (uiVertex.position.y - fBottomY) * fUIElementHeight - Offset); | |
// TODO: Evaluate actual gradient instead | |
//uiVertex.color = (Color32)gradientTest.Evaluate((uiVertex.position.y - fBottomY) * fUIElementHeight - Offset); | |
// Tell people at https://forum.unity.com/threads/how-to-text-gradient-fill.268509/ | |
_vertexList[i] = uiVertex; | |
} | |
} | |
} | |
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); | |
for (int i = nCount - 1; i >= 0; --i) | |
{ | |
UIVertex uiVertex = _vertexList[i]; | |
uiVertex.color = Color32.Lerp(StartColor, EndColor, (uiVertex.position.x - fLeftX) * fUIElementWidth - Offset); | |
_vertexList[i] = uiVertex; | |
} | |
} | |
break; | |
default: | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment