Patched Gradient to use Color32 (for more vibrant colors)

Also corrected an obvious switch where each of the gradient directions were backwards :D

--HG--
branch : develop_4.6
pull/413/head
Simon (darkside) Jackson 2015-11-06 20:27:47 +00:00
parent d89d80f968
commit cfe6f3057a
1 changed files with 24 additions and 16 deletions

View File

@ -8,11 +8,11 @@ namespace UnityEngine.UI.Extensions
[AddComponentMenu("UI/Effects/Extensions/Gradient")] [AddComponentMenu("UI/Effects/Extensions/Gradient")]
public class Gradient : BaseVertexEffect public class Gradient : BaseVertexEffect
{ {
public GradientMode gradientMode = GradientMode.Global; public GradientMode gradientMode = GradientMode.Local;
public GradientDir gradientDir = GradientDir.Vertical; public GradientDir gradientDir = GradientDir.Vertical;
public bool overwriteAllColor = false; public bool overwriteAllColor = false;
public Color vertex1 = Color.white; public Color32 vertex1 = Color.white;
public Color vertex2 = Color.black; public Color32 vertex2 = Color.black;
private Graphic targetGraphic; private Graphic targetGraphic;
protected override void Start() protected override void Start()
@ -30,7 +30,7 @@ namespace UnityEngine.UI.Extensions
UIVertex uiVertex = vertexList[0]; UIVertex uiVertex = vertexList[0];
if (gradientMode == GradientMode.Global) if (gradientMode == GradientMode.Global)
{ {
if (gradientDir == GradientDir.DiagonalLeftToRight || gradientDir == GradientDir.DiagonalRightToLeft) if (gradientDir == GradientDir.DiagonalTopLeftToRight || gradientDir == GradientDir.DiagonalBottomLeftToRight)
{ {
#if UNITY_EDITOR #if UNITY_EDITOR
Debug.LogWarning("Diagonal dir is not supported in Global mode"); Debug.LogWarning("Diagonal dir is not supported in Global mode");
@ -47,7 +47,7 @@ namespace UnityEngine.UI.Extensions
uiVertex = vertexList[i]; uiVertex = vertexList[i];
if (!overwriteAllColor && uiVertex.color != targetGraphic.color) if (!overwriteAllColor && uiVertex.color != targetGraphic.color)
continue; continue;
uiVertex.color *= Color.Lerp(vertex2, vertex1, ((gradientDir == GradientDir.Vertical ? uiVertex.position.y : uiVertex.position.x) - bottomY) / uiElementHeight); uiVertex.color = Color32.Lerp(vertex2, vertex1, ((gradientDir == GradientDir.Vertical ? uiVertex.position.y : uiVertex.position.x) - bottomY) / uiElementHeight);
vertexList[i] = uiVertex; vertexList[i] = uiVertex;
} }
} }
@ -60,17 +60,23 @@ namespace UnityEngine.UI.Extensions
continue; continue;
switch (gradientDir) switch (gradientDir)
{ {
case GradientDir.Vertical:
uiVertex.color *= (i % 4 == 0 || (i - 1) % 4 == 0) ? vertex1 : vertex2;
break;
case GradientDir.Horizontal: case GradientDir.Horizontal:
uiVertex.color *= (i % 4 == 0 || (i - 3) % 4 == 0) ? vertex1 : vertex2; uiVertex.color = (i % 4 == 0 || (i - 1) % 4 == 0) ? vertex1 : vertex2;
break; break;
case GradientDir.DiagonalLeftToRight: case GradientDir.Vertical:
uiVertex.color *= (i % 4 == 0) ? vertex1 : ((i - 2) % 4 == 0 ? vertex2 : Color.Lerp(vertex2, vertex1, 0.5f)); uiVertex.color = (i % 4 == 0 || (i - 3) % 4 == 0) ? vertex1 : vertex2;
break; break;
case GradientDir.DiagonalRightToLeft: case GradientDir.DiagonalBottomLeftToRight:
uiVertex.color *= ((i - 1) % 4 == 0) ? vertex1 : ((i - 3) % 4 == 0 ? vertex2 : Color.Lerp(vertex2, vertex1, 0.5f)); uiVertex.color = (i % 4 == 0) ? vertex1 : ((i - 2) % 4 == 0 ? vertex2 : Color32.Lerp(vertex2, vertex1, 0.5f));
break;
case GradientDir.DiagonalTopLeftToRight:
uiVertex.color = ((i - 1) % 4 == 0) ? vertex1 : ((i - 3) % 4 == 0 ? vertex2 : Color32.Lerp(vertex2, vertex1, 0.5f));
break;
case GradientDir.DiagonalTopRightToLeft:
uiVertex.color = (i % 4 == 0) ? vertex2 : ((i - 2) % 4 == 0 ? vertex1 : Color32.Lerp(vertex1, vertex2, 0.5f));
break;
case GradientDir.DiagonalBottomRightToLeft:
uiVertex.color = ((i - 1) % 4 == 0) ? vertex2 : ((i - 3) % 4 == 0 ? vertex1 : Color32.Lerp(vertex1, vertex2, 0.5f));
break; break;
} }
@ -78,7 +84,7 @@ namespace UnityEngine.UI.Extensions
} }
} }
} }
private bool CompareCarefully(Color col1, Color col2) private bool CompareCarefully(Color32 col1, Color32 col2)
{ {
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) 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)
return true; return true;
@ -96,8 +102,10 @@ namespace UnityEngine.UI.Extensions
{ {
Vertical, Vertical,
Horizontal, Horizontal,
DiagonalLeftToRight, DiagonalTopLeftToRight,
DiagonalRightToLeft DiagonalBottomLeftToRight,
DiagonalTopRightToLeft,
DiagonalBottomRightToLeft
//Free //Free
} }
//enum color mode Additive, Multiply, Overwrite //enum color mode Additive, Multiply, Overwrite