Skip to content

Instantly share code, notes, and snippets.

@c0ffeeartc
Last active July 17, 2020 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c0ffeeartc/5a2bc7fdae817739104c3c91ce3a9c8f to your computer and use it in GitHub Desktop.
Save c0ffeeartc/5a2bc7fdae817739104c3c91ce3a9c8f to your computer and use it in GitHub Desktop.
#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