Updated Gradient scripts to "SetAllDirty" when properties changed, to ally changing through code.

Resolves #145

Thanks to @chrisparton1991 for the fix
pull/413/head
Simon Jackson 2017-06-04 18:19:14 +01:00
parent a059c148a6
commit 20a6cc962c
2 changed files with 51 additions and 22 deletions

View File

@ -8,13 +8,26 @@ namespace UnityEngine.UI.Extensions
[AddComponentMenu("UI/Effects/Extensions/Gradient")] [AddComponentMenu("UI/Effects/Extensions/Gradient")]
public class Gradient : BaseMeshEffect public class Gradient : BaseMeshEffect
{ {
public GradientMode gradientMode = GradientMode.Global; [SerializeField]
public GradientDir gradientDir = GradientDir.Vertical; private GradientMode _gradientMode = GradientMode.Global;
public bool overwriteAllColor = false; [SerializeField]
public Color vertex1 = Color.white; private GradientDir _gradientDir = GradientDir.Vertical;
public Color vertex2 = Color.black; [SerializeField]
private bool _overwriteAllColor = false;
[SerializeField]
private Color _vertex1 = Color.white;
[SerializeField]
private Color _vertex2 = Color.black;
private Graphic targetGraphic; private Graphic targetGraphic;
#region Properties
public GradientMode GradientMode { get { return _gradientMode; } set { _gradientMode = value; graphic.SetVerticesDirty(); } }
public GradientDir GradientDir { get { return _gradientDir; } set { _gradientDir = value; graphic.SetVerticesDirty(); } }
public bool OverwriteAllColor { get { return _overwriteAllColor; } set { _overwriteAllColor = value; graphic.SetVerticesDirty(); } }
public Color Vertex1 { get { return _vertex1; } set { _vertex1 = value; graphic.SetVerticesDirty(); } }
public Color Vertex2 { get { return _vertex2; } set { _vertex2 = value; graphic.SetVerticesDirty(); } }
#endregion
protected override void Awake() protected override void Awake()
{ {
targetGraphic = GetComponent<Graphic>(); targetGraphic = GetComponent<Graphic>();
@ -30,26 +43,26 @@ namespace UnityEngine.UI.Extensions
var vertexList = new List<UIVertex>(); var vertexList = new List<UIVertex>();
vh.GetUIVertexStream(vertexList); vh.GetUIVertexStream(vertexList);
UIVertex uiVertex = new UIVertex(); UIVertex uiVertex = new UIVertex();
if (gradientMode == GradientMode.Global) if (_gradientMode == GradientMode.Global)
{ {
if (gradientDir == GradientDir.DiagonalLeftToRight || gradientDir == GradientDir.DiagonalRightToLeft) if (_gradientDir == GradientDir.DiagonalLeftToRight || _gradientDir == GradientDir.DiagonalRightToLeft)
{ {
#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");
#endif #endif
gradientDir = GradientDir.Vertical; _gradientDir = GradientDir.Vertical;
} }
float bottomY = gradientDir == GradientDir.Vertical ? vertexList[vertexList.Count - 1].position.y : vertexList[vertexList.Count - 1].position.x; float bottomY = _gradientDir == GradientDir.Vertical ? vertexList[vertexList.Count - 1].position.y : vertexList[vertexList.Count - 1].position.x;
float topY = gradientDir == GradientDir.Vertical ? vertexList[0].position.y : vertexList[0].position.x; float topY = _gradientDir == GradientDir.Vertical ? vertexList[0].position.y : vertexList[0].position.x;
float uiElementHeight = topY - bottomY; float uiElementHeight = topY - bottomY;
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
vh.PopulateUIVertex(ref uiVertex, i); vh.PopulateUIVertex(ref uiVertex, 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 *= Color.Lerp(_vertex2, _vertex1, ((_gradientDir == GradientDir.Vertical ? uiVertex.position.y : uiVertex.position.x) - bottomY) / uiElementHeight);
vh.SetUIVertex(uiVertex, i); vh.SetUIVertex(uiVertex, i);
} }
} }
@ -58,21 +71,21 @@ namespace UnityEngine.UI.Extensions
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
vh.PopulateUIVertex(ref uiVertex, i); vh.PopulateUIVertex(ref uiVertex, i);
if (!overwriteAllColor && !CompareCarefully(uiVertex.color, targetGraphic.color)) if (!_overwriteAllColor && !CompareCarefully(uiVertex.color, targetGraphic.color))
continue; continue;
switch (gradientDir) switch (_gradientDir)
{ {
case GradientDir.Vertical: case GradientDir.Vertical:
uiVertex.color *= (i % 4 == 0 || (i - 1) % 4 == 0) ? vertex1 : vertex2; uiVertex.color *= (i % 4 == 0 || (i - 1) % 4 == 0) ? _vertex1 : _vertex2;
break; break;
case GradientDir.Horizontal: case GradientDir.Horizontal:
uiVertex.color *= (i % 4 == 0 || (i - 3) % 4 == 0) ? vertex1 : vertex2; uiVertex.color *= (i % 4 == 0 || (i - 3) % 4 == 0) ? _vertex1 : _vertex2;
break; break;
case GradientDir.DiagonalLeftToRight: case GradientDir.DiagonalLeftToRight:
uiVertex.color *= (i % 4 == 0) ? vertex1 : ((i - 2) % 4 == 0 ? vertex2 : Color.Lerp(vertex2, vertex1, 0.5f)); uiVertex.color *= (i % 4 == 0) ? _vertex1 : ((i - 2) % 4 == 0 ? _vertex2 : Color.Lerp(_vertex2, _vertex1, 0.5f));
break; break;
case GradientDir.DiagonalRightToLeft: case GradientDir.DiagonalRightToLeft:
uiVertex.color *= ((i - 1) % 4 == 0) ? vertex1 : ((i - 3) % 4 == 0 ? vertex2 : Color.Lerp(vertex2, vertex1, 0.5f)); uiVertex.color *= ((i - 1) % 4 == 0) ? _vertex1 : ((i - 3) % 4 == 0 ? _vertex2 : Color.Lerp(_vertex2, _vertex1, 0.5f));
break; break;
} }

View File

@ -29,22 +29,38 @@ namespace UnityEngine.UI.Extensions
#region Properties #region Properties
public Blend BlendMode { public Blend BlendMode {
get { return _blendMode; } get { return _blendMode; }
set { _blendMode = value; } set
{
_blendMode = value;
graphic.SetVerticesDirty();
}
} }
public UnityEngine.Gradient EffectGradient { public UnityEngine.Gradient EffectGradient {
get { return _effectGradient; } get { return _effectGradient; }
set { _effectGradient = value; } set
{
_effectGradient = value;
graphic.SetVerticesDirty();
}
} }
public Type GradientType { public Type GradientType {
get { return _gradientType; } get { return _gradientType; }
set { _gradientType = value; } set
{
_gradientType = value;
graphic.SetVerticesDirty();
}
} }
public float Offset { public float Offset {
get { return _offset; } get { return _offset; }
set { _offset = value; } set
{
_offset = value;
graphic.SetVerticesDirty();
}
} }
#endregion #endregion