diff --git a/Packages/src/Runtime/Internal/Utilities/Misc.cs b/Packages/src/Runtime/Internal/Utilities/Misc.cs new file mode 100644 index 00000000..59405089 --- /dev/null +++ b/Packages/src/Runtime/Internal/Utilities/Misc.cs @@ -0,0 +1,76 @@ +using System; +using System.Diagnostics; +using UnityEditor; +using UnityEngine; +using Object = UnityEngine.Object; +#if UNITY_EDITOR && UNITY_2021_2_OR_NEWER +using UnityEditor.SceneManagement; +#elif UNITY_EDITOR +using UnityEditor.Experimental.SceneManagement; +#endif + +namespace Coffee.UIEffectInternal +{ + internal static class Misc + { + public static T[] FindObjectsOfType() where T : Object + { +#if UNITY_2023_1_OR_NEWER + return Object.FindObjectsByType(FindObjectsInactive.Include, FindObjectsSortMode.None); +#else + return Object.FindObjectsOfType(); +#endif + } + + public static void Destroy(Object obj) + { + if (!obj) return; +#if UNITY_EDITOR + if (!Application.isPlaying) + { + Object.DestroyImmediate(obj); + } + else +#endif + { + Object.Destroy(obj); + } + } + + public static void DestroyImmediate(Object obj) + { + if (!obj) return; +#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 + if (!obj) return; + EditorUtility.SetDirty(obj); +#endif + } + +#if UNITY_EDITOR + public static T[] GetAllComponentsInPrefabStage() where T : Component + { + if (!PrefabStageUtility.GetCurrentPrefabStage()) return Array.Empty(); + + var prefabStage = PrefabStageUtility.GetCurrentPrefabStage(); + if (!prefabStage) return Array.Empty(); + + return prefabStage.prefabContentsRoot.GetComponentsInChildren(true); + } +#endif + } +} diff --git a/Packages/src/Runtime/Internal/Utilities/Misc.cs.meta b/Packages/src/Runtime/Internal/Utilities/Misc.cs.meta new file mode 100644 index 00000000..ce90b0f1 --- /dev/null +++ b/Packages/src/Runtime/Internal/Utilities/Misc.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b516f6b0a2e2542dab543c7ab4026245 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/src/Runtime/UIEffectBase.cs b/Packages/src/Runtime/UIEffectBase.cs index 608e677d..d9b28321 100644 --- a/Packages/src/Runtime/UIEffectBase.cs +++ b/Packages/src/Runtime/UIEffectBase.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using System.Runtime.CompilerServices; using Coffee.UIEffectInternal; using UnityEngine; @@ -245,11 +246,8 @@ private static void OnPostprocessAllAssets(string[] _, string[] __, string[] ___ if (Application.isBatchMode || BuildPipeline.isBuildingPlayer) return; s_ShaderNameCache.Clear(); -#if UNITY_2021_3 || UNITY_2022_2_OR_NEWER - foreach (var effect in FindObjectsByType(FindObjectsSortMode.None)) -#else - foreach (var effect in FindObjectsOfType()) -#endif + foreach (var effect in Misc.FindObjectsOfType() + .Concat(Misc.GetAllComponentsInPrefabStage())) { if (!effect.isActiveAndEnabled) continue; if (!(effect.graphic is TextMeshProUGUI tmp) || !tmp.isActiveAndEnabled) continue;