AutoSave Window

Hey guys/gals, I seen a post a couple hours ago requesting an AutoSave feature so I decided to make one up real fast. It works great but you have to make sure you read the disclaimer before using it!

Remember you can always find/request scripts on my website. If you dont see a script, request it! If there are enough requests I will make it!

using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

public class AutoSave : EditorWindow
{
    public static string relativeSavePath = "/";
    public static string autoSaveFileName = "AutoSave";
    public static int secondsBetweenSaves = 600;

    static float nextsave = 0;
    static int timeLeft = 0;
    static bool showDisclaimer = true;
    static bool showDebug = true;
    static bool toggleVisible = false;
    static bool inPlayMode = false;
    static bool firstStart = true;
    static Vector2 winPosition = Vector2.zero;
    static Vector2 disPosition = Vector2.zero;

    [MenuItem("RI Editors/AutoSave")]
    static void Init()
    {
        // Get existing open window or if none, make a new one:
        AutoSave window = (AutoSave)EditorWindow.GetWindow(typeof(AutoSave));
        window.minSize = new Vector2(225, 21);
        window.Show();
    }

    void OnGUI()
    {
        if (firstStart)
        {
            nextsave = secondsBetweenSaves;
            firstStart = false;

            secondsBetweenSaves = PlayerPrefs.GetInt("AutoSave_seconds", secondsBetweenSaves);
            showDisclaimer = ConvertInttoBool(PlayerPrefs.GetInt("AutoSave_showHelp", ConvertBooltoInt(showDisclaimer)));
            relativeSavePath = PlayerPrefs.GetString("AutoSave_relPath", relativeSavePath);
            autoSaveFileName = PlayerPrefs.GetString("AutoSave_FileName", autoSaveFileName);
            showDebug = ConvertInttoBool(PlayerPrefs.GetInt("AutoSave_ShowDebug", ConvertBooltoInt(showDebug)));
        }

        if (GUILayout.Button("Created By ReverieInteractive"))
        {
            Application.OpenURL("www.reverieinteractive.com");
        }
        GUI.color = Color.white;
        GUI.contentColor = Color.white;
        winPosition = GUILayout.BeginScrollView(winPosition);
        if (showDisclaimer = EditorGUILayout.Foldout(showDisclaimer, "Disclaimer and Information"))
        {
            GUI.contentColor = Color.yellow;
            disPosition = GUILayout.BeginScrollView(disPosition);
            GUILayout.TextArea("IMPORTANT: The autosave will only save scenes if this window is OPEN and VISIBLE. If you hide this window, the autosave WILL NOT WORK!\n\n-------HELP INFO------\n\n\"Save Iteration\" - Seconds between saves (600 = saves every 10 min)\n\n\"File Name\" - The name of the AutoSave file (DONT name this the same as your opened scene)\n\n\"Relative Path\" - The path (from your projects folder up) to where the file is saved\n\n\"Apply Instantly\" - Applies settings and saves the scene without having to wait for the next save cycle");
            GUILayout.EndScrollView();
        }
        GUI.contentColor = Color.white;
        if (toggleVisible = EditorGUILayout.Foldout(toggleVisible, "AutoSave Settings"))
        {
            secondsBetweenSaves = EditorGUILayout.IntField("Save Iteration", secondsBetweenSaves);
            autoSaveFileName = EditorGUILayout.TextField("File Name", autoSaveFileName);
            relativeSavePath = EditorGUILayout.TextField("Relative Path", relativeSavePath);
            showDebug = EditorGUILayout.Toggle("Show Warning", showDebug);
        }

        if(GUILayout.Button("Apply/Save Instantly"))
        {
            SaveScene();
        }
        EditorGUILayout.LabelField("Next Save:", timeLeft + " Seconds");
        GUILayout.EndScrollView();
    }

    void OnInspectorUpdate()
    {
        if (EditorApplication.isPlaying)
        {
            inPlayMode = true;
        }
        else
        {
            if (inPlayMode == true)
            {
                SaveScene();
            }

            timeLeft = (int)(nextsave - Time.realtimeSinceStartup);
            this.Repaint();

            if (Time.realtimeSinceStartup >= nextsave)
            {
                SaveScene();
            }
        }
    }

    void SaveScene()
    {
        inPlayMode = false;

        if (!relativeSavePath.EndsWith("/"))
        {
            relativeSavePath += "/";
        }

        EditorApplication.SaveScene("Assets" + relativeSavePath + autoSaveFileName + ".unity");
        nextsave = Time.realtimeSinceStartup + secondsBetweenSaves;

        if(showDebug)
            Debug.LogWarning("Saved scene: " + relativeSavePath + autoSaveFileName + ".unity");


        PlayerPrefs.SetInt("AutoSave_seconds", secondsBetweenSaves);
        PlayerPrefs.SetInt("AutoSave_showHelp", ConvertBooltoInt(showDisclaimer));
        PlayerPrefs.SetString("AutoSave_relPath", relativeSavePath);
        PlayerPrefs.SetString("AutoSave_FileName", autoSaveFileName);
        PlayerPrefs.SetInt("AutoSave_ShowDebug", ConvertBooltoInt(showDebug));
    }

    int ConvertBooltoInt(bool boolean)
    {
        if (boolean)
            return 1;
        else
            return 0;
    }

    bool ConvertInttoBool(int boolean)
    {
        if (boolean == 1)
            return true;
        else
            return false;
    }
}

Hope this is helpful.
Jake

MAJOR FLAW: Hey guys I just noticed that I overlooked a major flaw in the AutoSave window. The flaw is, when you first open Unity (After a crash or it closes) the scene is automatically saved over the last file before you have time to re-open it!!

Please update your scripts to the new one I posted above if you are currently using this script!

Now it also remembers your last settings. Settings are applied to memory every time your scene is saved.

Sorry, and thanks!
Jake

This is certainly a good script and stuff, but eumh, could we get just a little more explanation about this?

Is there anything specific you wanna know?

It saves your scene every so often to help you when and if Unity crashes. It has saved me a few times when my computer froze and I had to restart!

Jake