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

Commit 995e2a3

Browse files
committed
3.1.11: fix potential crash on scene reload
1 parent 2c95fec commit 995e2a3

File tree

3 files changed

+48
-69
lines changed

3 files changed

+48
-69
lines changed

src/ExplorerCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace UnityExplorer
1515
public class ExplorerCore
1616
{
1717
public const string NAME = "UnityExplorer";
18-
public const string VERSION = "3.1.10";
18+
public const string VERSION = "3.1.11";
1919
public const string AUTHOR = "Sinai";
2020
public const string GUID = "com.sinai.unityexplorer";
2121

src/Inspectors/SceneExplorer.cs

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,15 @@ internal static GameObject DontDestroyObject
5555
{
5656
get
5757
{
58-
if (!m_dontDestroyObject)
58+
if (!s_dontDestroyObject)
5959
{
60-
m_dontDestroyObject = new GameObject("DontDestroyMe");
61-
GameObject.DontDestroyOnLoad(m_dontDestroyObject);
60+
s_dontDestroyObject = new GameObject("DontDestroyMe");
61+
GameObject.DontDestroyOnLoad(s_dontDestroyObject);
6262
}
63-
return m_dontDestroyObject;
63+
return s_dontDestroyObject;
6464
}
6565
}
66-
67-
internal static GameObject m_dontDestroyObject;
66+
internal static GameObject s_dontDestroyObject;
6867

6968
public void Init()
7069
{
@@ -97,22 +96,6 @@ public void Update()
9796
}
9897
}
9998

