Last active
January 29, 2022 06:13
-
-
Save TheJustLink/2acc7bf358483c0de58e7a23e9461a97 to your computer and use it in GitHub Desktop.
Auto save unity scene/assets when playmode started. Works fine if crash happened. Optional: You can place this C# file to Editor folder in Assets
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.SceneManagement; | |
[InitializeOnLoad] | |
class AutoSaveExtension | |
{ | |
static AutoSaveExtension() | |
{ | |
EditorApplication.playModeStateChanged += OnPlaymodeStateChanged; | |
} | |
private static void OnPlaymodeStateChanged(PlayModeStateChange playModeStateChange) | |
{ | |
if (IsStartingPlaymode()) | |
Save(); | |
} | |
private static bool IsStartingPlaymode() | |
{ | |
return EditorApplication.isPlayingOrWillChangePlaymode | |
&& !EditorApplication.isPlaying; | |
} | |
private static void Save() | |
{ | |
EditorSceneManager.SaveScene(SceneManager.GetActiveScene()); | |
AssetDatabase.SaveAssets(); | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment