TMP_UpdateManager.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using UnityEngine;
  2. using UnityEngine.Profiling;
  3. using UnityEngine.UI;
  4. using System.Collections.Generic;
  5. namespace TMPro
  6. {
  7. public class TMP_UpdateManager
  8. {
  9. private static TMP_UpdateManager s_Instance;
  10. private readonly HashSet<int> m_LayoutQueueLookup = new HashSet<int>();
  11. private readonly List<TMP_Text> m_LayoutRebuildQueue = new List<TMP_Text>();
  12. private readonly HashSet<int> m_GraphicQueueLookup = new HashSet<int>();
  13. private readonly List<TMP_Text> m_GraphicRebuildQueue = new List<TMP_Text>();
  14. private readonly HashSet<int> m_InternalUpdateLookup = new HashSet<int>();
  15. private readonly List<TMP_Text> m_InternalUpdateQueue = new List<TMP_Text>();
  16. private readonly HashSet<int> m_CullingUpdateLookup = new HashSet<int>();
  17. private readonly List<TMP_Text> m_CullingUpdateQueue = new List<TMP_Text>();
  18. /// <summary>
  19. /// Get a singleton instance of the registry
  20. /// </summary>
  21. static TMP_UpdateManager instance
  22. {
  23. get
  24. {
  25. if (s_Instance == null)
  26. s_Instance = new TMP_UpdateManager();
  27. return s_Instance;
  28. }
  29. }
  30. /// <summary>
  31. /// Register to receive rendering callbacks.
  32. /// </summary>
  33. TMP_UpdateManager()
  34. {
  35. Canvas.willRenderCanvases += DoRebuilds;
  36. }
  37. /// <summary>
  38. /// Function used as a replacement for LateUpdate() to handle SDF Scale updates and Legacy Animation updates.
  39. /// </summary>
  40. /// <param name="textObject"></param>
  41. internal static void RegisterTextObjectForUpdate(TMP_Text textObject)
  42. {
  43. Profiler.BeginSample("TMP.RegisterTextObjectForUpdate");
  44. instance.InternalRegisterTextObjectForUpdate(textObject);
  45. Profiler.EndSample();
  46. }
  47. private void InternalRegisterTextObjectForUpdate(TMP_Text textObject)
  48. {
  49. int id = textObject.GetInstanceID();
  50. if (m_InternalUpdateLookup.Contains(id))
  51. return;
  52. m_InternalUpdateLookup.Add(id);
  53. m_InternalUpdateQueue.Add(textObject);
  54. }
  55. /// <summary>
  56. /// Function to register elements which require a layout rebuild.
  57. /// </summary>
  58. /// <param name="element"></param>
  59. public static void RegisterTextElementForLayoutRebuild(TMP_Text element)
  60. {
  61. instance.InternalRegisterTextElementForLayoutRebuild(element);
  62. }
  63. private void InternalRegisterTextElementForLayoutRebuild(TMP_Text element)
  64. {
  65. int id = element.GetInstanceID();
  66. if (m_LayoutQueueLookup.Contains(id))
  67. return;
  68. m_LayoutQueueLookup.Add(id);
  69. m_LayoutRebuildQueue.Add(element);
  70. }
  71. /// <summary>
  72. /// Function to register elements which require a layout rebuild.
  73. /// </summary>
  74. /// <param name="element"></param>
  75. public static void RegisterTextElementForGraphicRebuild(TMP_Text element)
  76. {
  77. Profiler.BeginSample("TMP.RegisterTextElementForGraphicRebuild");
  78. instance.InternalRegisterTextElementForGraphicRebuild(element);
  79. Profiler.EndSample();
  80. }
  81. private void InternalRegisterTextElementForGraphicRebuild(TMP_Text element)
  82. {
  83. int id = element.GetInstanceID();
  84. if (m_GraphicQueueLookup.Contains(id))
  85. return;
  86. m_GraphicQueueLookup.Add(id);
  87. m_GraphicRebuildQueue.Add(element);
  88. }
  89. public static void RegisterTextElementForCullingUpdate(TMP_Text element)
  90. {
  91. Profiler.BeginSample("TMP.RegisterTextElementForCullingUpdate");
  92. instance.InternalRegisterTextElementForCullingUpdate(element);
  93. Profiler.EndSample();
  94. }
  95. private void InternalRegisterTextElementForCullingUpdate(TMP_Text element)
  96. {
  97. int id = element.GetInstanceID();
  98. if (m_CullingUpdateLookup.Contains(id))
  99. return;
  100. m_CullingUpdateLookup.Add(id);
  101. m_CullingUpdateQueue.Add(element);
  102. }
  103. /// <summary>
  104. /// Callback which occurs just before the cam is rendered.
  105. /// </summary>
  106. void OnCameraPreCull()
  107. {
  108. DoRebuilds();
  109. }
  110. /// <summary>
  111. /// Process the rebuild requests in the rebuild queues.
  112. /// </summary>
  113. void DoRebuilds()
  114. {
  115. // Handle text objects the require an update either as a result of scale changes or legacy animation.
  116. for (int i = 0; i < m_InternalUpdateQueue.Count; i++)
  117. {
  118. m_InternalUpdateQueue[i].InternalUpdate();
  119. }
  120. // Handle Layout Rebuild Phase
  121. for (int i = 0; i < m_LayoutRebuildQueue.Count; i++)
  122. {
  123. m_LayoutRebuildQueue[i].Rebuild(CanvasUpdate.Prelayout);
  124. }
  125. if (m_LayoutRebuildQueue.Count > 0)
  126. {
  127. m_LayoutRebuildQueue.Clear();
  128. m_LayoutQueueLookup.Clear();
  129. }
  130. // Handle Graphic Rebuild Phase
  131. for (int i = 0; i < m_GraphicRebuildQueue.Count; i++)
  132. {
  133. m_GraphicRebuildQueue[i].Rebuild(CanvasUpdate.PreRender);
  134. }
  135. // If there are no objects in the queue, we don't need to clear the lists again.
  136. if (m_GraphicRebuildQueue.Count > 0)
  137. {
  138. m_GraphicRebuildQueue.Clear();
  139. m_GraphicQueueLookup.Clear();
  140. }
  141. // Handle Culling Update
  142. for (int i = 0; i < m_CullingUpdateQueue.Count; i++)
  143. {
  144. m_CullingUpdateQueue[i].UpdateCulling();
  145. }
  146. // If there are no objects in the queue, we don't need to clear the lists again.
  147. if (m_CullingUpdateQueue.Count > 0)
  148. {
  149. m_CullingUpdateQueue.Clear();
  150. m_CullingUpdateLookup.Clear();
  151. }
  152. }
  153. internal static void UnRegisterTextObjectForUpdate(TMP_Text textObject)
  154. {
  155. Profiler.BeginSample("TMP.UnRegisterTextObjectForUpdate");
  156. instance.InternalUnRegisterTextObjectForUpdate(textObject);
  157. Profiler.EndSample();
  158. }
  159. /// <summary>
  160. /// Function to unregister elements which no longer require a rebuild.
  161. /// </summary>
  162. /// <param name="element"></param>
  163. public static void UnRegisterTextElementForRebuild(TMP_Text element)
  164. {
  165. instance.InternalUnRegisterTextElementForGraphicRebuild(element);
  166. instance.InternalUnRegisterTextElementForLayoutRebuild(element);
  167. instance.InternalUnRegisterTextObjectForUpdate(element);
  168. }
  169. private void InternalUnRegisterTextElementForGraphicRebuild(TMP_Text element)
  170. {
  171. Profiler.BeginSample("TMP.InternalUnRegisterTextElementForGraphicRebuild");
  172. int id = element.GetInstanceID();
  173. m_GraphicRebuildQueue.Remove(element);
  174. m_GraphicQueueLookup.Remove(id);
  175. Profiler.EndSample();
  176. }
  177. private void InternalUnRegisterTextElementForLayoutRebuild(TMP_Text element)
  178. {
  179. int id = element.GetInstanceID();
  180. m_LayoutRebuildQueue.Remove(element);
  181. m_LayoutQueueLookup.Remove(id);
  182. }
  183. private void InternalUnRegisterTextObjectForUpdate(TMP_Text textObject)
  184. {
  185. int id = textObject.GetInstanceID();
  186. m_InternalUpdateQueue.Remove(textObject);
  187. m_InternalUpdateLookup.Remove(id);
  188. }
  189. }
  190. }