Skip to content

Instantly share code, notes, and snippets.

@gaypunkposer
Last active November 16, 2023 11:39
Show Gist options
  • Save gaypunkposer/89baa0169354a18bb5347270775309f3 to your computer and use it in GitHub Desktop.
Save gaypunkposer/89baa0169354a18bb5347270775309f3 to your computer and use it in GitHub Desktop.
Unity auto save script. Throw in Assets/Editor. Saves assets and scene when playing a scene.
//Works on Unity 5.3+
using UnityEditor;
using UnityEditor.SceneManagement;
[InitializeOnLoad]
public class AutoSave
{
//Constructor called by Unity Editor
static AutoSave()
{
EditorApplication.playModeStateChanged += SaveWhenExitEdit;
}
private static void SaveWhenExitEdit(PlayModeStateChange change)
{
//If we're exiting edit mode (about to play the scene)
if (change == PlayModeStateChange.ExitingEditMode)
{
//Save the scene and the assets
EditorSceneManager.SaveOpenScenes();
AssetDatabase.SaveAssets();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment