Gradient.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /// Credit Breyer
  2. /// Sourced from - http://forum.unity3d.com/threads/scripts-useful-4-6-scripts-collection.264161/#post-1780095
  3. using System.Collections.Generic;
  4. namespace UnityEngine.UI.Extensions
  5. {
  6. [AddComponentMenu("UI/Effects/Extensions/Gradient")]
  7. public class Gradient : BaseMeshEffect
  8. {
  9. [SerializeField]
  10. private GradientMode _gradientMode = GradientMode.Global;
  11. [SerializeField]
  12. private GradientDir _gradientDir = GradientDir.Vertical;
  13. [SerializeField]
  14. private bool _overwriteAllColor = false;
  15. [SerializeField]
  16. private Color _vertex1 = Color.white;
  17. [SerializeField]
  18. private Color _vertex2 = Color.black;
  19. private Graphic targetGraphic;
  20. #region Properties
  21. public GradientMode GradientMode { get { return _gradientMode; } set { _gradientMode = value; graphic.SetVerticesDirty(); } }
  22. public GradientDir GradientDir { get { return _gradientDir; } set { _gradientDir = value; graphic.SetVerticesDirty(); } }
  23. public bool OverwriteAllColor { get { return _overwriteAllColor; } set { _overwriteAllColor = value; graphic.SetVerticesDirty(); } }
  24. public Color Vertex1 { get { return _vertex1; } set { _vertex1 = value; graphic.SetAllDirty(); } }
  25. public Color Vertex2 { get { return _vertex2; } set { _vertex2 = value; graphic.SetAllDirty(); } }
  26. #endregion
  27. protected override void Awake()
  28. {
  29. targetGraphic = GetComponent<Graphic>();
  30. }
  31. public override void ModifyMesh(VertexHelper vh)
  32. {
  33. int count = vh.currentVertCount;
  34. if (!IsActive() || count == 0)
  35. {
  36. return;
  37. }
  38. var vertexList = new List<UIVertex>();
  39. vh.GetUIVertexStream(vertexList);
  40. UIVertex uiVertex = new UIVertex();
  41. if (_gradientMode == GradientMode.Global)
  42. {
  43. if (_gradientDir == GradientDir.DiagonalLeftToRight || _gradientDir == GradientDir.DiagonalRightToLeft)
  44. {
  45. #if UNITY_EDITOR
  46. Debug.LogWarning("Diagonal dir is not supported in Global mode");
  47. #endif
  48. _gradientDir = GradientDir.Vertical;
  49. }
  50. float bottomY = _gradientDir == GradientDir.Vertical ? vertexList[vertexList.Count - 1].position.y : vertexList[vertexList.Count - 1].position.x;
  51. float topY = _gradientDir == GradientDir.Vertical ? vertexList[0].position.y : vertexList[0].position.x;
  52. float uiElementHeight = topY - bottomY;
  53. for (int i = 0; i < count; i++)
  54. {
  55. vh.PopulateUIVertex(ref uiVertex, i);
  56. if (!_overwriteAllColor && uiVertex.color != targetGraphic.color)
  57. continue;
  58. uiVertex.color *= Color.Lerp(_vertex2, _vertex1, ((_gradientDir == GradientDir.Vertical ? uiVertex.position.y : uiVertex.position.x) - bottomY) / uiElementHeight);
  59. vh.SetUIVertex(uiVertex, i);
  60. }
  61. }
  62. else
  63. {
  64. for (int i = 0; i < count; i++)
  65. {
  66. vh.PopulateUIVertex(ref uiVertex, i);
  67. if (!_overwriteAllColor && !CompareCarefully(uiVertex.color, targetGraphic.color))
  68. continue;
  69. switch (_gradientDir)
  70. {
  71. case GradientDir.Vertical:
  72. uiVertex.color *= (i % 4 == 0 || (i - 1) % 4 == 0) ? _vertex1 : _vertex2;
  73. break;
  74. case GradientDir.Horizontal:
  75. uiVertex.color *= (i % 4 == 0 || (i - 3) % 4 == 0) ? _vertex1 : _vertex2;
  76. break;
  77. case GradientDir.DiagonalLeftToRight:
  78. uiVertex.color *= (i % 4 == 0) ? _vertex1 : ((i - 2) % 4 == 0 ? _vertex2 : Color.Lerp(_vertex2, _vertex1, 0.5f));
  79. break;
  80. case GradientDir.DiagonalRightToLeft:
  81. uiVertex.color *= ((i - 1) % 4 == 0) ? _vertex1 : ((i - 3) % 4 == 0 ? _vertex2 : Color.Lerp(_vertex2, _vertex1, 0.5f));
  82. break;
  83. }
  84. vh.SetUIVertex(uiVertex, i);
  85. }
  86. }
  87. }
  88. private bool CompareCarefully(Color col1, Color col2)
  89. {
  90. 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)
  91. return true;
  92. return false;
  93. }
  94. }
  95. public enum GradientMode
  96. {
  97. Global,
  98. Local
  99. }
  100. public enum GradientDir
  101. {
  102. Vertical,
  103. Horizontal,
  104. DiagonalLeftToRight,
  105. DiagonalRightToLeft
  106. //Free
  107. }
  108. //enum color mode Additive, Multiply, Overwrite
  109. }