From 49cc1f7df351914a3e2f36bd41bbae412a0d6f42 Mon Sep 17 00:00:00 2001 From: Sinai <49360850+sinai-dev@users.noreply.github.com> Date: Mon, 24 Jan 2022 17:50:01 +1100 Subject: [PATCH] Fix issue with DontDestroyOnLoad not existing in some games, cleanups --- src/ObjectExplorer/SceneHandler.cs | 76 ++++++++++-------------------- 1 file changed, 26 insertions(+), 50 deletions(-) diff --git a/src/ObjectExplorer/SceneHandler.cs b/src/ObjectExplorer/SceneHandler.cs index a566a6d..58cc5c8 100644 --- a/src/ObjectExplorer/SceneHandler.cs +++ b/src/ObjectExplorer/SceneHandler.cs @@ -11,9 +11,7 @@ namespace UnityExplorer.ObjectExplorer { public static class SceneHandler { - /// - /// The currently inspected Scene. - /// + /// The currently inspected Scene. public static Scene? SelectedScene { get => selectedScene; @@ -27,63 +25,39 @@ namespace UnityExplorer.ObjectExplorer } private static Scene? selectedScene; - /// - /// The GameObjects in the currently inspected scene. - /// + /// The GameObjects in the currently inspected scene. public static GameObject[] CurrentRootObjects { get; private set; } = new GameObject[0]; - /// - /// All currently loaded Scenes. - /// - public static List LoadedScenes { get; private set; } = new List(); + /// All currently loaded Scenes. + public static List LoadedScenes { get; private set; } = new(); private static HashSet previousLoadedScenes; - /// - /// The names of all scenes in the build settings, if they could be retrieved. - /// - public static readonly List AllSceneNames = new List(); + /// The names of all scenes in the build settings, if they could be retrieved. + public static List AllSceneNames { get; private set; } = new(); - /// - /// Whether or not we successfuly retrieved the names of the scenes in the build settings. - /// - public static bool WasAbleToGetScenesInBuild { get; private set; } - - /// - /// Invoked when the currently inspected Scene changes. The argument is the new scene. - /// + /// Invoked when the currently inspected Scene changes. The argument is the new scene. public static event Action OnInspectedSceneChanged; - /// - /// Invoked whenever the list of currently loaded Scenes changes. The argument contains all loaded scenes after the change. - /// + /// Invoked whenever the list of currently loaded Scenes changes. The argument contains all loaded scenes after the change. public static event Action> OnLoadedScenesChanged; - /// - /// Equivalent to + 2, to include 'DontDestroyOnLoad' and the 'None' scene. - /// - public static int LoadedSceneCount => SceneManager.sceneCount + 2; - - internal static Scene DontDestroyScene => DontDestroyMe.scene; - internal static int DontDestroyHandle => DontDestroyScene.handle; - - internal static GameObject DontDestroyMe - { - get - { - if (!dontDestroyObject) - { - dontDestroyObject = new GameObject("DontDestroyMe"); - GameObject.DontDestroyOnLoad(dontDestroyObject); - } - return dontDestroyObject; - } - } - private static GameObject dontDestroyObject; + /// Generally will be 2, unless DontDestroyExists == false, then this will be 1. + internal static int DefaultSceneCount => 1 + (DontDestroyExists ? 1 : 0); + /// Whether or not we are currently inspecting the "HideAndDontSave" asset scene. public static bool InspectingAssetScene => SelectedScene.HasValue && SelectedScene.Value == default; + /// Whether or not we successfuly retrieved the names of the scenes in the build settings. + public static bool WasAbleToGetScenesInBuild { get; private set; } + + /// Whether or not the "DontDestroyOnLoad" scene exists in this game. + public static bool DontDestroyExists { get; private set; } + internal static void Init() { + // Check if the game has "DontDestroyOnLoad" + DontDestroyExists = Scene.GetNameInternal(-12) == "DontDestroyOnLoad"; + // Try to get all scenes in the build settings. This may not work. try { @@ -111,8 +85,9 @@ namespace UnityExplorer.ObjectExplorer internal static void Update() { // check if the loaded scenes changed. always confirm DontDestroy / HideAndDontSave - int confirmedCount = 2; - bool inspectedExists = SelectedScene == DontDestroyScene || (SelectedScene.HasValue && SelectedScene.Value == default); + int confirmedCount = DefaultSceneCount; + bool inspectedExists = (SelectedScene.HasValue && SelectedScene.Value.handle == -12) + || (SelectedScene.HasValue && SelectedScene.Value.handle == -1); LoadedScenes.Clear(); @@ -133,8 +108,9 @@ namespace UnityExplorer.ObjectExplorer LoadedScenes.Add(scene); } - LoadedScenes.Add(DontDestroyScene); - LoadedScenes.Add(default); + if (DontDestroyExists) + LoadedScenes.Add(new Scene { m_Handle = -12 }); + LoadedScenes.Add(new Scene { m_Handle = -1 }); bool anyChange = confirmedCount != LoadedScenes.Count;