暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TMP_EditorResourceManager.cs 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #if UNITY_EDITOR
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. namespace TMPro
  6. {
  7. public class TMP_EditorResourceManager
  8. {
  9. private static TMP_EditorResourceManager s_Instance;
  10. private readonly List<Object> m_ObjectUpdateQueue = new List<Object>();
  11. private HashSet<int> m_ObjectUpdateQueueLookup = new HashSet<int>();
  12. private readonly List<Object> m_ObjectReImportQueue = new List<Object>();
  13. private HashSet<int> m_ObjectReImportQueueLookup = new HashSet<int>();
  14. private readonly List<TMP_FontAsset> m_FontAssetDefinitionRefreshQueue = new List<TMP_FontAsset>();
  15. private HashSet<int> m_FontAssetDefinitionRefreshQueueLookup = new HashSet<int>();
  16. /// <summary>
  17. /// Get a singleton instance of the manager.
  18. /// </summary>
  19. public static TMP_EditorResourceManager instance
  20. {
  21. get
  22. {
  23. if (s_Instance == null)
  24. s_Instance = new TMP_EditorResourceManager();
  25. return s_Instance;
  26. }
  27. }
  28. /// <summary>
  29. /// Register to receive rendering callbacks.
  30. /// </summary>
  31. private TMP_EditorResourceManager()
  32. {
  33. Camera.onPostRender += OnCameraPostRender;
  34. Canvas.willRenderCanvases += OnPreRenderCanvases;
  35. }
  36. void OnCameraPostRender(Camera cam)
  37. {
  38. // Exclude the PreRenderCamera
  39. if (cam.cameraType != CameraType.SceneView)
  40. return;
  41. DoPostRenderUpdates();
  42. }
  43. void OnPreRenderCanvases()
  44. {
  45. DoPreRenderUpdates();
  46. }
  47. /// <summary>
  48. /// Register resource for re-import.
  49. /// </summary>
  50. /// <param name="obj"></param>
  51. internal static void RegisterResourceForReimport(Object obj)
  52. {
  53. instance.InternalRegisterResourceForReimport(obj);
  54. }
  55. private void InternalRegisterResourceForReimport(Object obj)
  56. {
  57. int id = obj.GetInstanceID();
  58. if (m_ObjectReImportQueueLookup.Contains(id))
  59. return;
  60. m_ObjectReImportQueueLookup.Add(id);
  61. m_ObjectReImportQueue.Add(obj);
  62. }
  63. /// <summary>
  64. /// Register resource to be updated.
  65. /// </summary>
  66. /// <param name="textObject"></param>
  67. internal static void RegisterResourceForUpdate(Object obj)
  68. {
  69. instance.InternalRegisterResourceForUpdate(obj);
  70. }
  71. private void InternalRegisterResourceForUpdate(Object obj)
  72. {
  73. int id = obj.GetInstanceID();
  74. if (m_ObjectUpdateQueueLookup.Contains(id))
  75. return;
  76. m_ObjectUpdateQueueLookup.Add(id);
  77. m_ObjectUpdateQueue.Add(obj);
  78. }
  79. /// <summary>
  80. ///
  81. /// </summary>
  82. /// <param name="fontAsset"></param>
  83. internal static void RegisterFontAssetForDefinitionRefresh(TMP_FontAsset fontAsset)
  84. {
  85. instance.InternalRegisterFontAssetForDefinitionRefresh(fontAsset);
  86. }
  87. private void InternalRegisterFontAssetForDefinitionRefresh(TMP_FontAsset fontAsset)
  88. {
  89. int id = fontAsset.GetInstanceID();
  90. if (m_FontAssetDefinitionRefreshQueueLookup.Contains(id))
  91. return;
  92. m_FontAssetDefinitionRefreshQueueLookup.Add(id);
  93. m_FontAssetDefinitionRefreshQueue.Add(fontAsset);
  94. }
  95. void DoPostRenderUpdates()
  96. {
  97. // Handle objects that need updating
  98. int objUpdateCount = m_ObjectUpdateQueue.Count;
  99. for (int i = 0; i < objUpdateCount; i++)
  100. {
  101. Object obj = m_ObjectUpdateQueue[i];
  102. if (obj != null)
  103. {
  104. EditorUtility.SetDirty(obj);
  105. }
  106. }
  107. if (objUpdateCount > 0)
  108. {
  109. //Debug.Log("Saving assets");
  110. //AssetDatabase.SaveAssets();
  111. m_ObjectUpdateQueue.Clear();
  112. m_ObjectUpdateQueueLookup.Clear();
  113. }
  114. // Handle objects that need re-importing
  115. int objReImportCount = m_ObjectReImportQueue.Count;
  116. for (int i = 0; i < objReImportCount; i++)
  117. {
  118. Object obj = m_ObjectReImportQueue[i];
  119. if (obj != null)
  120. {
  121. AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(obj));
  122. }
  123. }
  124. if (objReImportCount > 0)
  125. {
  126. m_ObjectReImportQueue.Clear();
  127. m_ObjectReImportQueueLookup.Clear();
  128. }
  129. }
  130. void DoPreRenderUpdates()
  131. {
  132. // Handle Font Asset Definition Refresh
  133. for (int i = 0; i < m_FontAssetDefinitionRefreshQueue.Count; i++)
  134. {
  135. TMP_FontAsset fontAsset = m_FontAssetDefinitionRefreshQueue[i];
  136. if (fontAsset != null)
  137. {
  138. fontAsset.ReadFontAssetDefinition();
  139. TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, fontAsset);
  140. }
  141. }
  142. if (m_FontAssetDefinitionRefreshQueue.Count > 0)
  143. {
  144. m_FontAssetDefinitionRefreshQueue.Clear();
  145. m_FontAssetDefinitionRefreshQueueLookup.Clear();
  146. }
  147. }
  148. }
  149. }
  150. #endif