Developer’s Blog

This free Unity Package includes a very small but useful script to save your Unity scene when you press the "Play" button.

Unity Extension: Auto-Save Scene on Run

Unity Crash

I’m sure it’s happened to every Unity developer with some frequency: You’re there working on something, totally in the flow and getting mad work done, and all of a sudden BAM! Unity crashes. Unfortunately for you, you’ve probably been so “in the zone” that you haven’t saved the last fifteen minutes or more of that work.

Yes, learning to press CTRL-S regularly is the most effective solution possible, but everyone forgets once in a while and the price for forgetting can sometimes be particularly high.

One thing that would help is if Unity could auto-save your scene for you, and there are a number of Unity editor extensions out there to help you, but it seems the majority of them are either more complicated than you need or don’t quite work right. That’s why I wrote the following script, which saves the current scene prior to entering Play mode when you press the Play button. It requires no configuration, and doesn’t distract you from the ultimately most important task of developing your game.  There are no editor windows to keep open, no menu options that need to be clicked.

If this sounds helpful to you, you can either copy the code below into a C# script file somewhere in an Editor folder in your project, or click the button below to download a zipped .unitypackage that can be easily imported into your project.
[download id=”451″ template=”button”]

// *******************************************************
// Copyright 2013 Daikon Forge, all rights reserved under 
// US Copyright Law and international treaties
// *******************************************************
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;

using UnityEngine;
using UnityEditor;

[InitializeOnLoad]
public class OnUnityLoad
{

	static OnUnityLoad()
	{

		EditorApplication.playmodeStateChanged = () =>
		{

			ifEditorApplication.isPlayingOrWillChangePlaymode && !EditorApplication.isPlaying )
			{

				Debug.Log"Auto-Saving scene before entering Play mode: " + EditorApplication.currentScene );

				EditorApplication.SaveScene();
				EditorApplication.SaveAssets();
			}

		};

	}

}

I really do hope you find this as useful as I have!