A Simple Save On Play Script

I use Visual Studio a lot and one of the things I came to rely on is the project saving everytime I run it.

I wanted the same for Unity scenes so I created a simple little script.

To use it:

  1. Add the script to your scene.

  2. Go to Edit–>Project Settings–>Script Execution Order and set your “SaveOnPlay” script to execute first. (This is so it saves before any of your code runs that may modify the scene programmatically.)

Note: You’ll want to remove/disable this script prior to releasing you game.

That’s it. Let me know what you think.

The script:

using UnityEngine;
using UnityEditor;
using System.Collections;

public class SaveOnPlay : MonoBehaviour {

	void Awake()
	{
		EditorApplication.playmodeStateChanged += HandlePlayModeStateChanged;
	}
	
	void HandlePlayModeStateChanged()
	{
		EditorApplication.SaveScene();
		EditorApplication.playmodeStateChanged -= HandlePlayModeStateChanged; //don't fire event on exiting play
		
	}
}
2 Likes

Dude, thank you so much. I too have been spoiled by save-on-run programs like MVS and I have lost a lot of work in unity because of it crashing. This is awesome.

Thank you lord!

Rather than manually removing it from builds, you could stick it in a #if block.

#if UNITY_EDITOR
    // Editor specific stuff in here
#endif

Nice snippet! Thanks a lot!