TMP_EditorResourceManager.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. }
  35. void OnCameraPostRender(Camera cam)
  36. {
  37. // Exclude the PreRenderCamera
  38. if (cam.cameraType == CameraType.Preview)
  39. return;
  40. DoUpdates();
  41. }
  42. /// <summary>
  43. /// Register resource for re-import.
  44. /// </summary>
  45. /// <param name="obj"></param>
  46. internal static void RegisterResourceForReimport(Object obj)
  47. {
  48. instance.InternalRegisterResourceForReimport(obj);
  49. }
  50. private void InternalRegisterResourceForReimport(Object obj)
  51. {
  52. int id = obj.GetInstanceID();
  53. if (m_ObjectReImportQueueLookup.Contains(id))
  54. return;
  55. m_ObjectReImportQueueLookup.Add(id);
  56. m_ObjectReImportQueue.Add(obj);
  57. }
  58. /// <summary>
  59. /// Register resource to be updated.
  60. /// </summary>
  61. /// <param name="textObject"></param>
  62. internal static void RegisterResourceForUpdate(Object obj)
  63. {
  64. instance.InternalRegisterResourceForUpdate(obj);
  65. }
  66. private void InternalRegisterResourceForUpdate(Object obj)
  67. {
  68. int id = obj.GetInstanceID();
  69. if (m_ObjectUpdateQueueLookup.Contains(id))
  70. return;
  71. m_ObjectUpdateQueueLookup.Add(id);
  72. m_ObjectUpdateQueue.Add(obj);
  73. }
  74. /// <summary>
  75. ///
  76. /// </summary>
  77. /// <param name="fontAsset"></param>
  78. internal static void RegisterFontAssetForDefinitionRefresh(TMP_FontAsset fontAsset)
  79. {
  80. instance.InternalRegisterFontAssetForDefinitionRefresh(fontAsset);
  81. }
  82. private void InternalRegisterFontAssetForDefinitionRefresh(TMP_FontAsset fontAsset)
  83. {
  84. int id = fontAsset.GetInstanceID();
  85. if (m_FontAssetDefinitionRefreshQueueLookup.Contains(id))
  86. return;
  87. m_FontAssetDefinitionRefreshQueueLookup.Add(id);
  88. m_FontAssetDefinitionRefreshQueue.Add(fontAsset);
  89. }
  90. void DoUpdates()
  91. {
  92. // Handle objects that need updating
  93. int objUpdateCount = m_ObjectUpdateQueue.Count;
  94. for (int i = 0; i < objUpdateCount; i++)
  95. {
  96. Object obj = m_ObjectUpdateQueue[i];
  97. if (obj != null)
  98. {
  99. EditorUtility.SetDirty(obj);
  100. }
  101. }
  102. if (objUpdateCount > 0)
  103. {
  104. //Debug.Log("Saving assets");
  105. //AssetDatabase.SaveAssets();
  106. m_ObjectUpdateQueue.Clear();
  107. m_ObjectUpdateQueueLookup.Clear();
  108. }
  109. // Handle objects that need re-importing
  110. int objReImportCount = m_ObjectReImportQueue.Count;
  111. for (int i = 0; i < objReImportCount; i++)
  112. {
  113. Object obj = m_ObjectReImportQueue[i];
  114. if (obj != null)
  115. {
  116. //Debug.Log("Re-importing [" + obj.name + "]");
  117. AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(obj));
  118. }
  119. }
  120. if (objReImportCount > 0)
  121. {
  122. m_ObjectReImportQueue.Clear();
  123. m_ObjectReImportQueueLookup.Clear();
  124. }
  125. // Handle Font Asset Definition Refresh
  126. for (int i = 0; i < m_FontAssetDefinitionRefreshQueue.Count; i++)
  127. {
  128. TMP_FontAsset fontAsset = m_FontAssetDefinitionRefreshQueue[i];
  129. if (fontAsset != null)
  130. {
  131. fontAsset.ReadFontAssetDefinition();
  132. TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, fontAsset);
  133. }
  134. }
  135. if (m_FontAssetDefinitionRefreshQueue.Count > 0)
  136. {
  137. m_FontAssetDefinitionRefreshQueue.Clear();
  138. m_FontAssetDefinitionRefreshQueueLookup.Clear();
  139. }
  140. }
  141. }
  142. }
  143. #endif