2024-12-22 12:09:07 +08:00
|
|
|
using System;
|
2024-09-29 23:17:16 +08:00
|
|
|
using System.Diagnostics;
|
|
|
|
|
using UnityEditor;
|
|
|
|
|
using UnityEngine;
|
2024-12-22 12:09:07 +08:00
|
|
|
using Object = UnityEngine.Object;
|
2025-02-21 17:00:29 +08:00
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
#if UNITY_2021_2_OR_NEWER
|
2024-12-22 12:09:07 +08:00
|
|
|
using UnityEditor.SceneManagement;
|
2025-02-21 17:00:29 +08:00
|
|
|
#else
|
2024-12-22 12:09:07 +08:00
|
|
|
using UnityEditor.Experimental.SceneManagement;
|
|
|
|
|
#endif
|
2025-02-21 17:00:29 +08:00
|
|
|
#endif
|
2024-09-29 23:17:16 +08:00
|
|
|
|
|
|
|
|
namespace Coffee.UIParticleInternal
|
|
|
|
|
{
|
|
|
|
|
internal static class Misc
|
|
|
|
|
{
|
2024-11-21 00:36:51 +08:00
|
|
|
public static T[] FindObjectsOfType<T>() where T : Object
|
|
|
|
|
{
|
|
|
|
|
#if UNITY_2023_1_OR_NEWER
|
|
|
|
|
return Object.FindObjectsByType<T>(FindObjectsInactive.Include, FindObjectsSortMode.None);
|
|
|
|
|
#else
|
|
|
|
|
return Object.FindObjectsOfType<T>();
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-29 23:17:16 +08:00
|
|
|
public static void Destroy(Object obj)
|
|
|
|
|
{
|
2026-03-24 16:32:31 +08:00
|
|
|
if (obj == null) return;
|
2024-09-29 23:17:16 +08:00
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
if (!Application.isPlaying)
|
|
|
|
|
{
|
|
|
|
|
Object.DestroyImmediate(obj);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
Object.Destroy(obj);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void DestroyImmediate(Object obj)
|
|
|
|
|
{
|
2026-03-24 16:32:31 +08:00
|
|
|
if (obj == null) return;
|
2024-09-29 23:17:16 +08:00
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
if (Application.isEditor)
|
|
|
|
|
{
|
|
|
|
|
Object.DestroyImmediate(obj);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
Object.Destroy(obj);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Conditional("UNITY_EDITOR")]
|
|
|
|
|
public static void SetDirty(Object obj)
|
|
|
|
|
{
|
|
|
|
|
#if UNITY_EDITOR
|
2026-03-24 16:32:31 +08:00
|
|
|
if (obj == null) return;
|
2024-09-29 23:17:16 +08:00
|
|
|
EditorUtility.SetDirty(obj);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2024-12-22 12:09:07 +08:00
|
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
public static T[] GetAllComponentsInPrefabStage<T>() where T : Component
|
|
|
|
|
{
|
|
|
|
|
var prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
|
|
|
|
|
if (prefabStage == null) return Array.Empty<T>();
|
|
|
|
|
|
|
|
|
|
return prefabStage.prefabContentsRoot.GetComponentsInChildren<T>(true);
|
|
|
|
|
}
|
2025-01-03 21:58:18 +08:00
|
|
|
|
|
|
|
|
public static bool isBatchOrBuilding => Application.isBatchMode || BuildPipeline.isBuildingPlayer;
|
2024-12-22 12:09:07 +08:00
|
|
|
#endif
|
2025-02-21 17:00:29 +08:00
|
|
|
|
|
|
|
|
[Conditional("UNITY_EDITOR")]
|
|
|
|
|
public static void QueuePlayerLoopUpdate()
|
|
|
|
|
{
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
if (!EditorApplication.isPlaying)
|
|
|
|
|
{
|
|
|
|
|
EditorApplication.QueuePlayerLoopUpdate();
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2024-09-29 23:17:16 +08:00
|
|
|
}
|
2025-02-21 17:00:29 +08:00
|
|
|
|
|
|
|
|
#if !UNITY_2021_2_OR_NEWER
|
|
|
|
|
[AttributeUsage(AttributeTargets.Class)]
|
|
|
|
|
[Conditional("UNITY_EDITOR")]
|
|
|
|
|
internal class IconAttribute : Attribute
|
|
|
|
|
{
|
|
|
|
|
private readonly string _path;
|
|
|
|
|
|
|
|
|
|
public IconAttribute(string path)
|
|
|
|
|
{
|
|
|
|
|
_path = path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
private static Action<Object, Texture2D> s_SetIconForObject = typeof(EditorGUIUtility)
|
|
|
|
|
.GetMethod("SetIconForObject", BindingFlags.Static | BindingFlags.NonPublic)
|
|
|
|
|
.CreateDelegate(typeof(Action<Object, Texture2D>), null) as Action<Object, Texture2D>;
|
|
|
|
|
|
|
|
|
|
[InitializeOnLoadMethod]
|
|
|
|
|
private static void InitializeOnLoadMethod()
|
|
|
|
|
{
|
|
|
|
|
if (Misc.isBatchOrBuilding) return;
|
|
|
|
|
|
|
|
|
|
var types = TypeCache.GetTypesWithAttribute<IconAttribute>();
|
|
|
|
|
var scripts = MonoImporter.GetAllRuntimeMonoScripts();
|
|
|
|
|
foreach (var type in types)
|
|
|
|
|
{
|
|
|
|
|
var script = scripts.FirstOrDefault(x => x.GetClass() == type);
|
2026-03-24 16:32:31 +08:00
|
|
|
if (script == null) continue;
|
2025-02-21 17:00:29 +08:00
|
|
|
|
|
|
|
|
var path = type.GetCustomAttribute<IconAttribute>()?._path;
|
|
|
|
|
var icon = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
|
2026-03-24 16:32:31 +08:00
|
|
|
if (icon == null) continue;
|
2025-02-21 17:00:29 +08:00
|
|
|
|
|
|
|
|
s_SetIconForObject(script, icon);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2024-09-29 23:17:16 +08:00
|
|
|
}
|