Skip to content

Instantly share code, notes, and snippets.

@der-hugo
Created March 21, 2022 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save der-hugo/e0566c1a60728676af5a19aba75fb564 to your computer and use it in GitHub Desktop.
Save der-hugo/e0566c1a60728676af5a19aba75fb564 to your computer and use it in GitHub Desktop.
Enables auto-saving in Unity
/// <summary>
/// Editor tools for auto-saving
/// </summary>
public static class AutoSaving
{
private const string k_MenuName = "Examples/Auto-Save/";
private static class IntervalAutoSaving
{
/// <summary>
/// Time interval in seconds in which to automatically save all assets and open scenes
/// </summary>
private const double k_SaveIntervalSeconds = 120;
/// <summary>
/// Stores the last time the assets where saved
/// </summary>
private static double s_LastSaveTime;
/// <summary>
/// Menu path for enabling and disabling auto-saving every <see cref="k_SaveIntervalSeconds"/> seconds
/// </summary>
private const string k_MenuNameToggleAutoSave = k_MenuName + "Save on time interval";
/// <summary>
/// Reads from / Writes to <see cref="EditorPrefs"/> using the <see cref="k_MenuNameToggleAutoSave"/> as key
/// </summary>
private static bool IsIntervalAutoSaving
{
get => EditorPrefs.GetBool(k_MenuNameToggleAutoSave, false);
set => EditorPrefs.SetBool(k_MenuNameToggleAutoSave, value);
}
[MenuItem(k_MenuNameToggleAutoSave)]
private static void ToggleIntervalAutoSaving()
{
IsIntervalAutoSaving = !IsIntervalAutoSaving;
ApplySettings();
}
[MenuItem(k_MenuNameToggleAutoSave, true)]
private static bool ToggleIntervalAutoSavingValidate()
{
Menu.SetChecked(k_MenuNameToggleAutoSave, IsIntervalAutoSaving);
return true;
}
private static void HandleIntervalAutoSaving()
{
// ignore play mode
if (EditorApplication.isPlayingOrWillChangePlaymode)
{
return;
}
if (EditorApplication.timeSinceStartup - s_LastSaveTime > k_SaveIntervalSeconds)
{
EditorSceneManager.SaveOpenScenes();
AssetDatabase.SaveAssets();
s_LastSaveTime = EditorApplication.timeSinceStartup;
Debug.Log("Auto saved assets.");
}
}
/// <summary>
/// Make sure the current settings are applied when the project is opened / on domain reload
/// </summary>
[InitializeOnLoadMethod]
private static void Initialize()
{
ApplySettings();
}
private static void ApplySettings()
{
// remove listener first -> makes sure it is always attached only exactly once
EditorApplication.update -= HandleIntervalAutoSaving;
if (IsIntervalAutoSaving)
{
EditorApplication.update += HandleIntervalAutoSaving;
}
}
}
private static class BeforePlayModeAutoSaving
{
/// <summary>
/// Menu path for enabling and disabling auto-saving before entering play mode
/// </summary>
private const string k_MenuNameToggleSaveBeforePlaymode = k_MenuName + "Save beofre enter playmode";
/// <summary>
/// Reads from / Writes to <see cref="EditorPrefs"/> using the <see cref="k_MenuNameToggleSaveBeforePlaymode"/> as key
/// </summary>
private static bool IsBeforePlayModeAutoSaving
{
get => EditorPrefs.GetBool(k_MenuNameToggleSaveBeforePlaymode, false);
set => EditorPrefs.SetBool(k_MenuNameToggleSaveBeforePlaymode, value);
}
[MenuItem(k_MenuNameToggleSaveBeforePlaymode)]
private static void ToggleBeforePlayModeAutoSaving()
{
IsBeforePlayModeAutoSaving = !IsBeforePlayModeAutoSaving;
ApplySettings();
}
[MenuItem(k_MenuNameToggleSaveBeforePlaymode, true)]
private static bool ToggleBeforePlayModeAutoSavingValidate()
{
Menu.SetChecked(k_MenuNameToggleSaveBeforePlaymode, IsBeforePlayModeAutoSaving);
return true;
}
/// <summary>
/// Make sure the current settings are applied when the project is opened / on domain reload
/// </summary>
[InitializeOnLoadMethod]
private static void Initialize()
{
ApplySettings();
}
private static void ApplySettings()
{
// remove listener first -> makes sure it is always attached only exactly once
EditorApplication.playModeStateChanged -= HandleBeforePlayModeAutoSaving;
if (IsBeforePlayModeAutoSaving)
{
EditorApplication.playModeStateChanged += HandleBeforePlayModeAutoSaving;
}
}
private static void HandleBeforePlayModeAutoSaving(PlayModeStateChange playModeStateChange)
{
if (playModeStateChange == PlayModeStateChange.ExitingEditMode)
{
EditorSceneManager.SaveOpenScenes();
AssetDatabase.SaveAssets();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment