Search Unity

We NEED Auto-save feature

Discussion in 'Editor & General Support' started by PapitoMyKing, Jul 18, 2017.

  1. PapitoMyKing

    PapitoMyKing

    Joined:
    Apr 25, 2016
    Posts:
    10
    Two brits working in a basement made Construct 2 ten years ago, and while running on tea and fried fish, managed to implement an auto-save feature for their game-making software.

    I just lost 2 hours of my time because this pig of a software decided to hang itself.

    Why don't I save often? Sorry but when I'm focused, I forget to babysit the editor.

    Just found this plugin on the store, going to give it a try:

    https://www.assetstore.unity3d.com/en/#!/content/90091
     
    kilik128, Circool, zeiksz and 4 others like this.
  2. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,470
    save often and commit to a VCS problem solved
     
    leftshoe18 likes this.
  3. ScottyB

    ScottyB

    Joined:
    Apr 4, 2011
    Posts:
    131
    Unity actually does auto-save your scenes every time you enter play-mode and stores them in the Temp/__Backupscenes folder. These files are not meant to be used as a user-facing auto-save feature (I think Unity might use them to restore all your scene settings after exiting play-mode so play-mode changes don't stick around) but it can be used in a pinch to restore your lost progress. Just remember that you need to copy those backup files before you re-open Unity after a crash as Unity wipes the Temp folder on launch.
     
    guetta18, phoda, Ryiah and 3 others like this.
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,403
    It's trivial to extend the editor to do this yourself.
     
  5. The-Britain

    The-Britain

    Joined:
    Mar 31, 2015
    Posts:
    714
    Then, it's trivial for Unity to implement this. It's a necessary feature that everything from Word to Unreal 4 has. Don't downplay his request.
     
    Sandsoft, zeiksz, Gametyme and 6 others like this.
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,403
    I'm not downplaying it. But Unity takes their own sweet time to actually implement a feature. No matter how trivial it is. So in the meantime you should just implement it yourself.

    You should also put in a feedback request and link it here. There is a slightly greater chance Unity will see the request there then in the forums.
     
  7. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,298
    Make a script called Autosave.cs and put it in a folder named "Editor", with this in it:

    Code (csharp):
    1. using UnityEditor;
    2. using UnityEditor.SceneManagement;
    3. using UnityEngine;
    4.  
    5. [InitializeOnLoad]
    6. public class Autosave
    7. {
    8.     static Autosave()
    9.     {
    10.         EditorApplication.playmodeStateChanged += () =>
    11.         {
    12.             if (EditorApplication.isPlayingOrWillChangePlaymode && !EditorApplication.isPlaying)
    13.             {
    14.                 Debug.Log("Auto-saving all open scenes...");
    15.                 EditorSceneManager.SaveOpenScenes();
    16.                 AssetDatabase.SaveAssets();
    17.             }
    18.         };
    19.     }
    20. }
    It will automatically save any time you press Play. This is the first thing I do in any project. I'm terrible at remembering to manually save.

    And I agree with the OP, this should really be a built-in feature of Unity. It's crazy that they don't save automatically.
     
    Last edited: Jul 19, 2017
  8. sarynth

    sarynth

    Joined:
    May 16, 2017
    Posts:
    16
    Updated for 2017.4 as it says playmodeStateChanged is obsolete.

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using UnityEditor.SceneManagement;
    4.  
    5. [InitializeOnLoad]
    6. public class AutoSave
    7. {
    8.     // Static constructor that gets called when unity fires up.
    9.     static AutoSave()
    10.     {
    11.         EditorApplication.playModeStateChanged += (PlayModeStateChange state) => {
    12.             // If we're about to run the scene...
    13.             if (EditorApplication.isPlayingOrWillChangePlaymode && !EditorApplication.isPlaying)
    14.             {
    15.                 // Save the scene and the assets.
    16.                 Debug.Log("Auto-saving all open scenes... " + state);
    17.                 EditorSceneManager.SaveOpenScenes();
    18.                 AssetDatabase.SaveAssets();
    19.             }
    20.         };
    21.     }
    22. }
    23.  
     
  9. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,211
    Doesn't it already autosave when you press play?
     
  10. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    2,960
    After spending a few days with the latest beta, I think that is expecting too much ;)
     
  11. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,211
    Oh wow. Today I learned my most used key combination (right after CTRL+ALT+DELETE - old statistic trash from WinXP days) is CTRL+S. It's not even a habit I developed from Unity...
     
  12. L42yB

    L42yB

    Joined:
    Dec 20, 2012
    Posts:
    6
    Just wanted to add a slightly better solution here. Add this as a script in the Editor folder and you'll get a menu toggle that will allow users to choose if they want the autosave enabled:


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using UnityEditor.SceneManagement;
    4.  
    5. [InitializeOnLoad]
    6. public class AutoSaveOnRunMenuItem
    7. {
    8.     public const string MenuName = "Tools/Autosave On Run";
    9.     private static bool isToggled;
    10.  
    11.     static AutoSaveOnRunMenuItem()
    12.     {
    13.         EditorApplication.delayCall += () =>
    14.         {
    15.             isToggled = EditorPrefs.GetBool(MenuName, false);          
    16.             UnityEditor.Menu.SetChecked(MenuName, isToggled);
    17.             SetMode();
    18.         };
    19.     }
    20.  
    21.     [MenuItem(MenuName)]
    22.     private static void ToggleMode()
    23.     {
    24.         isToggled = !isToggled;
    25.         UnityEditor.Menu.SetChecked(MenuName, isToggled);
    26.         EditorPrefs.SetBool(MenuName, isToggled);
    27.         SetMode();
    28.     }
    29.  
    30.     private static void SetMode()
    31.     {
    32.         if(isToggled)
    33.         {
    34.             EditorApplication.playModeStateChanged += AutoSaveOnRun;
    35.         }
    36.         else
    37.         {
    38.             EditorApplication.playModeStateChanged -= AutoSaveOnRun;
    39.         }
    40.     }
    41.  
    42.     private static void AutoSaveOnRun(PlayModeStateChange state)
    43.     {
    44.         if (EditorApplication.isPlayingOrWillChangePlaymode && !EditorApplication.isPlaying)
    45.         {
    46.             Debug.Log("Auto-Saving before entering Play mode");
    47.  
    48.             EditorSceneManager.SaveOpenScenes();
    49.             AssetDatabase.SaveAssets();
    50.         }
    51.     }
    52. }
     
  13. Kerosini

    Kerosini

    Joined:
    Aug 16, 2014
    Posts:
    12
    Thanks to all who post there solutions but I think unity has to implement an option to enable autosave when clicking on play. It is just one line of code :)
     
  14. kaledbuilder

    kaledbuilder

    Joined:
    Feb 19, 2013
    Posts:
    1
    I just found out the hard way the 'Save Project' didn't save, well, you know my project so I lost my scenes. I did wonder why there was a separate 'Save Scene' option !

    Anyhow, your script is awesome and why this is not bundled with Unity is anybody's guess.
    Cheers
     
  15. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    3,876
    Rhetorical questions... why? Why is the <esc>:q!<enter> is the quit function in the Vi text editor? Because they designed it this way.
     
    Last edited: Dec 9, 2018
  16. nowletsstart

    nowletsstart

    Joined:
    Jan 1, 2016
    Posts:
    78
    This is very helpful. How can I update this script save a scene automatically whenever a change is made to the scene?
     
  17. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    5,676
    I'm not interested in this feature. I often make changes to experiment, and don't want those saved until I say so specifically. It will be way more irritating to have to disable this feature any time I want to experiment than it is to just tell Unity to save when I want it to save.
     
    Xarbrough, Circool, Zaine7673 and 3 others like this.
  18. soleron

    soleron

    Joined:
    Apr 21, 2013
    Posts:
    87
    I am not interested in this feature one bit. A. most of what we do in Unity is automatically saved. (unlike other engines that you need to save everything) B. It is extremely rare that any of the people I have known and any of the projects I have worked on, large and small, ever needed it.

    Unity is rock solid compared to so many other tool out there. It is the most stable tool I have ever used. Even photoshop crashes more which is known to be rock solid.

    So no, not really needed. Learn to save or create an editor tool that does it for you.
     
    Xarbrough and Joe-Censored like this.
  19. DreamPower

    DreamPower

    Joined:
    Apr 2, 2017
    Posts:
    61
    Nothing you do in Unity is automatically saved. Nothing at all. If you change the scene, you must save. If you change a prefab, you must save. If you change a material or a build setting, you must save. If you shut it down and don't save, you lose every single change you made.

    I often use this fact, by doing an experiment, and if I don't like the results, I just reload without saving.
     
    nowletsstart likes this.
  20. Zaine7673

    Zaine7673

    Joined:
    Feb 15, 2018
    Posts:
    113
    If this is implemented it would be nice to have the option to switch it off.
     
    Xarbrough and Joe-Censored like this.
  21. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    5,676
    There's lots of things that save automatically, which I actually find fairly irritating.
     
    Kiwasi likes this.
  22. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    25,276
    That's not correct. By default, prefabs do automatically save but there's a tickbox for that... :)
     
    Kiwasi and Charles-PAT like this.
  23. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,298
    I still think it should be an option, of course not mandatory, for all those "experimenters" who really want an easy way to lose all their work and don't want to use Undo, saving a copy of a file, source control, backups, or any of the other many ways to revert stuff that you don't want to keep. ;)
    I want what you're smoking. :p
     
    Homicide and Kiwasi like this.
  24. DreamPower

    DreamPower

    Joined:
    Apr 2, 2017
    Posts:
    61
    I change prefabs all the time, the changes only get saved when I do a Save Project.
     
  25. soleron

    soleron

    Joined:
    Apr 21, 2013
    Posts:
    87
    Materials are saved automatically. Project settings too. If you change something on an asset any you apply it, it is saved, etc. etc. so yeah, most things (other than adding something in a scene or moving something, changing lighting etc.) do not get lost if you do not save your scene or project.

    What I would prefer instead of autosave, is something like Photoshop/Office where in a case of a crash your system holds the last state. (Not by autosaving the scene like in 3dsmax and Unreal)
     
    dziastiz and Circool like this.
  26. Rodolfo-Rubens

    Rodolfo-Rubens

    Joined:
    Nov 17, 2012
    Posts:
    1,086
    Same here, also, scriptable objects and most .asset files do not save unless you click save project.