Misc.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Diagnostics;
  3. using UnityEditor;
  4. using UnityEngine;
  5. using Object = UnityEngine.Object;
  6. #if UNITY_EDITOR
  7. using System.IO;
  8. using System.Linq;
  9. using System.Reflection;
  10. #if UNITY_2021_2_OR_NEWER
  11. using UnityEditor.SceneManagement;
  12. #else
  13. using UnityEditor.Experimental.SceneManagement;
  14. #endif
  15. #endif
  16. namespace Coffee.UIParticleInternal
  17. {
  18. internal static class Misc
  19. {
  20. public static T[] FindObjectsOfType<T>() where T : Object
  21. {
  22. #if UNITY_2023_1_OR_NEWER
  23. return Object.FindObjectsByType<T>(FindObjectsInactive.Include, FindObjectsSortMode.None);
  24. #else
  25. return Object.FindObjectsOfType<T>();
  26. #endif
  27. }
  28. public static void Destroy(Object obj)
  29. {
  30. if (!obj) return;
  31. #if UNITY_EDITOR
  32. if (!Application.isPlaying)
  33. {
  34. Object.DestroyImmediate(obj);
  35. }
  36. else
  37. #endif
  38. {
  39. Object.Destroy(obj);
  40. }
  41. }
  42. public static void DestroyImmediate(Object obj)
  43. {
  44. if (!obj) return;
  45. #if UNITY_EDITOR
  46. if (Application.isEditor)
  47. {
  48. Object.DestroyImmediate(obj);
  49. }
  50. else
  51. #endif
  52. {
  53. Object.Destroy(obj);
  54. }
  55. }
  56. [Conditional("UNITY_EDITOR")]
  57. public static void SetDirty(Object obj)
  58. {
  59. #if UNITY_EDITOR
  60. if (!obj) return;
  61. EditorUtility.SetDirty(obj);
  62. #endif
  63. }
  64. #if UNITY_EDITOR
  65. public static T[] GetAllComponentsInPrefabStage<T>() where T : Component
  66. {
  67. var prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
  68. if (prefabStage == null) return Array.Empty<T>();
  69. return prefabStage.prefabContentsRoot.GetComponentsInChildren<T>(true);
  70. }
  71. public static bool isBatchOrBuilding => Application.isBatchMode || BuildPipeline.isBuildingPlayer;
  72. #endif
  73. [Conditional("UNITY_EDITOR")]
  74. public static void QueuePlayerLoopUpdate()
  75. {
  76. #if UNITY_EDITOR
  77. if (!EditorApplication.isPlaying)
  78. {
  79. EditorApplication.QueuePlayerLoopUpdate();
  80. }
  81. #endif
  82. }
  83. }
  84. #if !UNITY_2021_2_OR_NEWER
  85. [AttributeUsage(AttributeTargets.Class)]
  86. [Conditional("UNITY_EDITOR")]
  87. internal class IconAttribute : Attribute
  88. {
  89. private readonly string _path;
  90. public IconAttribute(string path)
  91. {
  92. _path = path;
  93. }
  94. #if UNITY_EDITOR
  95. private static Action<Object, Texture2D> s_SetIconForObject = typeof(EditorGUIUtility)
  96. .GetMethod("SetIconForObject", BindingFlags.Static | BindingFlags.NonPublic)
  97. .CreateDelegate(typeof(Action<Object, Texture2D>), null) as Action<Object, Texture2D>;
  98. [InitializeOnLoadMethod]
  99. private static void InitializeOnLoadMethod()
  100. {
  101. if (Misc.isBatchOrBuilding) return;
  102. var types = TypeCache.GetTypesWithAttribute<IconAttribute>();
  103. var scripts = MonoImporter.GetAllRuntimeMonoScripts();
  104. foreach (var type in types)
  105. {
  106. var script = scripts.FirstOrDefault(x => x.GetClass() == type);
  107. if (!script) continue;
  108. var path = type.GetCustomAttribute<IconAttribute>()?._path;
  109. var icon = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
  110. if (!icon) continue;
  111. s_SetIconForObject(script, icon);
  112. }
  113. }
  114. #endif
  115. }
  116. #endif
  117. }