Skip to content

Instantly share code, notes, and snippets.

@kinifi
Created November 3, 2015 19:28
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kinifi/2f79262d6d7b7f7e95ee to your computer and use it in GitHub Desktop.
Save kinifi/2f79262d6d7b7f7e95ee to your computer and use it in GitHub Desktop.
Auto Save the current open Unity 3D level every 30 seconds
/* Created by Chris Figueroa - @Kinifi
* Create a script called AutoSave.cs
*/
using UnityEngine;
using UnityEditor;
using System.Collections;
public class AutoSave : EditorWindow {
private float saveTime = 30;
private double nextSaveTime = 0;
private bool stopAutoSave;
[MenuItem("Window/AutoSave Level")]
static void ShowEditor()
{
AutoSave editor = EditorWindow.GetWindow<AutoSave>();
editor.minSize = new Vector2(300, 100);
editor.maxSize = new Vector2(300, 100);
editor.Init();
}
public void Init()
{
Debug.Log("The level will now be saved automatically");
}
void OnGUI()
{
GUILayout.Label("Auto Saves Level Every 30 Seconds");
stopAutoSave = GUILayout.Toggle(stopAutoSave, "Stop Auto Save");
GUILayout.Space(10);
if (stopAutoSave == false)
{
EditorGUILayout.LabelField("Save Each:", saveTime + " Secs");
double timeToSave = nextSaveTime - EditorApplication.timeSinceStartup;
EditorGUILayout.LabelField("Next Save:", timeToSave.ToString() + " Sec");
this.Repaint();
if (EditorApplication.timeSinceStartup > nextSaveTime)
{
var path = EditorApplication.currentScene;
//EditorApplication.SaveScene(path, true);
EditorApplication.SaveScene();
EditorApplication.SaveAssets();
Debug.Log("Saved Scene: " + path);
nextSaveTime = EditorApplication.timeSinceStartup + saveTime;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment