Auto Save in Unity

What can you do when a team member suffers a recurring Unity editor crash, losing all their precious level design work ? My answer was to write a small and helpful editor extension – auto save ! (and then blog about it of course).

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:

  • GitHub – Also updated in case i get any feedback / future posts
  • Gist (copy & paste directly into your project)

Import the code into your project and you should be good to go ! In case you run into any issue, be sure to leave a comment or shoot me an email.

Why do I need this ?!

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 – Auto Save

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

  1. Hook a delegate to EditorApplication.update.
  2. In this method, check if the scene should be saved (if the configured time has elapsed. The default is 5 minutes).
  3. In case we need to save, generate a new unique name for the scene and save it to disk.

In order to have the code up and running when you launch the editor, the class is marked with the [InitializeOnLoad] attribute and initialization is done in its static constructor.

Show Me the Code

This is the complete code, you can paste it into your project:

using System;
using System.IO;
using System.Globalization;
using UnityEditor;
using UnityEngine;

[InitializeOnLoad]
public class AutoSaveScene
{
	private const string SAVE_FOLDER = "Editor/AutoSaves";

	private static System.DateTime lastSaveTime = System.DateTime.Now;
	private static System.TimeSpan updateInterval;

	static AutoSaveScene()
	{
		EnsureAutoSavePathExists();

		// Register for autosaves.
		// Change this number to modify the autosave interval.
		RegisterOnEditorUpdate(5);
	}

	public static void RegisterOnEditorUpdate(int interval)
	{
		Debug.Log ("Enabling AutoSave");

		updateInterval = new TimeSpan(0, interval, 0);
		EditorApplication.update += OnUpdate;
	}

	/// 
	/// Makes sure the target save path exists.
	/// 
	private static void EnsureAutoSavePathExists()
	{
		var path = Path.Combine(Application.dataPath, SAVE_FOLDER);

		if (!Directory.Exists(path))
		{
			Directory.CreateDirectory(path);
		}
	}

	/// 
	/// Saves a copy of the currently open scene.
	/// 
	private static void SaveScene()
	{
		Debug.Log("Auto saving scene: " + EditorApplication.currentScene);

		EnsureAutoSavePathExists();

		// Get the new saved scene name.
		var newName = GetNewSceneName(EditorApplication.currentScene);
		var folder = Path.Combine("Assets", SAVE_FOLDER);

		EditorApplication.SaveScene(Path.Combine(folder, newName), true);
		EditorApplication.SaveAssets();
	}

	/// 
	/// Helper method that creates a new scene name.
	/// 
	private static string GetNewSceneName(string originalSceneName)
	{
		var scene = Path.GetFileNameWithoutExtension(originalSceneName);

		return string.Format(
			"{0}_{1}.unity",
			scene,
			System.DateTime.Now.ToString(
			"yyyy-MM-dd_HH-mm-ss",
			CultureInfo.InvariantCulture));
	}

	private static void OnUpdate()
	{
		if ((System.DateTime.Now - lastSaveTime) >= updateInterval)
		{
			SaveScene();
			lastSaveTime = System.DateTime.Now;
		}
	}
}

Built In Auto Save

It should be noted that apparently Unity does auto save 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”. Auto saving can be further visited by adding any of the following:

  • Configuration – Allow controlling auto save (turning on/off, setting time interval) from an editor window or menu item.
  • Capping number of auto saves – Nobody really needs 50 copies of the open scene; instead, only 1 or 2 copies can be saved and recycled on every new save.
  • New save triggers – The current code saves every X minutes. The code can be adapted to save under different scenarios.

Resources

This entry was posted in Unity and tagged , , , . Bookmark the permalink.

3 Responses to Auto Save in Unity

  1. Pingback: Introduction to Unity: Getting Started – Part 2/2 - Joe Pimentel WebBlog

  2. Pingback: Introduction to Unity

  3. Pingback: Introduction to Unity: Getting Started – Part 2/2 – Nhuan Quang

Leave a Reply

Your email address will not be published. Required fields are marked *