Unity Autosave on Background

All current (free) Unity autosave solutions depend on having the autosave editor opened at all times. With the little help from this blogpost, I have put together a solution that using multi-threading automatically saves your work on background every specific time interval. Plugin is loaded automatically with the project, and reload after every code compilation. It saves both scene and all assets.

The plugin is open-source and can be downloaded from GitHub. Below you’ll find some instructions:

Installation

Copy files into your scripts folder, maintining them in the Editor folder

Usage

In Window > AutoSave configure the time interval of the auto save.
Here you can also enable/disable the autosave as well as configure the verbosity of the plugin.

Implementation instructions

Plugin is using the multi-threading possibilities of Unity editor (which are rather limited). Unity in general obliges to perform any project related actions to be executed on the main thread. This blogpost explains, how to achieve this in Unity Editor (not the game). This blogpost explains how to add mutli-threading into your game. We have adjusted the script from the previously mentioned blogpost and execute the Threader fro the static method rather than depending on the Scriptable object what led into memory leak of the editor.

Apart from the multi-threading operations, we needed to save the user options, such as time interval of auto save. First option was to use a ScriptableObject, but it’s behaviour proved to be rather unexpected when reloading it after code complation. Therefore, we are storing the options in PlayerPrefs and expose public properties such as following:

public static bool ShowMessage { 
    get {
        return PlayerPrefs.GetInt(ShowMessagePref) == 1;
    } 
    set {
        if (value && PlayerPrefs.GetInt(ShowMessagePref) == 0 ||
            !value && PlayerPrefs.GetInt(ShowMessagePref) == 1) 
        {
            PlayerPrefs.SetInt(ShowMessagePref, value ? 1 : 0);
            PlayerPrefs.Save();
        }
    }
}
Advertisements