Was curious if anybody has come up with a way to temporarily postpone entering play mode when it is clicked. I was wanting to have an alert dialogue come up if changes in a custom tool were unsaved when clicking play mode and give the user the option to save the changes before it actually enters play mode.
Thats something you’ll have to add to the game, you can’t really make alert thing were unity will display it, you’ll have to imply that in your scripts, maybe in the awake for unsaved changes, as far as I know
I think you can hook into the PLAY button intent before it happens in order to load a custom scene, but I haven’t looked up the API in forever… it’s somewhere in the editor space of course, and it might give you an opportunity to do what you want check-wise.
I was thinking that adding my own play button might be the way to go so then I could have my own event sequence lined up before play actually executes. Is it possible to do something like hide existing editor buttons (in this case, the play button)?
Based on the advice given, the code below works, but appears to still execute 1 frame before it exits. I tried adding a Debug.Break() call before the prompt to solve that, but realized there’s no Debug.Unbreak() or apparent way (at least to me) to resume if continuing. Maybe someone else knows how?
using UnityEditor;
[InitializeOnLoadAttribute]
public static class PlayModeConfirmation
{
static PlayModeConfirmation()
{
EditorApplication.playModeStateChanged += PlayModeStateChanged;
}
private static void PlayModeStateChanged(PlayModeStateChange state)
{
if (state == PlayModeStateChange.EnteredPlayMode)
{
if (!EditorUtility.DisplayDialog("Enter Play Mode", "Are you sure?", "Yes", "Cancel"))
{
EditorApplication.ExitPlaymode();
}
}
}
}
OP’s use case is analogous to the whole “Apply changes?” popup that you get if you press PLAY without finishing a Texture2D import setting change by applying it… I wonder if there’s a clue related to that mechanism?
Solved - changed “EnteredPlayMode” to “ExitingEditMode” and it works! Credit to this thread for the idea. It is curious how the “EditorApplication.ExitPlaymode()” call works despite not having entered it yet… but if it works, it works!