Search Unity

Feedback We need autosave. Let's talk about how it can be done.

Discussion in 'Editor & General Support' started by tomatohorse, Apr 9, 2020.

  1. tomatohorse

    tomatohorse

    Joined:
    Oct 26, 2013
    Posts:
    13
    I posted this in another thread, but saw elsewhere that feature requests should have a tag / flair, so I'm putting this in its own thread with said tag / flair.

    Here's an idea for how autosave could work:
    - Unity autosaves into a temp directory but doesn't overwrite your main project save file.
    - When you open the editor (let's say, after a crash), if Unity detects an autosave that is more recent than your most recent manual save, it says, "Would you like to load this autosave, from [timestamp]?"
    - If you say yes, it loads that file and that becomes your main project save file. If you say no, you load up your most recent manual save, which is what would happen currently. (This is how a number of programs work, so it's nothing too out of the ordinary)
    - Upon editor close, any temp autosave files that are OLDER than the most recent manual save would get deleted, so that you don't end up with tons of extra files.
    - You can set the autosave time interval in settings, or turn it off entirely if you want. But it defaults to on.

    Now, unless there are significant reasons to not have autosaving in principle (at least as an option like in my scenario above), it's really just a question of how to do it. I will also point out that many complex programs nowadays have figured out how to accomplish this (including other game engines, such as Unreal), so it can't be impossible. So come on, Unity team and community, let's get with the 21st century here and implement autosave!

    Someone responded to this post in the other thread saying that it could mess up versioning systems. OK, let's figure that out. Maybe Unity can store its autosaves in a separate temp file location so that when you point your versioning system at your project file, it doesn't get confused. Seems like a possible solution to me.
     
    Last edited: Apr 9, 2020
  2. Deleted User

    Deleted User

    Guest

    In the meantime, you can write your own autosave probramm:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using UnityEditor.SceneManagement;
    4.  
    5. public class AutoSave : EditorWindow
    6. {
    7.     [SerializeField] private float saveTime = 30;
    8.     [SerializeField] private float nextSave = 0;
    9.  
    10.     [MenuItem("Window/AutoSave")]
    11.     private static void Init()
    12.     {
    13.         AutoSave window = (AutoSave)EditorWindow.GetWindowWithRect(typeof(AutoSave), new Rect (0, 0, 200, 50));
    14.         window.Show();
    15.     }
    16.  
    17.     private void OnGUI()
    18.     {
    19.         EditorGUILayout.LabelField("Save Each:", saveTime + " Secs");
    20.         float timeToSave = nextSave - (float)EditorApplication.timeSinceStartup;
    21.         EditorGUILayout.LabelField("Next Save:", timeToSave.ToString() + " Sec");
    22.         Repaint();
    23.  
    24.         if(EditorApplication.timeSinceStartup > nextSave)
    25.         {
    26.             string[] path = EditorSceneManager.GetActiveScene().path.Split(char.Parse("/"));
    27.             path[path.Length - 1] = path[path.Length - 1];
    28.             bool saveOK = EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene(), string.Join("/", path));
    29.             Debug.Log("Saved Scene " + (saveOK ? "OK" : "Error!"));
    30.             nextSave = (float)EditorApplication.timeSinceStartup + saveTime;
    31.         }
    32.     }
    33. }