Skip to content

Commit

Permalink
fix: scene unload/reload is not detected (#394)
Browse files Browse the repository at this point in the history
Closes: #392
  • Loading branch information
bdunderscore authored Sep 15, 2024
1 parent dfa6ed3 commit 7d26771
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 47 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
56 changes: 10 additions & 46 deletions Editor/ChangeStream/ObjectWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Scene>();

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<GameObject> MonitorSceneRoots(ComputeContext ctx)
Expand Down Expand Up @@ -254,7 +218,7 @@ private static void InvokeCallback<T>(Action<T> callback, object t) where T : cl
}
catch (Exception e)
{
UnityEngine.Debug.LogException(e);
Debug.LogException(e);
}
}

Expand All @@ -267,7 +231,7 @@ private static bool InvokeCallback<T>(Func<T, bool> callback, object t) where T
}
catch (Exception e)
{
UnityEngine.Debug.LogException(e);
Debug.LogException(e);
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Editor/PreviewSystem/Rendering/NDMFPreviewSceneManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 7d26771

Please sign in to comment.