Skip to content

Instantly share code, notes, and snippets.

@TheJustLink
Last active January 29, 2022 06:13
Show Gist options
  • Save TheJustLink/2acc7bf358483c0de58e7a23e9461a97 to your computer and use it in GitHub Desktop.
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
#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