2015-02-03 07:07:31 +08:00
|
|
|
/// Credit Breyer
|
|
|
|
/// Sourced from - http://forum.unity3d.com/threads/scripts-useful-4-6-scripts-collection.264161/#post-1780095
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace UnityEngine.UI.Extensions
|
|
|
|
{
|
|
|
|
[AddComponentMenu("UI/Effects/Extensions/Gradient")]
|
2015-09-10 05:31:17 +08:00
|
|
|
public class Gradient : BaseMeshEffect
|
2015-02-03 07:07:31 +08:00
|
|
|
{
|
2017-06-05 01:19:14 +08:00
|
|
|
[SerializeField]
|
|
|
|
private GradientMode _gradientMode = GradientMode.Global;
|
|
|
|
[SerializeField]
|
|
|
|
private GradientDir _gradientDir = GradientDir.Vertical;
|
|
|
|
[SerializeField]
|
|
|
|
private bool _overwriteAllColor = false;
|
|
|
|
[SerializeField]
|
|
|
|
private Color _vertex1 = Color.white;
|
|
|
|
[SerializeField]
|
|
|
|
private Color _vertex2 = Color.black;
|
2015-02-03 07:07:31 +08:00
|
|
|
private Graphic targetGraphic;
|
|
|
|
|
2017-06-05 01:19:14 +08:00
|
|
|
#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(); } }
|
2017-06-05 23:01:47 +08:00
|
|
|
public Color Vertex1 { get { return _vertex1; } set { _vertex1 = value; graphic.SetAllDirty(); } }
|
|
|
|
public Color Vertex2 { get { return _vertex2; } set { _vertex2 = value; graphic.SetAllDirty(); } }
|
2017-06-05 01:19:14 +08:00
|
|
|
#endregion
|
|
|
|
|
2017-03-12 18:45:39 +08:00
|
|
|
protected override void Awake()
|
2015-02-03 07:07:31 +08:00
|
|
|
{
|
|
|
|
targetGraphic = GetComponent<Graphic>();
|
|
|
|
}
|
|
|
|
|
2015-10-11 18:33:43 +08:00
|
|
|
public override void ModifyMesh(VertexHelper vh)
|
2015-02-03 07:07:31 +08:00
|
|
|
{
|
2015-10-11 18:33:43 +08:00
|
|
|
int count = vh.currentVertCount;
|
|
|
|
if (!IsActive() || count == 0)
|
2015-02-03 07:07:31 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-10-11 18:33:43 +08:00
|
|
|
var vertexList = new List<UIVertex>();
|
|
|
|
vh.GetUIVertexStream(vertexList);
|
|
|
|
UIVertex uiVertex = new UIVertex();
|
2017-06-05 01:19:14 +08:00
|
|
|
if (_gradientMode == GradientMode.Global)
|
2015-02-03 07:07:31 +08:00
|
|
|
{
|
2017-06-05 01:19:14 +08:00
|
|
|
if (_gradientDir == GradientDir.DiagonalLeftToRight || _gradientDir == GradientDir.DiagonalRightToLeft)
|
2015-02-03 07:07:31 +08:00
|
|
|
{
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
Debug.LogWarning("Diagonal dir is not supported in Global mode");
|
|
|
|
#endif
|
2017-06-05 01:19:14 +08:00
|
|
|
_gradientDir = GradientDir.Vertical;
|
2015-02-03 07:07:31 +08:00
|
|
|
}
|
2017-06-05 01:19:14 +08:00
|
|
|
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;
|
2015-02-03 07:07:31 +08:00
|
|
|
|
|
|
|
float uiElementHeight = topY - bottomY;
|
|
|
|
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
{
|
2015-10-11 18:33:43 +08:00
|
|
|
vh.PopulateUIVertex(ref uiVertex, i);
|
2017-06-05 01:19:14 +08:00
|
|
|
if (!_overwriteAllColor && uiVertex.color != targetGraphic.color)
|
2015-02-03 07:07:31 +08:00
|
|
|
continue;
|
2017-06-05 01:19:14 +08:00
|
|
|
uiVertex.color *= Color.Lerp(_vertex2, _vertex1, ((_gradientDir == GradientDir.Vertical ? uiVertex.position.y : uiVertex.position.x) - bottomY) / uiElementHeight);
|
2015-10-11 18:33:43 +08:00
|
|
|
vh.SetUIVertex(uiVertex, i);
|
2015-02-03 07:07:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
{
|
2015-10-11 18:33:43 +08:00
|
|
|
vh.PopulateUIVertex(ref uiVertex, i);
|
2017-06-05 01:19:14 +08:00
|
|
|
if (!_overwriteAllColor && !CompareCarefully(uiVertex.color, targetGraphic.color))
|
2015-02-03 07:07:31 +08:00
|
|
|
continue;
|
2017-06-05 01:19:14 +08:00
|
|
|
switch (_gradientDir)
|
2015-02-03 07:07:31 +08:00
|
|
|
{
|
|
|
|
case GradientDir.Vertical:
|
2017-06-05 01:19:14 +08:00
|
|
|
uiVertex.color *= (i % 4 == 0 || (i - 1) % 4 == 0) ? _vertex1 : _vertex2;
|
2015-02-03 07:07:31 +08:00
|
|
|
break;
|
|
|
|
case GradientDir.Horizontal:
|
2017-06-05 01:19:14 +08:00
|
|
|
uiVertex.color *= (i % 4 == 0 || (i - 3) % 4 == 0) ? _vertex1 : _vertex2;
|
2015-02-03 07:07:31 +08:00
|
|
|
break;
|
|
|
|
case GradientDir.DiagonalLeftToRight:
|
2017-06-05 01:19:14 +08:00
|
|
|
uiVertex.color *= (i % 4 == 0) ? _vertex1 : ((i - 2) % 4 == 0 ? _vertex2 : Color.Lerp(_vertex2, _vertex1, 0.5f));
|
2015-02-03 07:07:31 +08:00
|
|
|
break;
|
|
|
|
case GradientDir.DiagonalRightToLeft:
|
2017-06-05 01:19:14 +08:00
|
|
|
uiVertex.color *= ((i - 1) % 4 == 0) ? _vertex1 : ((i - 3) % 4 == 0 ? _vertex2 : Color.Lerp(_vertex2, _vertex1, 0.5f));
|
2015-02-03 07:07:31 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
2015-10-11 18:33:43 +08:00
|
|
|
vh.SetUIVertex(uiVertex, i);
|
2015-02-03 07:07:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private bool CompareCarefully(Color col1, Color 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)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum GradientMode
|
|
|
|
{
|
|
|
|
Global,
|
|
|
|
Local
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum GradientDir
|
|
|
|
{
|
|
|
|
Vertical,
|
|
|
|
Horizontal,
|
|
|
|
DiagonalLeftToRight,
|
|
|
|
DiagonalRightToLeft
|
|
|
|
//Free
|
|
|
|
}
|
|
|
|
//enum color mode Additive, Multiply, Overwrite
|
|
|
|
}
|