Auto Save for Unity

This article was originally posted on Random Bits. Check it out for more Unity related content.What can you do when a team member suffers a recurring Unity editor crash, losing all level design work ? My answer was to write a small and helpful editor extension - auto save! (and then blog about it).

TL;DR

This post describes a simple solution for implementing auto save in Unity that saves the currently open scene every 5 minutes (configurable).The code is available for easy consumption in a few ways:

The code is imported and works out of the box, import / paste it into your Unity project and you're good to go.

Problem

It started a few days ago: a member of our team started experiencing occasional Unity editor crashes a few times daily. We do not know the exact reason for crashes, but we suspect it may be related to memory issues combined with voodoo magic. No matter what the root cause was, these crashes caused real damage in lost data (game levels) which we could not afford having.In order to keep lost work to a minimum, I suggested to implement a basic auto save solution, so at least we can go back to a backup in case the editor crashes.

Solution - AutoSave

The solution uses pretty simple editor scripting to the rescue. The process can be described in 3 main steps:

Built In AutoSave

It should be noted that apparently Unity does autosave the current scene every time you enter play mode. If this is enough for you (for example - the game crashed during play mode), a copy of the scene can be found in YourProject/Temp/__EditModeScene.

Conclusion

The code in this post helps ensuring no scene data is lost when experiencing editor crashes. I deliberately kept it short & simple so it can be easily "digested". Autosaving can be further visited by adding any of the following:

6 Comments

Recommended Comments

Hey that's neat. I've crashed the editor a few times myself, and been pretty annoyed at losing work, even though I had actually saved with ctrl+s. Does this code also fix that issue where Unity sometimes doesn't actually perform a save unless you close the editor gracefully?

0

Share this comment

Link to comment

Share this comment

Link to comment

ferrous - ctrl+s saves the scene but not the project, which means you can lose prefab changes and the like. You have to use File -> Save Project to ensure that. And I don't believe it has a keyboard shortcut.

0

Share this comment

Link to comment

Hey that's neat. I've crashed the editor a few times myself, and been pretty annoyed at losing work, even though I had actually saved with ctrl+s. Does this code also fix that issue where Unity sometimes doesn't actually perform a save unless you close the editor gracefully?

I am not familiar with the issue that Unity does not properly save the scene, so my guess is that there's 50/50 chance this code will help in that case.

Share this comment

Link to comment

ferrous - ctrl+s saves the scene but not the project, which means you can lose prefab changes and the like. You have to use File -> Save Project to ensure that. And I don't believe it has a keyboard shortcut.

Actually this solution can also save the project when saving the scene. I did not add that part to the code here though.