GradientColor.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. //-------------------------------------------------------------------------------
  2. // Copyright (c) 2016 Tag of Joy
  3. // E-mail: info@tagofjoy.lt
  4. // To use this, you must have purchased it on the Unity Asset Store (http://u3d.as/n57)
  5. // Sharing or distribution are not permitted
  6. //-------------------------------------------------------------------------------
  7. using UnityEngine;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using UnityEngine.UI;
  11. using System.Text.RegularExpressions;
  12. [AddComponentMenu("UI/ToJ Effects/Gradient Color", 1)]
  13. [RequireComponent(typeof(Text))]
  14. public class GradientColor : BaseMeshEffect
  15. {
  16. public enum GradientMode
  17. {
  18. Local = 0,
  19. GlobalTextArea = 1,
  20. GlobalFullRect = 2
  21. }
  22. public enum GradientDirection
  23. {
  24. Vertical,
  25. Horizontal
  26. }
  27. public enum ColorMode
  28. {
  29. Override,
  30. Additive,
  31. Multiply
  32. }
  33. [SerializeField]
  34. private GradientMode m_GradientMode = GradientMode.Local;
  35. [SerializeField]
  36. private GradientDirection m_GradientDirection = GradientDirection.Vertical;
  37. [SerializeField]
  38. private ColorMode m_ColorMode = ColorMode.Override;
  39. [SerializeField]
  40. public Color m_FirstColor = Color.white;
  41. [SerializeField]
  42. public Color m_SecondColor = Color.black;
  43. [SerializeField]
  44. private bool m_UseGraphicAlpha = true;
  45. private List<UIVertex> m_Verts = new List<UIVertex>();
  46. protected GradientColor () { }
  47. #if UNITY_EDITOR
  48. protected override void OnValidate()
  49. {
  50. gradientMode = m_GradientMode;
  51. gradientDirection = m_GradientDirection;
  52. colorMode = m_ColorMode;
  53. firstColor = m_FirstColor;
  54. secondColor = m_SecondColor;
  55. useGraphicAlpha = m_UseGraphicAlpha;
  56. base.OnValidate();
  57. }
  58. #endif
  59. public GradientMode gradientMode
  60. {
  61. get { return m_GradientMode; }
  62. set
  63. {
  64. m_GradientMode = value;
  65. if (graphic != null)
  66. graphic.SetVerticesDirty();
  67. }
  68. }
  69. public GradientDirection gradientDirection
  70. {
  71. get { return m_GradientDirection; }
  72. set
  73. {
  74. m_GradientDirection = value;
  75. if (graphic != null)
  76. graphic.SetVerticesDirty();
  77. }
  78. }
  79. public ColorMode colorMode
  80. {
  81. get { return m_ColorMode; }
  82. set
  83. {
  84. m_ColorMode = value;
  85. if (graphic != null)
  86. graphic.SetVerticesDirty();
  87. }
  88. }
  89. public Color firstColor
  90. {
  91. get { return m_FirstColor; }
  92. set
  93. {
  94. m_FirstColor = value;
  95. if (graphic != null)
  96. graphic.SetVerticesDirty();
  97. }
  98. }
  99. public Color secondColor
  100. {
  101. get { return m_SecondColor; }
  102. set
  103. {
  104. m_SecondColor = value;
  105. if (graphic != null)
  106. graphic.SetVerticesDirty();
  107. }
  108. }
  109. public bool useGraphicAlpha
  110. {
  111. get { return m_UseGraphicAlpha; }
  112. set
  113. {
  114. m_UseGraphicAlpha = value;
  115. if (graphic != null)
  116. graphic.SetVerticesDirty();
  117. }
  118. }
  119. public override void ModifyMesh (VertexHelper vh)
  120. {
  121. if (!IsActive ())
  122. {
  123. return;
  124. }
  125. vh.GetUIVertexStream(m_Verts);
  126. if (m_Verts.Count == 0)
  127. {
  128. return;
  129. }
  130. if ((gradientMode == GradientMode.GlobalTextArea) || (gradientMode == GradientMode.GlobalFullRect))
  131. {
  132. Vector2 topLeftPos = Vector2.zero;
  133. Vector2 bottomRightPos = Vector2.zero;
  134. if (gradientMode == GradientMode.GlobalFullRect)
  135. {
  136. Rect rect = GetComponent<RectTransform>().rect;
  137. topLeftPos = new Vector2(rect.xMin, rect.yMax);
  138. bottomRightPos = new Vector2(rect.xMax, rect.yMin);
  139. }
  140. else
  141. {
  142. topLeftPos = m_Verts[0].position;
  143. bottomRightPos = m_Verts[m_Verts.Count - 1].position;
  144. for (int i = 0; i < m_Verts.Count; i++)
  145. {
  146. if (m_Verts[i].position.x < topLeftPos.x)
  147. {
  148. topLeftPos.x = m_Verts[i].position.x;
  149. }
  150. if (m_Verts[i].position.y > topLeftPos.y)
  151. {
  152. topLeftPos.y = m_Verts[i].position.y;
  153. }
  154. if (m_Verts[i].position.x > bottomRightPos.x)
  155. {
  156. bottomRightPos.x = m_Verts[i].position.x;
  157. }
  158. if (m_Verts[i].position.y < bottomRightPos.y)
  159. {
  160. bottomRightPos.y = m_Verts[i].position.y;
  161. }
  162. }
  163. }
  164. float overallHeight = topLeftPos.y - bottomRightPos.y;
  165. float overallWidth = bottomRightPos.x - topLeftPos.x;
  166. for (int i = 0; i < m_Verts.Count; i++)
  167. {
  168. UIVertex uiVertex = m_Verts[i];
  169. if (gradientDirection == GradientDirection.Vertical)
  170. {
  171. Color newColor = Color.Lerp(firstColor, secondColor, (topLeftPos.y - uiVertex.position.y) / overallHeight);
  172. uiVertex.color = CalculateColor(uiVertex.color, newColor, colorMode);
  173. }
  174. else
  175. {
  176. Color newColor = Color.Lerp(firstColor, secondColor, (uiVertex.position.x - topLeftPos.x) / overallWidth);
  177. uiVertex.color = CalculateColor(uiVertex.color, newColor, colorMode);
  178. }
  179. if (useGraphicAlpha)
  180. {
  181. uiVertex.color.a = (byte)((uiVertex.color.a * m_Verts[i].color.a) / 255);
  182. }
  183. m_Verts[i] = uiVertex;
  184. }
  185. }
  186. else
  187. {
  188. for (int i = 0; i < m_Verts.Count; i++)
  189. {
  190. UIVertex uiVertex = m_Verts[i];
  191. if (gradientDirection == GradientDirection.Vertical)
  192. {
  193. if ((i % 6 == 0) || (i % 6 == 1) || (i % 6 == 5))
  194. {
  195. Color newColor = firstColor;
  196. uiVertex.color = CalculateColor(uiVertex.color, newColor, colorMode);
  197. }
  198. else
  199. {
  200. Color newColor = secondColor;
  201. uiVertex.color = CalculateColor(uiVertex.color, newColor, colorMode);
  202. }
  203. }
  204. else
  205. {
  206. if ((i % 6 == 0) || (i % 6 == 4) || (i % 6 == 5))
  207. {
  208. Color newColor = firstColor;
  209. uiVertex.color = CalculateColor(uiVertex.color, newColor, colorMode);
  210. }
  211. else
  212. {
  213. Color newColor = secondColor;
  214. uiVertex.color = CalculateColor(uiVertex.color, newColor, colorMode);
  215. }
  216. }
  217. if (useGraphicAlpha)
  218. {
  219. uiVertex.color.a = (byte)((uiVertex.color.a * m_Verts[i].color.a) / 255);
  220. }
  221. m_Verts[i] = uiVertex;
  222. }
  223. }
  224. vh.Clear();
  225. vh.AddUIVertexTriangleStream(m_Verts);
  226. }
  227. private Color CalculateColor (Color initialColor, Color newColor, ColorMode colorMode)
  228. {
  229. if (colorMode == ColorMode.Override)
  230. {
  231. return newColor;
  232. }
  233. else if (colorMode == ColorMode.Additive)
  234. {
  235. return initialColor + newColor;
  236. }
  237. else if (colorMode == ColorMode.Multiply)
  238. {
  239. return initialColor * newColor;
  240. }
  241. return newColor;
  242. }
  243. }