Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Auto-Save open unity scenes when launching play mode, or every five minutes.
#if UNITY_EDITOR
using System;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
[InitializeOnLoad]
public class AutoSave
{
private static DateTime nextSaveTime;
// Static constructor that gets called when unity fires up.
static AutoSave()
{
EditorApplication.playModeStateChanged += (PlayModeStateChange state) => {
// If we're about to run the scene...
if (!EditorApplication.isPlayingOrWillChangePlaymode || EditorApplication.isPlaying) return;
// Save the scene and the assets.
Debug.Log("Auto-saving all open scenes... " + state);
EditorSceneManager.SaveOpenScenes();
AssetDatabase.SaveAssets();
};
// Also, every five minutes.
nextSaveTime = DateTime.Now.AddMinutes(5);
EditorApplication.update += Update;
Debug.Log("Added callback.");
}
private static void Update()
{
if (nextSaveTime > DateTime.Now) return;
nextSaveTime = nextSaveTime.AddMinutes(5);
Debug.Log("AutoSave Scenes: "+DateTime.Now.ToShortTimeString());
EditorSceneManager.SaveOpenScenes();
AssetDatabase.SaveAssets();
}
}
#endif
@dustingraham
Copy link
Author

dustingraham commented Mar 31, 2018

Updated to also save every five minutes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment