From b7bb1124f6921ebca1b48991462637767b95e504 Mon Sep 17 00:00:00 2001 From: mob-sakai <12690315+mob-sakai@users.noreply.github.com> Date: Wed, 24 Jun 2026 18:57:55 +0900 Subject: [PATCH] fix: Support for skipping "reload domain" close #406 --- .../Internal/Extensions/CanvasExtensions.cs | 2 +- .../Extensions/ComponentExtensions.cs | 6 +-- .../PreloadedProjectSettings.cs | 46 ++++++++++++++----- .../Runtime/Internal/Utilities/FastAction.cs | 5 -- .../Runtime/Internal/Utilities/FrameCache.cs | 14 +++--- .../Internal/Utilities/UIExtraCallbacks.cs | 8 +++- Packages/src/Runtime/UIParticleUpdater.cs | 11 +++++ 7 files changed, 62 insertions(+), 30 deletions(-) diff --git a/Packages/src/Runtime/Internal/Extensions/CanvasExtensions.cs b/Packages/src/Runtime/Internal/Extensions/CanvasExtensions.cs index 5cbcb6e..bd8f261 100644 --- a/Packages/src/Runtime/Internal/Extensions/CanvasExtensions.cs +++ b/Packages/src/Runtime/Internal/Extensions/CanvasExtensions.cs @@ -98,7 +98,7 @@ namespace Coffee.UIParticleInternal Profiler.BeginSample("(COF)[CanvasExt] GetViewProjectionMatrix"); var rootCanvas = canvas.rootCanvas; var cam = rootCanvas.worldCamera; - if (rootCanvas && rootCanvas.renderMode != RenderMode.ScreenSpaceOverlay && cam) + if (rootCanvas != null && rootCanvas.renderMode != RenderMode.ScreenSpaceOverlay && cam != null) { if (eye == Camera.MonoOrStereoscopicEye.Mono) { diff --git a/Packages/src/Runtime/Internal/Extensions/ComponentExtensions.cs b/Packages/src/Runtime/Internal/Extensions/ComponentExtensions.cs index e28e353..6934eaf 100644 --- a/Packages/src/Runtime/Internal/Extensions/ComponentExtensions.cs +++ b/Packages/src/Runtime/Internal/Extensions/ComponentExtensions.cs @@ -99,7 +99,7 @@ namespace Coffee.UIParticleInternal { T component = null; var transform = self.transform; - while (transform) + while (transform != null) { if (transform.TryGetComponent(out var c)) { @@ -120,7 +120,7 @@ namespace Coffee.UIParticleInternal where T : Component { var tr = includeSelf ? self.transform : self.transform.parent; - while (tr) + while (tr != null) { if (tr.TryGetComponent(out var c) && valid(c)) return c; if (tr == stopAfter) return null; @@ -197,7 +197,7 @@ namespace Coffee.UIParticleInternal if (!includeInactive) return self.GetComponentInParent(); var current = self.transform; - while (current) + while (current != null) { if (current.TryGetComponent(out var c)) return c; current = current.parent; diff --git a/Packages/src/Runtime/Internal/ProjectSettings/PreloadedProjectSettings.cs b/Packages/src/Runtime/Internal/ProjectSettings/PreloadedProjectSettings.cs index 36e38ed..d5ef507 100644 --- a/Packages/src/Runtime/Internal/ProjectSettings/PreloadedProjectSettings.cs +++ b/Packages/src/Runtime/Internal/ProjectSettings/PreloadedProjectSettings.cs @@ -23,17 +23,14 @@ namespace Coffee.UIParticleInternal protected static bool s_BuildingPlayer; - private class Postprocessor : AssetPostprocessor + private class EditorEvents : AssetPostprocessor, IPreprocessBuildWithReport, IPostprocessBuildWithReport { + int IOrderedCallback.callbackOrder => 0; + private static void OnPostprocessAllAssets(string[] _, string[] __, string[] ___, string[] ____) { Initialize(); } - } - - private class ExcludeFromBuild : IPreprocessBuildWithReport, IPostprocessBuildWithReport - { - int IOrderedCallback.callbackOrder => 0; void IPreprocessBuildWithReport.OnPreprocessBuild(BuildReport report) { @@ -44,11 +41,11 @@ namespace Coffee.UIParticleInternal foreach (var t in TypeCache.GetTypesDerivedFrom(typeof(PreloadedProjectSettings<>))) { var settings = GetDefaultSettings(t); - if (!settings || settings.m_PreLoadSettingsInBuild) continue; + if (settings == null || settings.m_PreLoadSettingsInBuild) continue; PlayerSettings.SetPreloadedAssets( PlayerSettings.GetPreloadedAssets() - .Where(x => x && x.GetType() != t) + .Where(x => x != null && x.GetType() != t) .ToArray()); Debug.Log($"[PreloadedProjectSettings] Build started: removed '{settings.name}' " + @@ -62,6 +59,21 @@ namespace Coffee.UIParticleInternal s_BuildingPlayer = false; Initialize(); } + +#if UNITY_2019_3_OR_NEWER + [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] + private static void OnDomainReload() + { + foreach (var t in TypeCache.GetTypesDerivedFrom(typeof(PreloadedProjectSettings<>))) + { + var defaultSettings = GetDefaultSettings(t); + if (defaultSettings != null) + { + defaultSettings.OnDomainReload(); + } + } + } +#endif } private static void Initialize() @@ -154,6 +166,10 @@ namespace Coffee.UIParticleInternal protected virtual void OnInitialize() { } + + protected virtual void OnDomainReload() + { + } } internal abstract class PreloadedProjectSettingsEditor : Editor @@ -222,7 +238,7 @@ namespace Coffee.UIParticleInternal private void OnPlayModeStateChanged(PlayModeStateChange state) { - if (!this) return; + if (this == null) return; switch (state) { @@ -239,8 +255,13 @@ namespace Coffee.UIParticleInternal break; } } + + protected override void OnDomainReload() + { + s_Instance = null; + } #else - public static T instance => s_Instance != null ? s_Instance : s_Instance = CreateInstance(); + public static T instance => s_Instance != null ? s_Instance : s_Instance = CreateInstance(); #endif /// @@ -256,9 +277,10 @@ namespace Coffee.UIParticleInternal return; } + EditorApplication.playModeStateChanged -= OnPlayModeStateChanged; EditorApplication.playModeStateChanged += OnPlayModeStateChanged; #else - if (s_Instance && s_Instance != this) + if (s_Instance != null && s_Instance != this) { Destroy(s_Instance); } @@ -293,7 +315,7 @@ namespace Coffee.UIParticleInternal { if (_target == null) { - if (_editor) + if (_editor != null) { DestroyImmediate(_editor); _editor = null; diff --git a/Packages/src/Runtime/Internal/Utilities/FastAction.cs b/Packages/src/Runtime/Internal/Utilities/FastAction.cs index c5fbf50..7aadd14 100755 --- a/Packages/src/Runtime/Internal/Utilities/FastAction.cs +++ b/Packages/src/Runtime/Internal/Utilities/FastAction.cs @@ -66,11 +66,6 @@ namespace Coffee.UIParticleInternal node = node.Next; } } - - public void Clear() - { - _delegates.Clear(); - } } /// diff --git a/Packages/src/Runtime/Internal/Utilities/FrameCache.cs b/Packages/src/Runtime/Internal/Utilities/FrameCache.cs index 48186e1..ada0824 100644 --- a/Packages/src/Runtime/Internal/Utilities/FrameCache.cs +++ b/Packages/src/Runtime/Internal/Utilities/FrameCache.cs @@ -8,19 +8,19 @@ namespace Coffee.UIParticleInternal { private static readonly Dictionary s_Caches = new Dictionary(); - static FrameCache() - { - s_Caches.Clear(); - UIExtraCallbacks.onLateAfterCanvasRebuild += ClearAllCache; - } - #if UNITY_EDITOR && UNITY_2019_3_OR_NEWER [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] +#elif UNITY_EDITOR + [InitializeOnLoadMethod] +#else + [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] +#endif private static void Clear() { s_Caches.Clear(); + UIExtraCallbacks.onLateAfterCanvasRebuild -= ClearAllCache; + UIExtraCallbacks.onLateAfterCanvasRebuild += ClearAllCache; } -#endif /// /// Tries to retrieve a value from the frame cache with a specified key. diff --git a/Packages/src/Runtime/Internal/Utilities/UIExtraCallbacks.cs b/Packages/src/Runtime/Internal/Utilities/UIExtraCallbacks.cs index 758536a..038b114 100755 --- a/Packages/src/Runtime/Internal/Utilities/UIExtraCallbacks.cs +++ b/Packages/src/Runtime/Internal/Utilities/UIExtraCallbacks.cs @@ -82,14 +82,18 @@ namespace Coffee.UIParticleInternal message: "InitializeAfterCanvasRebuild"); } -#if UNITY_EDITOR +#if UNITY_EDITOR && UNITY_2019_3_OR_NEWER + [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] +#elif UNITY_EDITOR [InitializeOnLoadMethod] -#endif +#else [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] +#endif private static void InitializeOnLoad() { Canvas.willRenderCanvases -= OnAfterCanvasRebuild; s_IsInitializedAfterCanvasRebuild = false; + s_LastScreenSize = default; } /// diff --git a/Packages/src/Runtime/UIParticleUpdater.cs b/Packages/src/Runtime/UIParticleUpdater.cs index d82f485..2495c85 100644 --- a/Packages/src/Runtime/UIParticleUpdater.cs +++ b/Packages/src/Runtime/UIParticleUpdater.cs @@ -39,6 +39,17 @@ namespace Coffee.UIExtensions } #if UNITY_EDITOR +#if UNITY_2019_3_OR_NEWER + [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] + private static void OnDomainReload() + { + s_ActiveParticles.Clear(); + s_ActiveAttractors.Clear(); + s_UpdatedGroupIds.Clear(); + s_FrameCount = 0; + } +#endif + [InitializeOnLoadMethod] private static void InitializeOnLoad() {