Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit 79f2514

Browse files
committed
Fix issue with partially unloaded scenes(?)
1 parent 4e76eca commit 79f2514

File tree

2 files changed

+20
-27
lines changed

2 files changed

+20
-27
lines changed

src/ObjectExplorer/SceneExplorer.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public SceneExplorer(ObjectExplorerPanel parent)
2727
Parent = parent;
2828

2929
SceneHandler.OnInspectedSceneChanged += SceneHandler_OnInspectedSceneChanged;
30-
SceneHandler.OnLoadedScenesChanged += SceneHandler_OnLoadedScenesChanged;
30+
SceneHandler.OnLoadedScenesUpdated += SceneHandler_OnLoadedScenesUpdated;
3131
}
3232

3333
public override GameObject UIRoot => uiRoot;
@@ -87,7 +87,7 @@ public void JumpToTransform(Transform transform)
8787
Tree.JumpAndExpandToTransform(transform);
8888
}
8989

90-
private void OnDropdownChanged(int value)
90+
private void OnSceneSelectionDropdownChanged(int value)
9191
{
9292
if (value < 0 || SceneHandler.LoadedScenes.Count <= value)
9393
return;
@@ -101,7 +101,7 @@ private void OnDropdownChanged(int value)
101101
private void SceneHandler_OnInspectedSceneChanged(Scene scene)
102102
{
103103
if (!sceneToDropdownOption.ContainsKey(scene))
104-
PopulateSceneDropdown();
104+
PopulateSceneDropdown(SceneHandler.LoadedScenes);
105105

106106
if (sceneToDropdownOption.ContainsKey(scene))
107107
{
@@ -122,17 +122,17 @@ private void OnSelectedSceneChanged(Scene scene)
122122
refreshRow.SetActive(!scene.IsValid());
123123
}
124124

125-
private void SceneHandler_OnLoadedScenesChanged(List<Scene> loadedScenes)
125+
private void SceneHandler_OnLoadedScenesUpdated(List<Scene> loadedScenes)
126126
{
127-
PopulateSceneDropdown();
127+
PopulateSceneDropdown(loadedScenes);
128128
}
129129

130-
private void PopulateSceneDropdown()
130+
private void PopulateSceneDropdown(List<Scene> loadedScenes)
131131
{
132132
sceneToDropdownOption.Clear();
133133
sceneDropdown.options.Clear();
134134

135-
foreach (var scene in SceneHandler.LoadedScenes)
135+
foreach (var scene in loadedScenes)
136136
{
137137
if (sceneToDropdownOption.ContainsKey(scene))
138138
continue;
@@ -198,11 +198,11 @@ public override void ConstructUI(GameObject content)
198198
var dropLabel = UIFactory.CreateLabel(dropRow, "SelectorLabel", "Scene:", TextAnchor.MiddleLeft, Color.cyan, false, 15);
199199
UIFactory.SetLayoutElement(dropLabel.gameObject, minHeight: 25, minWidth: 60, flexibleWidth: 0);
200200

201-
var dropdownObj = UIFactory.CreateDropdown(dropRow, "SceneDropdown", out sceneDropdown, "<notset>", 13, OnDropdownChanged);
201+
var dropdownObj = UIFactory.CreateDropdown(dropRow, "SceneDropdown", out sceneDropdown, "<notset>", 13, OnSceneSelectionDropdownChanged);
202202
UIFactory.SetLayoutElement(dropdownObj, minHeight: 25, flexibleHeight: 0, flexibleWidth: 9999);
203203

204204
SceneHandler.Update();
205-
PopulateSceneDropdown();
205+
PopulateSceneDropdown(SceneHandler.LoadedScenes);
206206
sceneDropdown.captionText.text = sceneToDropdownOption.First().Value.text;
207207

208208
// Filter row

src/ObjectExplorer/SceneHandler.cs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ public static Scene? SelectedScene
1717
get => selectedScene;
1818
internal set
1919
{
20-
if (selectedScene != null && selectedScene == value)
20+
if (selectedScene.HasValue && selectedScene == value)
2121
return;
2222
selectedScene = value;
2323
OnInspectedSceneChanged?.Invoke((Scene)selectedScene);
24+
ExplorerCore.Log($"Set selected scene to {value?.name}");
2425
}
2526
}
2627
private static Scene? selectedScene;
@@ -30,7 +31,7 @@ internal set
3031

3132
/// <summary>All currently loaded Scenes.</summary>
3233
public static List<Scene> LoadedScenes { get; private set; } = new();
33-
private static HashSet<Scene> previousLoadedScenes;
34+
//private static HashSet<Scene> previousLoadedScenes;
3435

3536
/// <summary>The names of all scenes in the build settings, if they could be retrieved.</summary>
3637
public static List<string> AllSceneNames { get; private set; } = new();
@@ -39,7 +40,7 @@ internal set
3940
public static event Action<Scene> OnInspectedSceneChanged;
4041

4142
/// <summary>Invoked whenever the list of currently loaded Scenes changes. The argument contains all loaded scenes after the change.</summary>
42-
public static event Action<List<Scene>> OnLoadedScenesChanged;
43+
public static event Action<List<Scene>> OnLoadedScenesUpdated;
4344

4445
/// <summary>Generally will be 2, unless DontDestroyExists == false, then this will be 1.</summary>
4546
internal static int DefaultSceneCount => 1 + (DontDestroyExists ? 1 : 0);
@@ -84,23 +85,20 @@ internal static void Init()
8485

8586
internal static void Update()
8687
{
87-
// check if the loaded scenes changed. always confirm DontDestroy / HideAndDontSave
88-
int confirmedCount = DefaultSceneCount;
89-
bool inspectedExists = (SelectedScene.HasValue && SelectedScene.Value.handle == -12)
90-
|| (SelectedScene.HasValue && SelectedScene.Value.handle == -1);
88+
// Inspected scene will exist if it's DontDestroyOnLoad or HideAndDontSave
89+
bool inspectedExists =
90+
SelectedScene.HasValue
91+
&& ((DontDestroyExists && SelectedScene.Value.handle == -12)
92+
|| SelectedScene.Value.handle == -1);
9193

9294
LoadedScenes.Clear();
9395

9496
for (int i = 0; i < SceneManager.sceneCount; i++)
9597
{
9698
Scene scene = SceneManager.GetSceneAt(i);
97-
if (scene == default || !scene.isLoaded)
99+
if (scene == default || !scene.isLoaded || !scene.IsValid())
98100
continue;
99101

100-
// If no changes yet, ensure the previous list contained the scene
101-
if (previousLoadedScenes != null && previousLoadedScenes.Contains(scene))
102-
confirmedCount++;
103-
104102
// If we have not yet confirmed inspectedExists, check if this scene is our currently inspected one.
105103
if (!inspectedExists && scene == SelectedScene)
106104
inspectedExists = true;
@@ -112,17 +110,12 @@ internal static void Update()
112110
LoadedScenes.Add(new Scene { m_Handle = -12 });
113111
LoadedScenes.Add(new Scene { m_Handle = -1 });
114112

115-
bool anyChange = confirmedCount != LoadedScenes.Count;
116-
117-
previousLoadedScenes = new HashSet<Scene>(LoadedScenes);
118-
119113
// Default to first scene if none selected or previous selection no longer exists.
120114
if (!inspectedExists)
121115
SelectedScene = LoadedScenes.First();
122116

123117
// Notify on the list changing at all
124-
if (anyChange)
125-
OnLoadedScenesChanged?.Invoke(LoadedScenes);
118+
OnLoadedScenesUpdated?.Invoke(LoadedScenes);
126119

127120
// Finally, update the root objects list.
128121
if (SelectedScene != null && ((Scene)SelectedScene).IsValid())

0 commit comments

Comments
 (0)