diff --git a/src/ExplorerCore.cs b/src/ExplorerCore.cs index 0553d9b..cede49d 100644 --- a/src/ExplorerCore.cs +++ b/src/ExplorerCore.cs @@ -15,7 +15,7 @@ namespace UnityExplorer public class ExplorerCore { public const string NAME = "UnityExplorer"; - public const string VERSION = "3.1.10"; + public const string VERSION = "3.1.11"; public const string AUTHOR = "Sinai"; public const string GUID = "com.sinai.unityexplorer"; diff --git a/src/Inspectors/SceneExplorer.cs b/src/Inspectors/SceneExplorer.cs index db22e48..514286f 100644 --- a/src/Inspectors/SceneExplorer.cs +++ b/src/Inspectors/SceneExplorer.cs @@ -55,16 +55,15 @@ namespace UnityExplorer.Inspectors { get { - if (!m_dontDestroyObject) + if (!s_dontDestroyObject) { - m_dontDestroyObject = new GameObject("DontDestroyMe"); - GameObject.DontDestroyOnLoad(m_dontDestroyObject); + s_dontDestroyObject = new GameObject("DontDestroyMe"); + GameObject.DontDestroyOnLoad(s_dontDestroyObject); } - return m_dontDestroyObject; + return s_dontDestroyObject; } } - - internal static GameObject m_dontDestroyObject; + internal static GameObject s_dontDestroyObject; public void Init() { @@ -97,22 +96,6 @@ namespace UnityExplorer.Inspectors } } -//#if CPP -// public int GetSceneHandle(string sceneName) -// { -// if (sceneName == "DontDestroyOnLoad") -// return DontDestroyScene; - -// for (int i = 0; i < SceneManager.sceneCount; i++) -// { -// var scene = SceneManager.GetSceneAt(i); -// if (scene.name == sceneName) -// return scene.handle; -// } -// return -1; -// } -//#endif - internal void OnSceneChange() { m_sceneDropdown.OnCancel(null); @@ -121,8 +104,13 @@ namespace UnityExplorer.Inspectors private void RefreshSceneSelector() { - var names = new List(); - var scenes = new List(); + var newNames = new List(); + var newScenes = new List(); + + if (m_currentScenes == null) + m_currentScenes = new Scene[0]; + + bool anyChange = SceneManager.sceneCount != m_currentScenes.Length - 1; for (int i = 0; i < SceneManager.sceneCount; i++) { @@ -131,33 +119,32 @@ namespace UnityExplorer.Inspectors if (scene == default) continue; - scenes.Add(scene); - names.Add(scene.name); + if (!anyChange && !m_currentScenes.Any(it => it.GetHandle() == scene.GetHandle())) + anyChange = true; + + newScenes.Add(scene); + newNames.Add(scene.name); } - names.Add("DontDestroyOnLoad"); - scenes.Add(DontDestroyScene); + newNames.Add("DontDestroyOnLoad"); + newScenes.Add(DontDestroyScene); m_sceneDropdown.options.Clear(); - foreach (string scene in names) + foreach (string scene in newNames) { m_sceneDropdown.options.Add(new Dropdown.OptionData { text = scene }); } - if (!names.Contains(m_sceneDropdownText.text)) + if (anyChange) { - m_sceneDropdownText.text = names[0]; - SetTargetScene(scenes[0]); + m_sceneDropdownText.text = newNames[0]; + SetTargetScene(newScenes[0]); } - m_currentScenes = scenes.ToArray(); + m_currentScenes = newScenes.ToArray(); } -//#if CPP -// public void SetTargetScene(string name) => SetTargetScene(scene.handle); -//#endif - public void SetTargetScene(Scene scene) { if (scene == default) @@ -167,7 +154,7 @@ namespace UnityExplorer.Inspectors #if CPP GameObject[] rootObjs = SceneUnstrip.GetRootGameObjects(scene.handle); #else - GameObject[] rootObjs = SceneUnstrip.GetRootGameObjects(scene); + GameObject[] rootObjs = scene.GetRootGameObjects(); #endif SetSceneObjectList(rootObjs); diff --git a/src/Unstrip/SceneUnstrip.cs b/src/Unstrip/SceneUnstrip.cs index de7de35..6f8f659 100644 --- a/src/Unstrip/SceneUnstrip.cs +++ b/src/Unstrip/SceneUnstrip.cs @@ -3,36 +3,25 @@ using UnityExplorer.Helpers; using UnityEngine; using UnityEngine.SceneManagement; using UnityExplorer.Inspectors; +using System.Reflection; namespace UnityExplorer.Unstrip { - public class SceneUnstrip + public static class SceneUnstrip { #if MONO - public static GameObject[] GetRootGameObjects(Scene scene) => scene.GetRootGameObjects(); - - //public static GameObject[] GetRootGameObjects(int handle) - //{ - // Scene scene = default; - // if (handle == SceneExplorer.DontDestroyHandle) - // scene = SceneExplorer.DontDestroyObject.scene; - // else - // { - // for (int i = 0; i < SceneManager.sceneCount; i++) - // { - // var iscene = SceneManager.GetSceneAt(i); - // if (iscene.handle == handle) - // scene = iscene; - // } - // } - - // if (scene != default && scene.handle != -1) - // return scene.GetRootGameObjects(); - - // return new GameObject[0]; - //} + private static readonly FieldInfo fi_Scene_handle = typeof(Scene).GetField("m_Handle", ReflectionHelpers.CommonFlags); #endif + public static int GetHandle(this Scene scene) + { +#if CPP + return scene.handle; +#else + return (int)fi_Scene_handle.GetValue(scene); +#endif + } + #if CPP //Scene.GetRootGameObjects(); @@ -43,13 +32,16 @@ namespace UnityExplorer.Unstrip public static GameObject[] GetRootGameObjects(int handle) { if (handle == -1) - { return new GameObject[0]; - } - Il2CppSystem.Collections.Generic.List list = new Il2CppSystem.Collections.Generic.List(GetRootCount(handle)); + int count = GetRootCount(handle); - d_GetRootGameObjects iCall = ICallHelper.GetICall("UnityEngine.SceneManagement.Scene::GetRootGameObjectsInternal"); + if (count < 1) + return new GameObject[0]; + + var list = new Il2CppSystem.Collections.Generic.List(count); + + var iCall = ICallHelper.GetICall("UnityEngine.SceneManagement.Scene::GetRootGameObjectsInternal"); iCall.Invoke(handle, list.Pointer); @@ -58,14 +50,14 @@ namespace UnityExplorer.Unstrip //Scene.rootCount; - internal delegate int GetRootCountInternal_delegate(int handle); + internal delegate int d_GetRootCountInternal(int handle); public static int GetRootCount(Scene scene) => GetRootCount(scene.handle); public static int GetRootCount(int handle) { - GetRootCountInternal_delegate iCall = ICallHelper.GetICall("UnityEngine.SceneManagement.Scene::GetRootCountInternal"); - return iCall.Invoke(handle); + return ICallHelper.GetICall("UnityEngine.SceneManagement.Scene::GetRootCountInternal") + .Invoke(handle); } #endif }