[Unity] Auto-save scene on play (to prevent work-loss on crash)
Hi!
For those of you who use Unity, I am sure you have encountered the frustrations of having Unity crash on you - due to something you did, or due to a bug in Unity. Either way, if you have forgotten to save your scene in a while, you may have lost a significant amount of work. This script will help minimize the risk a bit by auto-saving your scene when you press play (assuming you play-test a lot like me, you will rarely lose any work again). Simply add it to an object that is in all your scenes:
For those of you who use Unity, I am sure you have encountered the frustrations of having Unity crash on you - due to something you did, or due to a bug in Unity. Either way, if you have forgotten to save your scene in a while, you may have lost a significant amount of work. This script will help minimize the risk a bit by auto-saving your scene when you press play (assuming you play-test a lot like me, you will rarely lose any work again). Simply add it to an object that is in all your scenes:
#if UNITY_EDITOR using UnityEngine; using UnityEditor; [InitializeOnLoad] public class AutosaveOnRun: ScriptableObject { static AutosaveOnRun() { EditorApplication.playmodeStateChanged = () => { if(EditorApplication.isPlayingOrWillChangePlaymode && !EditorApplication.isPlaying) { Debug.Log("Auto-Saving scene before entering Play mode: " + EditorApplication.currentScene); EditorApplication.SaveScene(); EditorApplication.SaveAssets(); } }; } } #endif
Thanked by 1hermantulleken
Comments
Me and @tbulford were saying the other day that we wish unity would do this... and now it does
THANK YOU
You should consider releasing this one the unity asset store as a free script.
Thank for this.