From 7d26771a064aca1962ebca95e9648f0239e5339e Mon Sep 17 00:00:00 2001 From: bd_ Date: Sat, 14 Sep 2024 21:47:17 -0400 Subject: [PATCH] fix: scene unload/reload is not detected (#394) Closes: #392 --- CHANGELOG.md | 1 + Editor/ChangeStream/ObjectWatcher.cs | 56 ++++--------------- .../Rendering/NDMFPreviewSceneManager.cs | 2 +- 3 files changed, 12 insertions(+), 47 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c11e8929..a8d12f6d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 caused by non-ASCII project paths. - [#388] Improve handling for renderers which are destroyed during preview pipeline construction - [#390] Suppress IRenderFilter with unsupported renderers +- [#394] Scenes which are unloaded and reloaded do not trigger preview processing ### Changed diff --git a/Editor/ChangeStream/ObjectWatcher.cs b/Editor/ChangeStream/ObjectWatcher.cs index f9326a98..159e8321 100644 --- a/Editor/ChangeStream/ObjectWatcher.cs +++ b/Editor/ChangeStream/ObjectWatcher.cs @@ -7,9 +7,9 @@ using System.Threading; using nadena.dev.ndmf.preview; using UnityEditor; +using UnityEditor.SceneManagement; using UnityEngine; using UnityEngine.SceneManagement; -using Debug = System.Diagnostics.Debug; using Object = UnityEngine.Object; #endregion @@ -76,54 +76,18 @@ private static void Init() { EditorApplication.delayCall += () => { - SceneManager.sceneLoaded += (_, _) => + EditorSceneManager.sceneOpened += (scene, _) => { - Debug.WriteLine("=== Scene loaded ==="); + if (scene.name == NDMFPreviewSceneManager.PreviewSceneName) return; Instance.Hierarchy.InvalidateAll(); }; - SceneManager.sceneUnloaded += _ => - { - Debug.WriteLine("=== Scene unloaded ==="); - Instance.Hierarchy.InvalidateAll(); - }; - SceneManager.activeSceneChanged += (_, _) => - { - Debug.WriteLine("=== Active scene changed ==="); - Instance.Hierarchy.InvalidateAll(); - }; - Instance.PropertyMonitor.MaybeStartRefreshTimer(); - - // These SceneManager callbacks are never invoked, for some reason. Workaround this with a periodic check. - EditorApplication.update += Instance.CheckActiveScenes; - }; - } - - private Scene[] _activeScenes = Array.Empty(); - - private void CheckActiveScenes() - { - if (SceneManager.sceneCount != _activeScenes.Length) - { - InvalidateScenes(); - - return; - } - - for (var i = 0; i < _activeScenes.Length; i++) - if (_activeScenes[i] != SceneManager.GetSceneAt(i)) - { - InvalidateScenes(); - return; - } - - void InvalidateScenes() - { - _activeScenes = new Scene[SceneManager.sceneCount]; - for (var i = 0; i < _activeScenes.Length; i++) _activeScenes[i] = SceneManager.GetSceneAt(i); + EditorSceneManager.sceneClosed += _ => Instance.Hierarchy.InvalidateAll(); - Hierarchy.InvalidateAll(); - } + EditorSceneManager.newSceneCreated += (_, _, _) => { Instance.Hierarchy.InvalidateAll(); }; + + Instance.PropertyMonitor.MaybeStartRefreshTimer(); + }; } public ImmutableList MonitorSceneRoots(ComputeContext ctx) @@ -254,7 +218,7 @@ private static void InvokeCallback(Action callback, object t) where T : cl } catch (Exception e) { - UnityEngine.Debug.LogException(e); + Debug.LogException(e); } } @@ -267,7 +231,7 @@ private static bool InvokeCallback(Func callback, object t) where T } catch (Exception e) { - UnityEngine.Debug.LogException(e); + Debug.LogException(e); return true; } } diff --git a/Editor/PreviewSystem/Rendering/NDMFPreviewSceneManager.cs b/Editor/PreviewSystem/Rendering/NDMFPreviewSceneManager.cs index 98b0d990..8e47de3a 100644 --- a/Editor/PreviewSystem/Rendering/NDMFPreviewSceneManager.cs +++ b/Editor/PreviewSystem/Rendering/NDMFPreviewSceneManager.cs @@ -21,7 +21,7 @@ private static string[] OnWillSaveAssets(string[] paths) public static class NDMFPreviewSceneManager { - private static string PreviewSceneName = "___NDMF Preview___"; + internal static string PreviewSceneName = "___NDMF Preview___"; private const string PreviewSceneGuid = "8cbd3f19cef3477439841053ced0661b"; private static Scene _previewScene;