[Unity] Auto-save scene on play (to prevent work-loss on crash)


Notice: Undefined variable: GuestHourOffset in /home/nitrogen/public_html_mgsatest/forums/themes/mgsa/class.mgsathemehooks.php on line 281

Notice: Undefined variable: GuestHourOffset in /home/nitrogen/public_html_mgsatest/forums/themes/mgsa/class.mgsathemehooks.php on line 281
edited in Tutorials
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:

#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

Comments


  • Notice: Undefined variable: GuestHourOffset in /home/nitrogen/public_html_mgsatest/forums/themes/mgsa/class.mgsathemehooks.php on line 281
    lol.
    Me and @tbulford were saying the other day that we wish unity would do this... and now it does
    THANK YOU

  • Notice: Undefined variable: GuestHourOffset in /home/nitrogen/public_html_mgsatest/forums/themes/mgsa/class.mgsathemehooks.php on line 281
    Oh nice.

    You should consider releasing this one the unity asset store as a free script.

  • Notice: Undefined variable: GuestHourOffset in /home/nitrogen/public_html_mgsatest/forums/themes/mgsa/class.mgsathemehooks.php on line 281

    Notice: Undefined variable: GuestHourOffset in /home/nitrogen/public_html_mgsatest/forums/themes/mgsa/class.mgsathemehooks.php on line 281
    edited
    @tbulfrod I actually copied from a Unity answers... answer. Just added the #if so that it doesn't compile in actual builds (as it can't and will give an error). I couldn't not share it with you guys. I nearly scratched my own eyeballs out when I lost some work. again. So I opted to instead keep my eyes, and look for a solution instead.

  • Notice: Undefined variable: GuestHourOffset in /home/nitrogen/public_html_mgsatest/forums/themes/mgsa/class.mgsathemehooks.php on line 281
    Wow @Denzil

    Thank for this.
Sign In or Register to comment.

Debug Trace

Info Need to re-index theme cache
Info Need to re-index plugin cache
Info CommentModel->PageWhere()
Info
Comment.Page.30.2280.1: false
Info CommentModel->CachePageWhere()
Info
Comment.Page.30.2280.1: array (
  0 => '2014-07-27 12:02:29',
)
Notice Undefined variable: GuestHourOffset
/home/nitrogen/public_html_mgsatest/forums/themes/mgsa/class.mgsathemehooks.php line 281.
/home/nitrogen/public_html_mgsatest/forums/library/core/class.format.php line 465.
/home/nitrogen/public_html_mgsatest/forums/themes/mgsa/views/discussion/discussion.php line 62.
/home/nitrogen/public_html_mgsatest/forums/themes/mgsa/views/discussion/index.php line 35.
Notice Undefined variable: GuestHourOffset
/home/nitrogen/public_html_mgsatest/forums/themes/mgsa/class.mgsathemehooks.php line 281.
/home/nitrogen/public_html_mgsatest/forums/library/core/class.format.php line 465.
/home/nitrogen/public_html_mgsatest/forums/library/core/functions.render.php line 338.
/home/nitrogen/public_html_mgsatest/forums/themes/mgsa/views/discussion/discussion.php line 66.
Notice Undefined variable: GuestHourOffset
/home/nitrogen/public_html_mgsatest/forums/themes/mgsa/class.mgsathemehooks.php line 281.
/home/nitrogen/public_html_mgsatest/forums/library/core/class.format.php line 465.
/home/nitrogen/public_html_mgsatest/forums/themes/mgsa/class.mgsathemehooks.php line 712.
/home/nitrogen/public_html_mgsatest/forums/applications/vanilla/views/discussion/comments.php line 19.
Notice Undefined variable: GuestHourOffset
/home/nitrogen/public_html_mgsatest/forums/themes/mgsa/class.mgsathemehooks.php line 281.
/home/nitrogen/public_html_mgsatest/forums/library/core/class.format.php line 465.
/home/nitrogen/public_html_mgsatest/forums/themes/mgsa/class.mgsathemehooks.php line 712.
/home/nitrogen/public_html_mgsatest/forums/applications/vanilla/views/discussion/comments.php line 19.
Notice Undefined variable: GuestHourOffset
/home/nitrogen/public_html_mgsatest/forums/themes/mgsa/class.mgsathemehooks.php line 281.
/home/nitrogen/public_html_mgsatest/forums/library/core/class.format.php line 465.
/home/nitrogen/public_html_mgsatest/forums/themes/mgsa/class.mgsathemehooks.php line 712.
/home/nitrogen/public_html_mgsatest/forums/applications/vanilla/views/discussion/comments.php line 19.
Notice Undefined variable: GuestHourOffset
/home/nitrogen/public_html_mgsatest/forums/themes/mgsa/class.mgsathemehooks.php line 281.
/home/nitrogen/public_html_mgsatest/forums/library/core/class.format.php line 465.
/home/nitrogen/public_html_mgsatest/forums/library/core/functions.render.php line 338.
/home/nitrogen/public_html_mgsatest/forums/themes/mgsa/class.mgsathemehooks.php line 715.
Notice Undefined variable: GuestHourOffset
/home/nitrogen/public_html_mgsatest/forums/themes/mgsa/class.mgsathemehooks.php line 281.
/home/nitrogen/public_html_mgsatest/forums/library/core/class.format.php line 465.
/home/nitrogen/public_html_mgsatest/forums/themes/mgsa/class.mgsathemehooks.php line 712.
/home/nitrogen/public_html_mgsatest/forums/applications/vanilla/views/discussion/comments.php line 19.