Skip to content

Instantly share code, notes, and snippets.

@SiarheiPilat
Last active September 5, 2022 00:36
Show Gist options
  • Save SiarheiPilat/090aea82820f5748b21ca30484dc0e87 to your computer and use it in GitHub Desktop.
Save SiarheiPilat/090aea82820f5748b21ca30484dc0e87 to your computer and use it in GitHub Desktop.
future release of autosaver sua
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
namespace Suasor
{
// TODO:
// editor settings window
// rate button
// first time setup window
// TAKEN FROM: https://gist.github.com/SiarheiPilat/090aea82820f5748b21ca30484dc0e87/
[InitializeOnLoad]
public class AutoSave
{
static float saveTime = 60.0f;
static double nextSave = 0.0f;
static string[] ConsoleMessages = new string[] { "Autosaving...", "Autosaver goes brrrrr!", "Autosaving (every " + saveTime + " seconds, by the way!)", "Autosaving... Automatically saved your scene: " + EditorPrefs.GetInt("sua_autosaver_count") + " time(s)." };
static string TextColorDarkMode = "#dfd55a";
static string TextColorLightMode = "#ab143d";
static string CurrentTextColor;
static AutoSave()
{
EditorPrefs.SetInt("sua_autosaver_count", 0);
nextSave = EditorApplication.timeSinceStartup + saveTime;
CurrentTextColor = EditorGUIUtility.isProSkin ? TextColorDarkMode : TextColorLightMode;
EditorApplication.update += EditorUpdate;
}
static void EditorUpdate()
{
if (EditorPrefs.GetBool("sua_autosaver_on", true))
{
double timeToSave = nextSave - EditorApplication.timeSinceStartup;
if (EditorApplication.timeSinceStartup > nextSave)
{
if (!EditorApplication.isPlaying && UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene().isDirty)
{
EditorApplication.ExecuteMenuItem("File/Save");
nextSave = EditorApplication.timeSinceStartup + saveTime;
EditorPrefs.SetInt("sua_autosaver_count", EditorPrefs.GetInt("sua_autosaver_count") + 1);
Debug.Log("<color=" + CurrentTextColor + ">" + "Autosaving...</color> Automatically saved your scene: " + EditorPrefs.GetInt("sua_autosaver_count") + " time(s).");
}
}
}
}
[MenuItem("Window/Suasor/Autosaver/Turn off Autosaver")]
static void TurnOff()
{
EditorPrefs.SetBool("sua_autosaver_on", false);
Debug.Log("<color=" + CurrentTextColor + ">" + "Autosaver is OFF!</color>");
}
[MenuItem("Window/Suasor/Autosaver/Turn on Autosaver")]
static void TurnOn()
{
EditorPrefs.SetBool("sua_autosaver_on", true);
Debug.Log("<color=" + CurrentTextColor + ">" + "Autosaver is ON!</color>");
}
[MenuItem("Window/Suasor/Autosaver/Turn off Autosaver", true)]
static bool ValidateTurnOff()
{
return EditorPrefs.GetBool("sua_autosaver_on");
}
[MenuItem("Window/Suasor/Autosaver/Turn on Autosaver", true)]
static bool ValidateTurnOn()
{
return !EditorPrefs.GetBool("sua_autosaver_on");
}
static string GetFunnyMessage()
{
int rand = Random.Range(0, ConsoleMessages.Length);
return ConsoleMessages[rand];
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment