@@ -27,47 +27,37 @@ namespace Unity.Multiplayer.Samples.BossRoom.Editor
27
27
[ InitializeOnLoad ]
28
28
public class SceneBootstrapper
29
29
{
30
- const string k_BootstrapSceneKey = "BootstrapScene" ;
31
30
const string k_PreviousSceneKey = "PreviousScene" ;
32
- const string k_LoadBootstrapSceneKey = "LoadBootstrapScene" ;
31
+ const string k_ShouldLoadBootstrapSceneKey = "LoadBootstrapScene" ;
33
32
34
33
const string k_LoadBootstrapSceneOnPlay = "Boss Room/Load Bootstrap Scene On Play" ;
35
34
const string k_DoNotLoadBootstrapSceneOnPlay = "Boss Room/Don't Load Bootstrap Scene On Play" ;
36
35
37
36
const string k_TestRunnerSceneName = "InitTestScene" ;
38
37
39
- static bool s_StoppingAndStarting ;
38
+ static bool s_RestartingToSwitchScene ;
40
39
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 ;
53
41
42
+ // to track where to go back to
54
43
static string PreviousScene
55
44
{
56
45
get => EditorPrefs . GetString ( k_PreviousSceneKey ) ;
57
46
set => EditorPrefs . SetString ( k_PreviousSceneKey , value ) ;
58
47
}
59
48
60
- static bool LoadBootstrapScene
49
+ static bool ShouldLoadBootstrapScene
61
50
{
62
51
get
63
52
{
64
- if ( ! EditorPrefs . HasKey ( k_LoadBootstrapSceneKey ) )
53
+ if ( ! EditorPrefs . HasKey ( k_ShouldLoadBootstrapSceneKey ) )
65
54
{
66
- EditorPrefs . SetBool ( k_LoadBootstrapSceneKey , true ) ;
55
+ EditorPrefs . SetBool ( k_ShouldLoadBootstrapSceneKey , true ) ;
67
56
}
68
- return EditorPrefs . GetBool ( k_LoadBootstrapSceneKey , true ) ;
57
+
58
+ return EditorPrefs . GetBool ( k_ShouldLoadBootstrapSceneKey , true ) ;
69
59
}
70
- set => EditorPrefs . SetBool ( k_LoadBootstrapSceneKey , value ) ;
60
+ set => EditorPrefs . SetBool ( k_ShouldLoadBootstrapSceneKey , value ) ;
71
61
}
72
62
73
63
static SceneBootstrapper ( )
@@ -78,45 +68,52 @@ static SceneBootstrapper()
78
68
[ MenuItem ( k_LoadBootstrapSceneOnPlay , true ) ]
79
69
static bool ShowLoadBootstrapSceneOnPlay ( )
80
70
{
81
- return ! LoadBootstrapScene ;
71
+ return ! ShouldLoadBootstrapScene ;
82
72
}
83
73
84
74
[ MenuItem ( k_LoadBootstrapSceneOnPlay ) ]
85
75
static void EnableLoadBootstrapSceneOnPlay ( )
86
76
{
87
- LoadBootstrapScene = true ;
77
+ ShouldLoadBootstrapScene = true ;
88
78
}
89
79
90
80
[ MenuItem ( k_DoNotLoadBootstrapSceneOnPlay , true ) ]
91
81
static bool ShowDoNotLoadBootstrapSceneOnPlay ( )
92
82
{
93
- return LoadBootstrapScene ;
83
+ return ShouldLoadBootstrapScene ;
94
84
}
95
85
96
86
[ MenuItem ( k_DoNotLoadBootstrapSceneOnPlay ) ]
97
87
static void DisableDoNotLoadBootstrapSceneOnPlay ( )
98
88
{
99
- LoadBootstrapScene = false ;
89
+ ShouldLoadBootstrapScene = false ;
100
90
}
101
91
102
- static void EditorApplicationOnplayModeStateChanged ( PlayModeStateChange obj )
92
+ static void EditorApplicationOnplayModeStateChanged ( PlayModeStateChange playModeStateChange )
103
93
{
104
94
if ( IsTestRunnerActive ( ) )
105
95
{
106
96
return ;
107
97
}
108
98
109
- if ( ! LoadBootstrapScene )
99
+ if ( ! ShouldLoadBootstrapScene )
110
100
{
111
101
return ;
112
102
}
113
103
114
- if ( s_StoppingAndStarting )
104
+ if ( s_RestartingToSwitchScene )
115
105
{
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
+ }
116
113
return ;
117
114
}
118
115
119
- if ( obj == PlayModeStateChange . ExitingEditMode )
116
+ if ( playModeStateChange == PlayModeStateChange . ExitingEditMode )
120
117
{
121
118
// cache previous scene so we return to this scene after play session, if possible
122
119
PreviousScene = EditorSceneManager . GetActiveScene ( ) . path ;
@@ -130,33 +127,28 @@ static void EditorApplicationOnplayModeStateChanged(PlayModeStateChange obj)
130
127
{
131
128
var activeScene = EditorSceneManager . GetActiveScene ( ) ;
132
129
133
- s_StoppingAndStarting = activeScene . path == string . Empty ||
134
- ! BootstrapScene . Contains ( activeScene . path ) ;
130
+ s_RestartingToSwitchScene = activeScene . path == string . Empty || ! BootstrapScene . Contains ( activeScene . path ) ;
135
131
136
132
// we only manually inject Bootstrap scene if we are in a blank empty scene,
137
133
// or if the active scene is not already BootstrapScene
138
- if ( s_StoppingAndStarting )
134
+ if ( s_RestartingToSwitchScene )
139
135
{
140
- s_StoppingAndStarting = true ;
141
- EditorApplication . ExitPlaymode ( ) ;
136
+ EditorApplication . isPlaying = false ;
142
137
143
138
// scene is included in build settings; open it
144
139
EditorSceneManager . OpenScene ( BootstrapScene ) ;
145
140
146
- EditorApplication . EnterPlaymode ( ) ;
147
- s_StoppingAndStarting = false ;
148
-
141
+ EditorApplication . isPlaying = true ;
149
142
}
150
143
}
151
144
}
152
145
else
153
146
{
154
147
// user either hit "Cancel" or exited window; don't open bootstrap scene & return to editor
155
-
156
148
EditorApplication . isPlaying = false ;
157
149
}
158
150
}
159
- else if ( obj == PlayModeStateChange . EnteredEditMode )
151
+ else if ( playModeStateChange == PlayModeStateChange . EnteredEditMode )
160
152
{
161
153
if ( ! string . IsNullOrEmpty ( PreviousScene ) )
162
154
{
0 commit comments