4using System.Collections.Generic;
8 [AddComponentMenu(
"UI/Effects/Extensions/Gradient")]
16 private bool _overwriteAllColor =
false;
21 private Graphic targetGraphic;
25 public GradientDir GradientDir {
get {
return _gradientDir; }
set { _gradientDir = value; graphic.SetVerticesDirty(); } }
26 public bool OverwriteAllColor {
get {
return _overwriteAllColor; }
set { _overwriteAllColor = value; graphic.SetVerticesDirty(); } }
27 public Color Vertex1 {
get {
return _vertex1; }
set { _vertex1 = value; graphic.SetAllDirty(); } }
28 public Color Vertex2 {
get {
return _vertex2; }
set { _vertex2 = value; graphic.SetAllDirty(); } }
33 targetGraphic = GetComponent<Graphic>();
38 int count = vh.currentVertCount;
39 if (!IsActive() || count == 0)
43 var vertexList =
new List<UIVertex>();
44 vh.GetUIVertexStream(vertexList);
45 UIVertex uiVertex =
new UIVertex();
51 Debug.LogWarning(
"Diagonal dir is not supported in Global mode");
55 float bottomY = _gradientDir ==
GradientDir.Vertical ? vertexList[vertexList.Count - 1].position.y : vertexList[vertexList.Count - 1].position.x;
56 float topY = _gradientDir ==
GradientDir.Vertical ? vertexList[0].position.y : vertexList[0].position.x;
58 float uiElementHeight = topY - bottomY;
60 for (
int i = 0; i < count; i++)
62 vh.PopulateUIVertex(ref uiVertex, i);
63 if (!_overwriteAllColor && uiVertex.color != targetGraphic.color)
65 uiVertex.color *=
Color.Lerp(_vertex2, _vertex1, ((_gradientDir ==
GradientDir.Vertical ? uiVertex.position.y : uiVertex.position.x) - bottomY) / uiElementHeight);
66 vh.SetUIVertex(uiVertex, i);
71 for (
int i = 0; i < count; i++)
73 vh.PopulateUIVertex(ref uiVertex, i);
74 if (!_overwriteAllColor && !CompareCarefully(uiVertex.color, targetGraphic.color))
79 uiVertex.color *= (i % 4 == 0 || (i - 1) % 4 == 0) ? _vertex1 : _vertex2;
82 uiVertex.color *= (i % 4 == 0 || (i - 3) % 4 == 0) ? _vertex1 : _vertex2;
85 uiVertex.color *= (i % 4 == 0) ? _vertex1 : ((i - 2) % 4 == 0 ? _vertex2 :
Color.Lerp(_vertex2, _vertex1, 0.5f));
88 uiVertex.color *= ((i - 1) % 4 == 0) ? _vertex1 : ((i - 3) % 4 == 0 ? _vertex2 :
Color.Lerp(_vertex2, _vertex1, 0.5f));
92 vh.SetUIVertex(uiVertex, i);
96 private bool CompareCarefully(
Color col1,
Color col2)
98 if (Mathf.Abs(col1.r - col2.r) < 0.003f && Mathf.Abs(col1.g - col2.g) < 0.003f && Mathf.Abs(col1.b - col2.b) < 0.003f && Mathf.Abs(col1.a - col2.a) < 0.003f)
override void ModifyMesh(VertexHelper vh)
Credit Erdener Gonenc - @PixelEnvision.