Created
July 25, 2016 15:45
-
-
Save shaneparsons/055dcf615ffbe0861508aac7a064d303 to your computer and use it in GitHub Desktop.
SceneAutoLoader.cs - Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using UnityEditor.SceneManagement; | |
/// <summary> | |
/// Scene auto loader. | |
/// </summary> | |
/// <description> | |
/// 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. | |
/// | |
/// 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 | |
/// </description> | |
[InitializeOnLoad] | |
public static class SceneAutoLoader | |
{ | |
// Static constructor binds a playmode-changed callback. | |
// [InitializeOnLoad] above makes sure this gets execusted. | |
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"); | |
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() | |
{ | |
if (!LoadMasterOnPlay) return; | |
if (!EditorApplication.isPlaying && EditorApplication.isPlayingOrWillChangePlaymode) | |
{ | |
// User pressed play -- autoload master scene. | |
PreviousScene = EditorSceneManager.GetActiveScene().path; | |
if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo()) | |
{ | |
EditorSceneManager.OpenScene(MasterScene, OpenSceneMode.Single); | |
} | |
else | |
{ | |
// User cancelled the save operation -- cancel play as well. | |
EditorApplication.isPlaying = false; | |
} | |
} | |
if (EditorApplication.isPlaying && !EditorApplication.isPlayingOrWillChangePlaymode) | |
{ | |
// User pressed stop -- reload previous scene. | |
if (SceneAutoLoader.PreviousScene != SceneAutoLoader.MasterScene) | |
EditorApplication.update += ReloadLastScene; | |
} | |
} | |
public static void ReloadLastScene() | |
{ | |
Debug.Log("Reloading.."); | |
if (!EditorApplication.isPlaying && EditorSceneManager.GetActiveScene().path != SceneAutoLoader.PreviousScene) | |
{ | |
EditorSceneManager.OpenScene(SceneAutoLoader.PreviousScene); | |
EditorApplication.update -= ReloadLastScene; | |
} | |
} | |
// Properties are remembered as editor preferences. | |
private const string cEditorPrefLoadMasterOnPlay = "SceneAutoLoader.LoadMasterOnPlay"; | |
private const string cEditorPrefMasterScene = "SceneAutoLoader.MasterScene"; | |
private const string cEditorPrefPreviousScene = "SceneAutoLoader.PreviousScene"; | |
public 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; | |
public static string PreviousScene | |
{ | |
get { return EditorPrefs.GetString(cEditorPrefPreviousScene, _previousScene); } | |
set | |
{ | |
_previousScene = value; | |
EditorPrefs.SetString(cEditorPrefPreviousScene, value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment