-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSceneAutoLoader.cs
104 lines (92 loc) · 3.88 KB
/
SceneAutoLoader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
/// This class adds a File > Scene Autoload menu containing options to select
/// a "master scene" enable it to be auto-loaded when the user presses play
/// in the editor. When enabled, the selected scene will be loaded on play,
/// then the original scene will be reloaded on stop.
///
/// To install, create a folder called Editor and put this script in there.
///
/// Based on an idea on this thread:
/// http://forum.unity3d.com/threads/157502-Executing-first-scene-in-build-settings-when-pressing-play-button-in-editor
[InitializeOnLoad]
static class SceneAutoLoader {
// Static constructor binds a playmode-changed callback.
// [InitializeOnLoad] above makes sure this gets executed.
static SceneAutoLoader() {
EditorApplication.playModeStateChanged += OnPlayModeChanged;
}
// Menu items to select the "master" scene and control whether or not to load it.
[MenuItem("File/Scene Autoload/Select Master Scene...")]
private static void SelectMasterScene() {
string masterScene = EditorUtility.OpenFilePanel("Select Master Scene", Application.dataPath, "unity");
masterScene = masterScene.Replace(Application.dataPath, "Assets"); //project relative instead of absolute path
if (!string.IsNullOrEmpty(masterScene)) {
MasterScene = masterScene;
LoadMasterOnPlay = true;
}
}
[MenuItem("File/Scene Autoload/Load Master On Play", true)]
private static bool ShowLoadMasterOnPlay() {
return !LoadMasterOnPlay;
}
[MenuItem("File/Scene Autoload/Load Master On Play")]
private static void EnableLoadMasterOnPlay() {
LoadMasterOnPlay = true;
}
[MenuItem("File/Scene Autoload/Don't Load Master On Play", true)]
private static bool ShowDontLoadMasterOnPlay() {
return LoadMasterOnPlay;
}
[MenuItem("File/Scene Autoload/Don't Load Master On Play")]
private static void DisableLoadMasterOnPlay() {
LoadMasterOnPlay = false;
}
// Play mode change callback handles the scene load/reload.
private static void OnPlayModeChanged(PlayModeStateChange state) {
if (!LoadMasterOnPlay) {
return;
}
if (!EditorApplication.isPlaying && EditorApplication.isPlayingOrWillChangePlaymode) {
// User pressed play -- autoload master scene.
PreviousScene = EditorSceneManager.GetActiveScene().path;
if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo()) {
try {
EditorSceneManager.OpenScene(MasterScene);
} catch {
Debug.LogError(string.Format("error: scene not found: {0}", MasterScene));
EditorApplication.isPlaying = false;
}
} else {
// User cancelled the save operation -- cancel play as well.
EditorApplication.isPlaying = false;
}
}
// isPlaying check required because cannot OpenScene while playing
if (!EditorApplication.isPlaying && !EditorApplication.isPlayingOrWillChangePlaymode) {
// User pressed stop -- reload previous scene.
try {
EditorSceneManager.OpenScene(PreviousScene);
} catch {
Debug.LogError(string.Format("error: scene not found: {0}", PreviousScene));
}
}
}
// Properties are remembered as editor preferences.
private const string cEditorPrefLoadMasterOnPlay = "SceneAutoLoader.LoadMasterOnPlay";
private const string cEditorPrefMasterScene = "SceneAutoLoader.MasterScene";
private const string cEditorPrefPreviousScene = "SceneAutoLoader.PreviousScene";
private static bool LoadMasterOnPlay {
get { return EditorPrefs.GetBool(cEditorPrefLoadMasterOnPlay, false); }
set { EditorPrefs.SetBool(cEditorPrefLoadMasterOnPlay, value); }
}
private static string MasterScene {
get { return EditorPrefs.GetString(cEditorPrefMasterScene, "Master.unity"); }
set { EditorPrefs.SetString(cEditorPrefMasterScene, value); }
}
private static string PreviousScene {
get { return EditorPrefs.GetString(cEditorPrefPreviousScene, EditorSceneManager.GetActiveScene().path); }
set { EditorPrefs.SetString(cEditorPrefPreviousScene, value); }
}
}