Last active
May 18, 2024 10:57
-
-
Save dustingraham/3e5367a12acba1eb013cc35e187e3898 to your computer and use it in GitHub Desktop.
Auto-Save open unity scenes when launching play mode, or every five minutes.
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 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to also save every five minutes.