TMP_UpdateRegistery.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.UI.Collections;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. namespace TMPro
  7. {
  8. /// <summary>
  9. /// Class for handling and scheduling text object updates.
  10. /// </summary>
  11. public class TMP_UpdateRegistry
  12. {
  13. private static TMP_UpdateRegistry s_Instance;
  14. private readonly List<ICanvasElement> m_LayoutRebuildQueue = new List<ICanvasElement>();
  15. private HashSet<int> m_LayoutQueueLookup = new HashSet<int>();
  16. private readonly List<ICanvasElement> m_GraphicRebuildQueue = new List<ICanvasElement>();
  17. private HashSet<int> m_GraphicQueueLookup = new HashSet<int>();
  18. //private bool m_PerformingLayoutUpdate;
  19. //private bool m_PerformingGraphicUpdate;
  20. /// <summary>
  21. /// Get a singleton instance of the registry
  22. /// </summary>
  23. public static TMP_UpdateRegistry instance
  24. {
  25. get
  26. {
  27. if (TMP_UpdateRegistry.s_Instance == null)
  28. TMP_UpdateRegistry.s_Instance = new TMP_UpdateRegistry();
  29. return TMP_UpdateRegistry.s_Instance;
  30. }
  31. }
  32. /// <summary>
  33. /// Register to receive callback from the Canvas System.
  34. /// </summary>
  35. protected TMP_UpdateRegistry()
  36. {
  37. //Debug.Log("Adding WillRenderCanvases");
  38. Canvas.willRenderCanvases += PerformUpdateForCanvasRendererObjects;
  39. }
  40. /// <summary>
  41. /// Function to register elements which require a layout rebuild.
  42. /// </summary>
  43. /// <param name="element"></param>
  44. public static void RegisterCanvasElementForLayoutRebuild(ICanvasElement element)
  45. {
  46. TMP_UpdateRegistry.instance.InternalRegisterCanvasElementForLayoutRebuild(element);
  47. }
  48. private bool InternalRegisterCanvasElementForLayoutRebuild(ICanvasElement element)
  49. {
  50. int id = (element as Object).GetInstanceID();
  51. if (m_LayoutQueueLookup.Contains(id))
  52. return false;
  53. m_LayoutQueueLookup.Add(id);
  54. m_LayoutRebuildQueue.Add(element);
  55. return true;
  56. }
  57. /// <summary>
  58. /// Function to register elements which require a graphic rebuild.
  59. /// </summary>
  60. /// <param name="element"></param>
  61. public static void RegisterCanvasElementForGraphicRebuild(ICanvasElement element)
  62. {
  63. TMP_UpdateRegistry.instance.InternalRegisterCanvasElementForGraphicRebuild(element);
  64. }
  65. private bool InternalRegisterCanvasElementForGraphicRebuild(ICanvasElement element)
  66. {
  67. int id = (element as Object).GetInstanceID();
  68. if (m_GraphicQueueLookup.Contains(id))
  69. return false;
  70. m_GraphicQueueLookup.Add(id);
  71. m_GraphicRebuildQueue.Add(element);
  72. return true;
  73. }
  74. /// <summary>
  75. /// Method to handle objects that need updating.
  76. /// </summary>
  77. private void PerformUpdateForCanvasRendererObjects()
  78. {
  79. //Debug.Log("Performing update of CanvasRenderer objects at Frame: " + Time.frameCount);
  80. // Processing elements that require a layout rebuild.
  81. //this.m_PerformingLayoutUpdate = true;
  82. for (int index = 0; index < m_LayoutRebuildQueue.Count; index++)
  83. {
  84. ICanvasElement element = TMP_UpdateRegistry.instance.m_LayoutRebuildQueue[index];
  85. element.Rebuild(CanvasUpdate.Prelayout);
  86. }
  87. if (m_LayoutRebuildQueue.Count > 0)
  88. {
  89. m_LayoutRebuildQueue.Clear();
  90. m_LayoutQueueLookup.Clear();
  91. }
  92. // Update font assets before graphic rebuild
  93. // Processing elements that require a graphic rebuild.
  94. for (int index = 0; index < m_GraphicRebuildQueue.Count; index++)
  95. {
  96. ICanvasElement element = TMP_UpdateRegistry.instance.m_GraphicRebuildQueue[index];
  97. element.Rebuild(CanvasUpdate.PreRender);
  98. }
  99. // If there are no objects in the queue, we don't need to clear the lists again.
  100. if (m_GraphicRebuildQueue.Count > 0)
  101. {
  102. m_GraphicRebuildQueue.Clear();
  103. m_GraphicQueueLookup.Clear();
  104. }
  105. }
  106. /// <summary>
  107. /// Method to handle objects that need updating.
  108. /// </summary>
  109. private void PerformUpdateForMeshRendererObjects()
  110. {
  111. Debug.Log("Perform update of MeshRenderer objects.");
  112. }
  113. /// <summary>
  114. /// Function to unregister elements which no longer require a rebuild.
  115. /// </summary>
  116. /// <param name="element"></param>
  117. public static void UnRegisterCanvasElementForRebuild(ICanvasElement element)
  118. {
  119. TMP_UpdateRegistry.instance.InternalUnRegisterCanvasElementForLayoutRebuild(element);
  120. TMP_UpdateRegistry.instance.InternalUnRegisterCanvasElementForGraphicRebuild(element);
  121. }
  122. private void InternalUnRegisterCanvasElementForLayoutRebuild(ICanvasElement element)
  123. {
  124. int id = (element as Object).GetInstanceID();
  125. //element.LayoutComplete();
  126. TMP_UpdateRegistry.instance.m_LayoutRebuildQueue.Remove(element);
  127. m_GraphicQueueLookup.Remove(id);
  128. }
  129. private void InternalUnRegisterCanvasElementForGraphicRebuild(ICanvasElement element)
  130. {
  131. int id = (element as Object).GetInstanceID();
  132. //element.GraphicUpdateComplete();
  133. TMP_UpdateRegistry.instance.m_GraphicRebuildQueue.Remove(element);
  134. m_LayoutQueueLookup.Remove(id);
  135. }
  136. }
  137. }