Skip to content

Commit a5e26b1

Browse files
Fix: bootstrap scene fix (MTT-3707) (#735)
1 parent 9d26864 commit a5e26b1

File tree

2 files changed

+31
-38
lines changed

2 files changed

+31
-38
lines changed

Assets/Scripts/Editor/SceneBootstrapper.cs

+30-38
Original file line numberDiff line numberDiff line change
@@ -27,47 +27,37 @@ namespace Unity.Multiplayer.Samples.BossRoom.Editor
2727
[InitializeOnLoad]
2828
public class SceneBootstrapper
2929
{
30-
const string k_BootstrapSceneKey = "BootstrapScene";
3130
const string k_PreviousSceneKey = "PreviousScene";
32-
const string k_LoadBootstrapSceneKey = "LoadBootstrapScene";
31+
const string k_ShouldLoadBootstrapSceneKey = "LoadBootstrapScene";
3332

3433
const string k_LoadBootstrapSceneOnPlay = "Boss Room/Load Bootstrap Scene On Play";
3534
const string k_DoNotLoadBootstrapSceneOnPlay = "Boss Room/Don't Load Bootstrap Scene On Play";
3635

3736
const string k_TestRunnerSceneName = "InitTestScene";
3837

39-
static bool s_StoppingAndStarting;
38+
static bool s_RestartingToSwitchScene;
4039

41-
static string BootstrapScene
42-
{
43-
get
44-
{
45-
if (!EditorPrefs.HasKey(k_BootstrapSceneKey))
46-
{
47-
EditorPrefs.SetString(k_BootstrapSceneKey, EditorBuildSettings.scenes[0].path);
48-
}
49-
return EditorPrefs.GetString(k_BootstrapSceneKey, EditorBuildSettings.scenes[0].path);
50-
}
51-
set => EditorPrefs.SetString(k_BootstrapSceneKey, value);
52-
}
40+
static string BootstrapScene => EditorBuildSettings.scenes[0].path;
5341

42+
// to track where to go back to
5443
static string PreviousScene
5544
{
5645
get => EditorPrefs.GetString(k_PreviousSceneKey);
5746
set => EditorPrefs.SetString(k_PreviousSceneKey, value);
5847
}
5948

60-
static bool LoadBootstrapScene
49+
static bool ShouldLoadBootstrapScene
6150
{
6251
get
6352
{
64-
if (!EditorPrefs.HasKey(k_LoadBootstrapSceneKey))
53+
if (!EditorPrefs.HasKey(k_ShouldLoadBootstrapSceneKey))
6554
{
66-
EditorPrefs.SetBool(k_LoadBootstrapSceneKey, true);
55+
EditorPrefs.SetBool(k_ShouldLoadBootstrapSceneKey, true);
6756
}
68-
return EditorPrefs.GetBool(k_LoadBootstrapSceneKey, true);
57+
58+
return EditorPrefs.GetBool(k_ShouldLoadBootstrapSceneKey, true);
6959
}
70-
set => EditorPrefs.SetBool(k_LoadBootstrapSceneKey, value);
60+
set => EditorPrefs.SetBool(k_ShouldLoadBootstrapSceneKey, value);
7161
}
7262

7363
static SceneBootstrapper()
@@ -78,45 +68,52 @@ static SceneBootstrapper()
7868
[MenuItem(k_LoadBootstrapSceneOnPlay, true)]
7969
static bool ShowLoadBootstrapSceneOnPlay()
8070
{
81-
return !LoadBootstrapScene;
71+
return !ShouldLoadBootstrapScene;
8272
}
8373

8474
[MenuItem(k_LoadBootstrapSceneOnPlay)]
8575
static void EnableLoadBootstrapSceneOnPlay()
8676
{
87-
LoadBootstrapScene = true;
77+
ShouldLoadBootstrapScene = true;
8878
}
8979

9080
[MenuItem(k_DoNotLoadBootstrapSceneOnPlay, true)]
9181
static bool ShowDoNotLoadBootstrapSceneOnPlay()
9282
{
93-
return LoadBootstrapScene;
83+
return ShouldLoadBootstrapScene;
9484
}
9585

9686
[MenuItem(k_DoNotLoadBootstrapSceneOnPlay)]
9787
static void DisableDoNotLoadBootstrapSceneOnPlay()
9888
{
99-
LoadBootstrapScene = false;
89+
ShouldLoadBootstrapScene = false;
10090
}
10191

102-
static void EditorApplicationOnplayModeStateChanged(PlayModeStateChange obj)
92+
static void EditorApplicationOnplayModeStateChanged(PlayModeStateChange playModeStateChange)
10393
{
10494
if (IsTestRunnerActive())
10595
{
10696
return;
10797
}
10898

109-
if (!LoadBootstrapScene)
99+
if (!ShouldLoadBootstrapScene)
110100
{
111101
return;
112102
}
113103

114-
if (s_StoppingAndStarting)
104+
if (s_RestartingToSwitchScene)
115105
{
106+
if (playModeStateChange == PlayModeStateChange.EnteredPlayMode)
107+
{
108+
// for some reason there's multiple start and stops events happening while restarting the editor playmode. We're making sure to
109+
// set stoppingAndStarting only when we're done and we've entered playmode. This way we won't corrupt "activeScene" with the multiple
110+
// start and stop and will be able to return to the scene we were editing at first
111+
s_RestartingToSwitchScene = false;
112+
}
116113
return;
117114
}
118115

119-
if (obj == PlayModeStateChange.ExitingEditMode)
116+
if (playModeStateChange == PlayModeStateChange.ExitingEditMode)
120117
{
121118
// cache previous scene so we return to this scene after play session, if possible
122119
PreviousScene = EditorSceneManager.GetActiveScene().path;
@@ -130,33 +127,28 @@ static void EditorApplicationOnplayModeStateChanged(PlayModeStateChange obj)
130127
{
131128
var activeScene = EditorSceneManager.GetActiveScene();
132129

133-
s_StoppingAndStarting = activeScene.path == string.Empty ||
134-
!BootstrapScene.Contains(activeScene.path);
130+
s_RestartingToSwitchScene = activeScene.path == string.Empty || !BootstrapScene.Contains(activeScene.path);
135131

136132
// we only manually inject Bootstrap scene if we are in a blank empty scene,
137133
// or if the active scene is not already BootstrapScene
138-
if (s_StoppingAndStarting)
134+
if (s_RestartingToSwitchScene)
139135
{
140-
s_StoppingAndStarting = true;
141-
EditorApplication.ExitPlaymode();
136+
EditorApplication.isPlaying = false;
142137

143138
// scene is included in build settings; open it
144139
EditorSceneManager.OpenScene(BootstrapScene);
145140

146-
EditorApplication.EnterPlaymode();
147-
s_StoppingAndStarting = false;
148-
141+
EditorApplication.isPlaying = true;
149142
}
150143
}
151144
}
152145
else
153146
{
154147
// user either hit "Cancel" or exited window; don't open bootstrap scene & return to editor
155-
156148
EditorApplication.isPlaying = false;
157149
}
158150
}
159-
else if (obj == PlayModeStateChange.EnteredEditMode)
151+
else if (playModeStateChange == PlayModeStateChange.EnteredEditMode)
160152
{
161153
if (!string.IsNullOrEmpty(PreviousScene))
162154
{

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
3939
* Subscribing to a message channel while unsubscribing is pending (#675)
4040
* Using ```Visible``` instead of ```Enabled``` to make sure RNSM continues updating when off (#702)
4141
* Some NetworkBehaviours are disabled instead of being destroyed (#718) - This preserves the index order for NetworkBehaviours between server and clients, resulting in no indexing issue for sending/receiving RPCs.
42+
* Scene Bootstrapper: future proofing bootstrap scene so we don't rely on Startup's path. MTT-3707. (#735)
4243

4344
## [v1.3.0-pre] - 2022-06-23
4445

0 commit comments

Comments
 (0)