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

Commit 49cc1f7

Browse files
committed
Fix issue with DontDestroyOnLoad not existing in some games, cleanups
1 parent 9f78c50 commit 49cc1f7

File tree

1 file changed

+25
-49
lines changed

1 file changed

+25
-49
lines changed

src/ObjectExplorer/SceneHandler.cs

Lines changed: 25 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ namespace UnityExplorer.ObjectExplorer
1111
{
1212
public static class SceneHandler
1313
{
14-
/// <summary>
15-
/// The currently inspected Scene.
16-
/// </summary>
14+
/// <summary>The currently inspected Scene.</summary>
1715
public static Scene? SelectedScene
1816
{
1917
get => selectedScene;
@@ -27,63 +25,39 @@ internal set
2725
}
2826
private static Scene? selectedScene;
2927

30-
/// <summary>
31-
/// The GameObjects in the currently inspected scene.
32-
/// </summary>
28+
/// <summary>The GameObjects in the currently inspected scene.</summary>
3329
public static GameObject[] CurrentRootObjects { get; private set; } = new GameObject[0];
3430

35-
/// <summary>
36-
/// All currently loaded Scenes.
37-
/// </summary>
38-
public static List<Scene> LoadedScenes { get; private set; } = new List<Scene>();
31+
/// <summary>All currently loaded Scenes.</summary>
32+
public static List<Scene> LoadedScenes { get; private set; } = new();
3933
private static HashSet<Scene> previousLoadedScenes;
4034

41-
/// <summary>
42-
/// The names of all scenes in the build settings, if they could be retrieved.
43-
/// </summary>
44-
public static readonly List<string> AllSceneNames = new List<string>();
35+
/// <summary>The names of all scenes in the build settings, if they could be retrieved.</summary>
36+
public static List<string> AllSceneNames { get; private set; } = new();
4537

46-
/// <summary>
47-
/// Whether or not we successfuly retrieved the names of the scenes in the build settings.
48-
/// </summary>
49-
public static bool WasAbleToGetScenesInBuild { get; private set; }
50-
51-
/// <summary>
52-
/// Invoked when the currently inspected Scene changes. The argument is the new scene.
53-
/// </summary>
38+
/// <summary>Invoked when the currently inspected Scene changes. The argument is the new scene.</summary>
5439
public static event Action<Scene> OnInspectedSceneChanged;
5540

56-
/// <summary>
57-
/// Invoked whenever the list of currently loaded Scenes changes. The argument contains all loaded scenes after the change.
58-
/// </summary>
41+
/// <summary>Invoked whenever the list of currently loaded Scenes changes. The argument contains all loaded scenes after the change.</summary>
5942
public static event Action<List<Scene>> OnLoadedScenesChanged;
6043

61-
/// <summary>
62-
/// Equivalent to <see cref="SceneManager.sceneCount"/> + 2, to include 'DontDestroyOnLoad' and the 'None' scene.
63-
/// </summary>
64-
public static int LoadedSceneCount => SceneManager.sceneCount + 2;
44+
/// <summary>Generally will be 2, unless DontDestroyExists == false, then this will be 1.</summary>
45+
internal static int DefaultSceneCount => 1 + (DontDestroyExists ? 1 : 0);
6546

66-
internal static Scene DontDestroyScene => DontDestroyMe.scene;
67-
internal static int DontDestroyHandle => DontDestroyScene.handle;
47+
/// <summary>Whether or not we are currently inspecting the "HideAndDontSave" asset scene.</summary>
48+
public static bool InspectingAssetScene => SelectedScene.HasValue && SelectedScene.Value == default;
6849

69-
internal static GameObject DontDestroyMe
70-
{
71-
get
72-
{
73-
if (!dontDestroyObject)
74-
{
75-
dontDestroyObject = new GameObject("DontDestroyMe");
76-
GameObject.DontDestroyOnLoad(dontDestroyObject);
77-
}
78-
return dontDestroyObject;
79-
}
80-
}
81-
private static GameObject dontDestroyObject;
50+
/// <summary>Whether or not we successfuly retrieved the names of the scenes in the build settings.</summary>
51+
public static bool WasAbleToGetScenesInBuild { get; private set; }
8252

83-
public static bool InspectingAssetScene => SelectedScene.HasValue && SelectedScene.Value == default;
53+
/// <summary>Whether or not the "DontDestroyOnLoad" scene exists in this game.</summary>
54+
public static bool DontDestroyExists { get; private set; }
8455

8556
internal static void Init()
8657
{
58+
// Check if the game has "DontDestroyOnLoad"
59+
DontDestroyExists = Scene.GetNameInternal(-12) == "DontDestroyOnLoad";
60+
8761
// Try to get all scenes in the build settings. This may not work.
8862
try
8963
{
@@ -111,8 +85,9 @@ internal static void Init()
11185
internal static void Update()
11286
{
11387
// check if the loaded scenes changed. always confirm DontDestroy / HideAndDontSave
114-
int confirmedCount = 2;
115-
bool inspectedExists = SelectedScene == DontDestroyScene || (SelectedScene.HasValue && SelectedScene.Value == default);
88+
int confirmedCount = DefaultSceneCount;
89+
bool inspectedExists = (SelectedScene.HasValue && SelectedScene.Value.handle == -12)
90+
|| (SelectedScene.HasValue && SelectedScene.Value.handle == -1);
11691

11792
LoadedScenes.Clear();
11893

@@ -133,8 +108,9 @@ internal static void Update()
133108
LoadedScenes.Add(scene);
134109
}
135110

136-
LoadedScenes.Add(DontDestroyScene);
137-
LoadedScenes.Add(default);
111+
if (DontDestroyExists)
112+
LoadedScenes.Add(new Scene { m_Handle = -12 });
113+
LoadedScenes.Add(new Scene { m_Handle = -1 });
138114

139115
bool anyChange = confirmedCount != LoadedScenes.Count;
140116

0 commit comments

Comments
 (0)