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

Commit 1410578

Browse files
committed
Fix SceneExplorer not properly detecting scene changes sometimes
1 parent 365269b commit 1410578

File tree

2 files changed

+22
-27
lines changed

2 files changed

+22
-27
lines changed

src/UI/ObjectExplorer/SceneExplorer.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public SceneExplorer(ObjectExplorerPanel parent)
3939

4040
private GameObject refreshRow;
4141
private Dropdown sceneDropdown;
42-
private readonly Dictionary<int, Dropdown.OptionData> sceneToDropdownOption = new Dictionary<int, Dropdown.OptionData>();
42+
private readonly Dictionary<Scene, Dropdown.OptionData> sceneToDropdownOption = new Dictionary<Scene, Dropdown.OptionData>();
4343

4444
private IEnumerable<GameObject> GetRootEntries() => SceneHandler.CurrentRootObjects;
4545

@@ -70,7 +70,7 @@ public void JumpToTransform(Transform transform)
7070
var go = transform.gameObject;
7171
if (SceneHandler.SelectedScene != go.scene)
7272
{
73-
int idx = sceneDropdown.options.IndexOf(sceneToDropdownOption[go.scene.handle]);
73+
int idx = sceneDropdown.options.IndexOf(sceneToDropdownOption[go.scene]);
7474
sceneDropdown.value = idx;
7575
}
7676

@@ -91,12 +91,12 @@ private void OnDropdownChanged(int value)
9191

9292
private void SceneHandler_OnInspectedSceneChanged(Scene scene)
9393
{
94-
if (!sceneToDropdownOption.ContainsKey(scene.handle))
94+
if (!sceneToDropdownOption.ContainsKey(scene))
9595
PopulateSceneDropdown();
9696

97-
if (sceneToDropdownOption.ContainsKey(scene.handle))
97+
if (sceneToDropdownOption.ContainsKey(scene))
9898
{
99-
var opt = sceneToDropdownOption[scene.handle];
99+
var opt = sceneToDropdownOption[scene];
100100
int idx = sceneDropdown.options.IndexOf(opt);
101101
if (sceneDropdown.value != idx)
102102
sceneDropdown.value = idx;
@@ -134,7 +134,7 @@ private void PopulateSceneDropdown()
134134

135135
var option = new Dropdown.OptionData(name);
136136
sceneDropdown.options.Add(option);
137-
sceneToDropdownOption.Add(scene.handle, option);
137+
sceneToDropdownOption.Add(scene, option);
138138
}
139139
}
140140

src/UI/ObjectExplorer/SceneHandler.cs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static Scene? SelectedScene
1818
get => m_selectedScene;
1919
internal set
2020
{
21-
if (m_selectedScene != null && m_selectedScene?.handle == value?.handle)
21+
if (m_selectedScene != null && m_selectedScene == value)
2222
return;
2323
m_selectedScene = value;
2424
OnInspectedSceneChanged?.Invoke((Scene)m_selectedScene);
@@ -37,6 +37,7 @@ internal set
3737
/// </summary>
3838
public static ReadOnlyCollection<Scene> LoadedScenes => new ReadOnlyCollection<Scene>(allLoadedScenes);
3939
private static readonly List<Scene> allLoadedScenes = new List<Scene>();
40+
private static HashSet<Scene> previousLoadedScenes;
4041

4142
/// <summary>
4243
/// The names of all scenes in the build settings, if they could be retrieved.
@@ -82,7 +83,7 @@ internal static GameObject DontDestroyMe
8283
}
8384
private static GameObject dontDestroyObject;
8485

85-
public static bool InspectingAssetScene => !SelectedScene?.IsValid() ?? false;
86+
public static bool InspectingAssetScene => SelectedScene.HasValue && SelectedScene.Value == default;
8687

8788
internal static void Init()
8889
{
@@ -110,17 +111,9 @@ internal static void Init()
110111

111112
internal static void Update()
112113
{
113-
int curHandle = SelectedScene?.handle ?? -1;
114-
// DontDestroyOnLoad always exists, so default to true if our curHandle is that handle.
115-
// otherwise we will check while iterating.
116-
bool inspectedExists = curHandle == DontDestroyHandle || curHandle == 0;
117-
118-
// Quick sanity check if the loaded scenes changed
119-
bool anyChange = LoadedSceneCount != allLoadedScenes.Count;
120-
// otherwise keep a lookup table of the previous handles to check if the list changed at all.
121-
HashSet<int> previousHandles = null;
122-
if (!anyChange)
123-
previousHandles = new HashSet<int>(allLoadedScenes.Select(it => it.handle));
114+
// check if the loaded scenes changed. always confirm DontDestroy / HideAndDontSave
115+
int confirmedCount = 2;
116+
bool inspectedExists = SelectedScene == DontDestroyScene || (SelectedScene.HasValue && SelectedScene.Value == default);
124117

125118
allLoadedScenes.Clear();
126119

@@ -130,20 +123,22 @@ internal static void Update()
130123
if (scene == default || !scene.isLoaded)
131124
continue;
132125

133-
// If no changes yet, ensure the previous list contained this handle.
134-
if (!anyChange && !previousHandles.Contains(scene.handle))
135-
anyChange = true;
126+
// If no changes yet, ensure the previous list contained the scene
127+
if (previousLoadedScenes != null && previousLoadedScenes.Contains(scene))
128+
confirmedCount++;
136129

137130
// If we have not yet confirmed inspectedExists, check if this scene is our currently inspected one.
138-
if (curHandle != -1 && !inspectedExists && scene.handle == curHandle)
131+
if (!inspectedExists && scene == SelectedScene)
139132
inspectedExists = true;
140133

141134
allLoadedScenes.Add(scene);
142135
}
143136

144-
// Always add the DontDestroyOnLoad scene and the "none" scene.
137+
bool anyChange = confirmedCount != allLoadedScenes.Count;
138+
145139
allLoadedScenes.Add(DontDestroyScene);
146140
allLoadedScenes.Add(default);
141+
previousLoadedScenes = new HashSet<Scene>(allLoadedScenes);
147142

148143
// Default to first scene if none selected or previous selection no longer exists.
149144
if (!inspectedExists)
@@ -163,14 +158,14 @@ internal static void Update()
163158
else
164159
{
165160
var allObjects = RuntimeProvider.Instance.FindObjectsOfTypeAll(typeof(GameObject));
166-
var list = new List<GameObject>();
161+
var objects = new List<GameObject>();
167162
foreach (var obj in allObjects)
168163
{
169164
var go = obj.TryCast<GameObject>();
170165
if (go.transform.parent == null && !go.scene.IsValid())
171-
list.Add(go);
166+
objects.Add(go);
172167
}
173-
rootObjects = list.ToArray();
168+
rootObjects = objects.ToArray();
174169
}
175170
}
176171
}

0 commit comments

Comments
 (0)