diff --git a/src/ObjectExplorer/SceneExplorer.cs b/src/ObjectExplorer/SceneExplorer.cs index 3f0a232..2e6f1a1 100644 --- a/src/ObjectExplorer/SceneExplorer.cs +++ b/src/ObjectExplorer/SceneExplorer.cs @@ -27,7 +27,7 @@ namespace UnityExplorer.ObjectExplorer Parent = parent; SceneHandler.OnInspectedSceneChanged += SceneHandler_OnInspectedSceneChanged; - SceneHandler.OnLoadedScenesChanged += SceneHandler_OnLoadedScenesChanged; + SceneHandler.OnLoadedScenesUpdated += SceneHandler_OnLoadedScenesUpdated; } public override GameObject UIRoot => uiRoot; @@ -87,7 +87,7 @@ namespace UnityExplorer.ObjectExplorer Tree.JumpAndExpandToTransform(transform); } - private void OnDropdownChanged(int value) + private void OnSceneSelectionDropdownChanged(int value) { if (value < 0 || SceneHandler.LoadedScenes.Count <= value) return; @@ -101,7 +101,7 @@ namespace UnityExplorer.ObjectExplorer private void SceneHandler_OnInspectedSceneChanged(Scene scene) { if (!sceneToDropdownOption.ContainsKey(scene)) - PopulateSceneDropdown(); + PopulateSceneDropdown(SceneHandler.LoadedScenes); if (sceneToDropdownOption.ContainsKey(scene)) { @@ -122,17 +122,17 @@ namespace UnityExplorer.ObjectExplorer refreshRow.SetActive(!scene.IsValid()); } - private void SceneHandler_OnLoadedScenesChanged(List loadedScenes) + private void SceneHandler_OnLoadedScenesUpdated(List loadedScenes) { - PopulateSceneDropdown(); + PopulateSceneDropdown(loadedScenes); } - private void PopulateSceneDropdown() + private void PopulateSceneDropdown(List loadedScenes) { sceneToDropdownOption.Clear(); sceneDropdown.options.Clear(); - foreach (var scene in SceneHandler.LoadedScenes) + foreach (var scene in loadedScenes) { if (sceneToDropdownOption.ContainsKey(scene)) continue; @@ -198,11 +198,11 @@ namespace UnityExplorer.ObjectExplorer var dropLabel = UIFactory.CreateLabel(dropRow, "SelectorLabel", "Scene:", TextAnchor.MiddleLeft, Color.cyan, false, 15); UIFactory.SetLayoutElement(dropLabel.gameObject, minHeight: 25, minWidth: 60, flexibleWidth: 0); - var dropdownObj = UIFactory.CreateDropdown(dropRow, "SceneDropdown", out sceneDropdown, "", 13, OnDropdownChanged); + var dropdownObj = UIFactory.CreateDropdown(dropRow, "SceneDropdown", out sceneDropdown, "", 13, OnSceneSelectionDropdownChanged); UIFactory.SetLayoutElement(dropdownObj, minHeight: 25, flexibleHeight: 0, flexibleWidth: 9999); SceneHandler.Update(); - PopulateSceneDropdown(); + PopulateSceneDropdown(SceneHandler.LoadedScenes); sceneDropdown.captionText.text = sceneToDropdownOption.First().Value.text; // Filter row diff --git a/src/ObjectExplorer/SceneHandler.cs b/src/ObjectExplorer/SceneHandler.cs index 0ff4fe5..afa5a43 100644 --- a/src/ObjectExplorer/SceneHandler.cs +++ b/src/ObjectExplorer/SceneHandler.cs @@ -17,10 +17,11 @@ namespace UnityExplorer.ObjectExplorer get => selectedScene; internal set { - if (selectedScene != null && selectedScene == value) + if (selectedScene.HasValue && selectedScene == value) return; selectedScene = value; OnInspectedSceneChanged?.Invoke((Scene)selectedScene); + ExplorerCore.Log($"Set selected scene to {value?.name}"); } } private static Scene? selectedScene; @@ -30,7 +31,7 @@ namespace UnityExplorer.ObjectExplorer /// All currently loaded Scenes. public static List LoadedScenes { get; private set; } = new(); - private static HashSet previousLoadedScenes; + //private static HashSet previousLoadedScenes; /// The names of all scenes in the build settings, if they could be retrieved. public static List AllSceneNames { get; private set; } = new(); @@ -39,7 +40,7 @@ namespace UnityExplorer.ObjectExplorer public static event Action OnInspectedSceneChanged; /// Invoked whenever the list of currently loaded Scenes changes. The argument contains all loaded scenes after the change. - public static event Action> OnLoadedScenesChanged; + public static event Action> OnLoadedScenesUpdated; /// Generally will be 2, unless DontDestroyExists == false, then this will be 1. internal static int DefaultSceneCount => 1 + (DontDestroyExists ? 1 : 0); @@ -84,23 +85,20 @@ namespace UnityExplorer.ObjectExplorer internal static void Update() { - // check if the loaded scenes changed. always confirm DontDestroy / HideAndDontSave - int confirmedCount = DefaultSceneCount; - bool inspectedExists = (SelectedScene.HasValue && SelectedScene.Value.handle == -12) - || (SelectedScene.HasValue && SelectedScene.Value.handle == -1); + // Inspected scene will exist if it's DontDestroyOnLoad or HideAndDontSave + bool inspectedExists = + SelectedScene.HasValue + && ((DontDestroyExists && SelectedScene.Value.handle == -12) + || SelectedScene.Value.handle == -1); LoadedScenes.Clear(); for (int i = 0; i < SceneManager.sceneCount; i++) { Scene scene = SceneManager.GetSceneAt(i); - if (scene == default || !scene.isLoaded) + if (scene == default || !scene.isLoaded || !scene.IsValid()) continue; - // If no changes yet, ensure the previous list contained the scene - if (previousLoadedScenes != null && previousLoadedScenes.Contains(scene)) - confirmedCount++; - // If we have not yet confirmed inspectedExists, check if this scene is our currently inspected one. if (!inspectedExists && scene == SelectedScene) inspectedExists = true; @@ -112,17 +110,12 @@ namespace UnityExplorer.ObjectExplorer LoadedScenes.Add(new Scene { m_Handle = -12 }); LoadedScenes.Add(new Scene { m_Handle = -1 }); - bool anyChange = confirmedCount != LoadedScenes.Count; - - previousLoadedScenes = new HashSet(LoadedScenes); - // Default to first scene if none selected or previous selection no longer exists. if (!inspectedExists) SelectedScene = LoadedScenes.First(); // Notify on the list changing at all - if (anyChange) - OnLoadedScenesChanged?.Invoke(LoadedScenes); + OnLoadedScenesUpdated?.Invoke(LoadedScenes); // Finally, update the root objects list. if (SelectedScene != null && ((Scene)SelectedScene).IsValid())