TMP_EditorResourceManager.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 Dictionary<int, int> m_ObjectUpdateQueueLookup = new Dictionary<int, int>();
  12. private readonly List<Object> m_ObjectReImportQueue = new List<Object>();
  13. private Dictionary<int, int> m_ObjectReImportQueueLookup = new Dictionary<int, int>();
  14. /// <summary>
  15. /// Get a singleton instance of the manager.
  16. /// </summary>
  17. public static TMP_EditorResourceManager instance
  18. {
  19. get
  20. {
  21. if (s_Instance == null)
  22. s_Instance = new TMP_EditorResourceManager();
  23. return s_Instance;
  24. }
  25. }
  26. /// <summary>
  27. /// Register to receive rendering callbacks.
  28. /// </summary>
  29. private TMP_EditorResourceManager()
  30. {
  31. Camera.onPostRender += OnCameraPostRender;
  32. }
  33. void OnCameraPostRender(Camera cam)
  34. {
  35. // Exclude the PreRenderCamera
  36. if (cam.cameraType == CameraType.Preview)
  37. return;
  38. DoUpdates();
  39. }
  40. /// <summary>
  41. /// Register resource for re-import.
  42. /// </summary>
  43. /// <param name="obj"></param>
  44. internal static void RegisterResourceForReimport(Object obj)
  45. {
  46. instance.InternalRegisterResourceForReimport(obj);
  47. }
  48. private void InternalRegisterResourceForReimport(Object obj)
  49. {
  50. int id = obj.GetInstanceID();
  51. if (m_ObjectReImportQueueLookup.ContainsKey(id))
  52. return;
  53. m_ObjectReImportQueueLookup[id] = id;
  54. m_ObjectReImportQueue.Add(obj);
  55. return;
  56. }
  57. /// <summary>
  58. /// Register resource to be updated.
  59. /// </summary>
  60. /// <param name="textObject"></param>
  61. internal static void RegisterResourceForUpdate(Object obj)
  62. {
  63. instance.InternalRegisterResourceForUpdate(obj);
  64. }
  65. private void InternalRegisterResourceForUpdate(Object obj)
  66. {
  67. int id = obj.GetInstanceID();
  68. if (m_ObjectUpdateQueueLookup.ContainsKey(id))
  69. return;
  70. m_ObjectUpdateQueueLookup[id] = id;
  71. m_ObjectUpdateQueue.Add(obj);
  72. return;
  73. }
  74. void DoUpdates()
  75. {
  76. // Handle objects that need updating
  77. int objUpdateCount = m_ObjectUpdateQueue.Count;
  78. for (int i = 0; i < objUpdateCount; i++)
  79. {
  80. Object obj = m_ObjectUpdateQueue[i];
  81. if (obj != null)
  82. {
  83. EditorUtility.SetDirty(obj);
  84. }
  85. }
  86. if (objUpdateCount > 0)
  87. {
  88. //Debug.Log("Saving assets");
  89. //AssetDatabase.SaveAssets();
  90. m_ObjectUpdateQueue.Clear();
  91. m_ObjectUpdateQueueLookup.Clear();
  92. }
  93. // Handle objects that need re-importing
  94. int objReImportCount = m_ObjectReImportQueue.Count;
  95. for (int i = 0; i < objReImportCount; i++)
  96. {
  97. Object obj = m_ObjectReImportQueue[i];
  98. if (obj != null)
  99. {
  100. //Debug.Log("Re-importing [" + obj.name + "]");
  101. AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(obj));
  102. }
  103. }
  104. if (objReImportCount > 0)
  105. {
  106. m_ObjectReImportQueue.Clear();
  107. m_ObjectReImportQueueLookup.Clear();
  108. }
  109. }
  110. }
  111. }
  112. #endif