BaseMeshEffect.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. using UnityEngine.UI;
  5. namespace Coffee.UIEffects
  6. {
  7. /// <summary>
  8. /// Base class for effects that modify the generated Mesh.
  9. /// It works well not only for standard Graphic components (Image, RawImage, Text, etc.) but also for TextMeshPro and TextMeshProUGUI.
  10. /// </summary>
  11. [RequireComponent(typeof(Graphic))]
  12. [RequireComponent(typeof(RectTransform))]
  13. [ExecuteInEditMode]
  14. public abstract class BaseMeshEffect : UIBehaviour, IMeshModifier
  15. {
  16. RectTransform _rectTransform;
  17. Graphic _graphic;
  18. GraphicConnector _connector;
  19. /// <summary>
  20. /// The Graphic attached to this GameObject.
  21. /// </summary>
  22. protected GraphicConnector connector
  23. {
  24. get { return _connector ?? (_connector = GraphicConnector.FindConnector(graphic)); }
  25. }
  26. /// <summary>
  27. /// The Graphic attached to this GameObject.
  28. /// </summary>
  29. public Graphic graphic
  30. {
  31. get { return _graphic ? _graphic : _graphic = GetComponent<Graphic>(); }
  32. }
  33. /// <summary>
  34. /// The RectTransform attached to this GameObject.
  35. /// </summary>
  36. protected RectTransform rectTransform
  37. {
  38. get { return _rectTransform ? _rectTransform : _rectTransform = GetComponent<RectTransform>(); }
  39. }
  40. internal readonly List<UISyncEffect> syncEffects = new List<UISyncEffect>(0);
  41. /// <summary>
  42. /// Call used to modify mesh. (legacy)
  43. /// </summary>
  44. /// <param name="mesh">Mesh.</param>
  45. public virtual void ModifyMesh(Mesh mesh)
  46. {
  47. }
  48. /// <summary>
  49. /// Call used to modify mesh.
  50. /// </summary>
  51. /// <param name="vh">VertexHelper.</param>
  52. public virtual void ModifyMesh(VertexHelper vh)
  53. {
  54. ModifyMesh(vh, graphic);
  55. }
  56. public virtual void ModifyMesh(VertexHelper vh, Graphic graphic)
  57. {
  58. }
  59. /// <summary>
  60. /// Mark the vertices as dirty.
  61. /// </summary>
  62. protected virtual void SetVerticesDirty()
  63. {
  64. connector.SetVerticesDirty(graphic);
  65. foreach (var effect in syncEffects)
  66. {
  67. effect.SetVerticesDirty();
  68. }
  69. // #if TMP_PRESENT
  70. // if (textMeshPro)
  71. // {
  72. // foreach (var info in textMeshPro.textInfo.meshInfo)
  73. // {
  74. // var mesh = info.mesh;
  75. // if (mesh)
  76. // {
  77. // mesh.Clear();
  78. // mesh.vertices = info.vertices;
  79. // mesh.uv = info.uvs0;
  80. // mesh.uv2 = info.uvs2;
  81. // mesh.colors32 = info.colors32;
  82. // mesh.normals = info.normals;
  83. // mesh.tangents = info.tangents;
  84. // mesh.triangles = info.triangles;
  85. // }
  86. // }
  87. //
  88. // if (canvasRenderer)
  89. // {
  90. // canvasRenderer.SetMesh(textMeshPro.mesh);
  91. //
  92. // GetComponentsInChildren(false, s_SubMeshUIs);
  93. // foreach (var sm in s_SubMeshUIs)
  94. // {
  95. // sm.canvasRenderer.SetMesh(sm.mesh);
  96. // }
  97. //
  98. // s_SubMeshUIs.Clear();
  99. // }
  100. //
  101. // textMeshPro.havePropertiesChanged = true;
  102. // }
  103. // else
  104. // #endif
  105. // if (graphic)
  106. // {
  107. // graphic.SetVerticesDirty();
  108. // }
  109. }
  110. //################################
  111. // Protected Members.
  112. //################################
  113. /// <summary>
  114. /// Should the effect modify the mesh directly for TMPro?
  115. /// </summary>
  116. // protected virtual bool isLegacyMeshModifier
  117. // {
  118. // get { return false; }
  119. // }
  120. // protected virtual void Initialize()
  121. // {
  122. // if (_initialized) return;
  123. //
  124. // _initialized = true;
  125. // _graphic = _graphic ? _graphic : GetComponent<Graphic>();
  126. //
  127. // _connector = GraphicConnector.FindConnector(_graphic);
  128. //
  129. // // _canvasRenderer = _canvasRenderer ?? GetComponent<CanvasRenderer> ();
  130. // _rectTransform = _rectTransform ? _rectTransform : GetComponent<RectTransform>();
  131. // // #if TMP_PRESENT
  132. // // _textMeshPro = _textMeshPro ?? GetComponent<TMP_Text> ();
  133. // // #endif
  134. // }
  135. /// <summary>
  136. /// This function is called when the object becomes enabled and active.
  137. /// </summary>
  138. protected override void OnEnable()
  139. {
  140. connector.OnEnable(graphic);
  141. SetVerticesDirty();
  142. // SetVerticesDirty();
  143. // #if TMP_PRESENT
  144. // if (textMeshPro)
  145. // {
  146. // TMPro_EventManager.TEXT_CHANGED_EVENT.Add (OnTextChanged);
  147. // }
  148. // #endif
  149. //
  150. // #if UNITY_EDITOR && TMP_PRESENT
  151. // if (graphic && textMeshPro)
  152. // {
  153. // GraphicRebuildTracker.TrackGraphic (graphic);
  154. // }
  155. // #endif
  156. //
  157. // #if UNITY_5_6_OR_NEWER
  158. // if (graphic)
  159. // {
  160. // AdditionalCanvasShaderChannels channels = requiredChannels;
  161. // var canvas = graphic.canvas;
  162. // if (canvas && (canvas.additionalShaderChannels & channels) != channels)
  163. // {
  164. // Debug.LogWarningFormat (this, "Enable {1} of Canvas.additionalShaderChannels to use {0}.", GetType ().Name, channels);
  165. // }
  166. // }
  167. // #endif
  168. }
  169. /// <summary>
  170. /// This function is called when the behaviour becomes disabled () or inactive.
  171. /// </summary>
  172. protected override void OnDisable()
  173. {
  174. connector.OnDisable(graphic);
  175. SetVerticesDirty();
  176. }
  177. /// <summary>
  178. /// Mark the effect parameters as dirty.
  179. /// </summary>
  180. protected virtual void SetEffectParamsDirty()
  181. {
  182. if (!isActiveAndEnabled) return;
  183. SetVerticesDirty();
  184. }
  185. /// <summary>
  186. /// Callback for when properties have been changed by animation.
  187. /// </summary>
  188. protected override void OnDidApplyAnimationProperties()
  189. {
  190. if (!isActiveAndEnabled) return;
  191. SetEffectParamsDirty();
  192. }
  193. #if UNITY_EDITOR
  194. protected override void Reset()
  195. {
  196. if (!isActiveAndEnabled) return;
  197. SetVerticesDirty();
  198. }
  199. /// <summary>
  200. /// This function is called when the script is loaded or a value is changed in the inspector (Called in the editor only).
  201. /// </summary>
  202. protected override void OnValidate()
  203. {
  204. if (!isActiveAndEnabled) return;
  205. SetEffectParamsDirty();
  206. }
  207. #endif
  208. }
  209. }