100-
//#if CPP
101-
// public int GetSceneHandle(string sceneName)
102-
// {
103-
// if (sceneName == "DontDestroyOnLoad")
104-
// return DontDestroyScene;
105-
106-
// for (int i = 0; i < SceneManager.sceneCount; i++)
107-
// {
108-
// var scene = SceneManager.GetSceneAt(i);
109-
// if (scene.name == sceneName)
110-
// return scene.handle;
111-
// }
112-
// return -1;
113-
// }
114-
//#endif
115-
11699
internal void OnSceneChange()
117100
{
118101
m_sceneDropdown.OnCancel(null);
@@ -121,8 +104,13 @@ internal void OnSceneChange()
121104

122105
private void RefreshSceneSelector()
123106
{
124-
var names = new List<string>();
125-
var scenes = new List<Scene>();
107+
var newNames = new List<string>();
108+
var newScenes = new List<Scene>();
109+
110+
if (m_currentScenes == null)
111+
m_currentScenes = new Scene[0];
112+
113+
bool anyChange = SceneManager.sceneCount != m_currentScenes.Length - 1;
126114

127115
for (int i = 0; i < SceneManager.sceneCount; i++)
128116
{
@@ -131,33 +119,32 @@ private void RefreshSceneSelector()
131119
if (scene == default)
132120
continue;
133121

134-
scenes.Add(scene);
135-
names.Add(scene.name);
122+
if (!anyChange && !m_currentScenes.Any(it => it.GetHandle() == scene.GetHandle()))
123+
anyChange = true;
124+
125+
newScenes.Add(scene);
126+
newNames.Add(scene.name);
136127
}
137128

138-
names.Add("DontDestroyOnLoad");
139-
scenes.Add(DontDestroyScene);
129+
newNames.Add("DontDestroyOnLoad");
130+
newScenes.Add(DontDestroyScene);
140131

141132
m_sceneDropdown.options.Clear();
142133

143-
foreach (string scene in names)
134+
foreach (string scene in newNames)
144135
{
145136
m_sceneDropdown.options.Add(new Dropdown.OptionData { text = scene });
146137
}
147138

148-
if (!names.Contains(m_sceneDropdownText.text))
139+
if (anyChange)
149140
{
150-
m_sceneDropdownText.text = names[0];
151-
SetTargetScene(scenes[0]);
141+
m_sceneDropdownText.text = newNames[0];
142+
SetTargetScene(newScenes[0]);
152143
}
153144

154-
m_currentScenes = scenes.ToArray();
145+
m_currentScenes = newScenes.ToArray();
155146
}
156147

157-
//#if CPP
158-
// public void SetTargetScene(string name) => SetTargetScene(scene.handle);
159-
//#endif
160-
161148
public void SetTargetScene(Scene scene)
162149
{
163150
if (scene == default)
@@ -167,7 +154,7 @@ public void SetTargetScene(Scene scene)
167154
#if CPP
168155
GameObject[] rootObjs = SceneUnstrip.GetRootGameObjects(scene.handle);
169156
#else
170-
GameObject[] rootObjs = SceneUnstrip.GetRootGameObjects(scene);
157+
GameObject[] rootObjs = scene.GetRootGameObjects();
171158
#endif
172159
SetSceneObjectList(rootObjs);
173160

src/Unstrip/SceneUnstrip.cs

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,25 @@
33
using UnityEngine;
44
using UnityEngine.SceneManagement;
55
using UnityExplorer.Inspectors;
6+
using System.Reflection;
67

78
namespace UnityExplorer.Unstrip
89
{
9-
public class SceneUnstrip
10+
public static class SceneUnstrip
1011
{
1112
#if MONO
12-
public static GameObject[] GetRootGameObjects(Scene scene) => scene.GetRootGameObjects();
13-
14-
//public static GameObject[] GetRootGameObjects(int handle)
15-
//{
16-
// Scene scene = default;
17-
// if (handle == SceneExplorer.DontDestroyHandle)
18-
// scene = SceneExplorer.DontDestroyObject.scene;
19-
// else
20-
// {
21-
// for (int i = 0; i < SceneManager.sceneCount; i++)
22-
// {
23-
// var iscene = SceneManager.GetSceneAt(i);
24-
// if (iscene.handle == handle)
25-
// scene = iscene;
26-
// }
27-
// }
28-
29-
// if (scene != default && scene.handle != -1)
30-
// return scene.GetRootGameObjects();
31-
32-
// return new GameObject[0];
33-
//}
13+
private static readonly FieldInfo fi_Scene_handle = typeof(Scene).GetField("m_Handle", ReflectionHelpers.CommonFlags);
3414
#endif
3515

16+
public static int GetHandle(this Scene scene)
17+
{
18+
#if CPP
19+
return scene.handle;
20+
#else
21+
return (int)fi_Scene_handle.GetValue(scene);
22+
#endif
23+
}
24+
3625
#if CPP
3726
//Scene.GetRootGameObjects();
3827

@@ -43,13 +32,16 @@ public class SceneUnstrip
4332
public static GameObject[] GetRootGameObjects(int handle)
4433
{
4534
if (handle == -1)
46-
{
4735
return new GameObject[0];
48-
}
4936

50-
Il2CppSystem.Collections.Generic.List<GameObject> list = new Il2CppSystem.Collections.Generic.List<GameObject>(GetRootCount(handle));
37+
int count = GetRootCount(handle);
38+
39+
if (count < 1)
40+
return new GameObject[0];
41+
42+
var list = new Il2CppSystem.Collections.Generic.List<GameObject>(count);
5143

52-
d_GetRootGameObjects iCall = ICallHelper.GetICall<d_GetRootGameObjects>("UnityEngine.SceneManagement.Scene::GetRootGameObjectsInternal");
44+
var iCall = ICallHelper.GetICall<d_GetRootGameObjects>("UnityEngine.SceneManagement.Scene::GetRootGameObjectsInternal");
5345

5446
iCall.Invoke(handle, list.Pointer);
5547

@@ -58,14 +50,14 @@ public static GameObject[] GetRootGameObjects(int handle)
5850

5951
//Scene.rootCount;
6052

61-
internal delegate int GetRootCountInternal_delegate(int handle);
53+
internal delegate int d_GetRootCountInternal(int handle);
6254

6355
public static int GetRootCount(Scene scene) => GetRootCount(scene.handle);
6456

6557
public static int GetRootCount(int handle)
6658
{
67-
GetRootCountInternal_delegate iCall = ICallHelper.GetICall<GetRootCountInternal_delegate>("UnityEngine.SceneManagement.Scene::GetRootCountInternal");
68-
return iCall.Invoke(handle);
59+
return ICallHelper.GetICall<d_GetRootCountInternal>("UnityEngine.SceneManagement.Scene::GetRootCountInternal")
60+
.Invoke(handle);
6961
}
7062
#endif
7163
}

0 commit comments

Comments
 (0)