Last active
July 17, 2020 11:53
-
-
Save c0ffeeartc/5a2bc7fdae817739104c3c91ce3a9c8f to your computer and use it in GitHub Desktop.
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
#if UNITY_EDITOR | |
using UnityEditor; | |
using UnityEditor.SceneManagement; | |
using UnityEngine; | |
namespace Custom.Scripts | |
{ | |
[InitializeOnLoad] | |
static class PlayMainScene_PrevSceneAutoLoader | |
{ | |
static PlayMainScene_PrevSceneAutoLoader() // [InitializeOnLoad] above makes sure this gets executed. | |
{ | |
EditorApplication.playModeStateChanged += OnPlayModeChanged; | |
} | |
private static string PrevScenePath | |
{ | |
get { return EditorPrefs.GetString("cEditorPrefPreviousScene", EditorSceneManager.GetActiveScene().path); } | |
set { EditorPrefs.SetString("cEditorPrefPreviousScene", value); } | |
} | |
private static void OnPlayModeChanged(PlayModeStateChange state) | |
{ | |
if (!EditorApplication.isPlaying | |
&& !EditorApplication.isPlayingOrWillChangePlaymode // User pressed stop -- reload previous scene. | |
&& !string.IsNullOrEmpty(PrevScenePath) ) | |
{ | |
// Debug.Log( PrevScenePath ); | |
try | |
{ | |
EditorSceneManager.OpenScene(PrevScenePath); | |
} | |
catch | |
{ | |
Debug.LogError(string.Format("error: scene not found: {0}", PrevScenePath)); | |
} | |
finally | |
{ | |
PrevScenePath = ""; | |
} | |
} | |
} | |
[MenuItem("Custom/Play Main Scene #F8")] | |
private static void Play() | |
{ | |
if ( EditorApplication.isPlaying ) | |
{ | |
EditorApplication.isPaused = !EditorApplication.isPaused; | |
return; | |
} | |
var proceed = EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo(); | |
if ( proceed ) | |
{ | |
PrevScenePath = EditorSceneManager.GetActiveScene().path; | |
EditorSceneManager.OpenScene(EditorBuildSettings.scenes[0].path); | |
EditorApplication.isPlaying = true; | |
} | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment