Alert message that postpones entering play mode?

Hey all,

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.

Might not be possible, but figured I’d ask

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.

Though you can’t use this to delay entering play mode as it’s just a delegate. You’d have to add your own button/shortcut for that.

I don’t have it handy to test but I thought you could throw an exception out of there to stop the choo choo train… perhaps I’m misremembering it.

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)?

Actually you could probably just use any one of the EditorUtility methods for such a purpose: https://docs.unity3d.com/ScriptReference/EditorUtility.DisplayDialog.html

I have seen people do it, I don’t know how to do it. Some googling might yield you the answers.

1 Like

Thanks, I’ll do some testing this evening and see if I can get it to postpone entering play with such a dialogue

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();
      }
    }
  }
}

Edit: Solved in subsequent post here .

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?

1 Like

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! :smile:

2 Likes

Awesome, thanks for this, really appreciate it. Made my life easy :stuck_out_tongue:

1 Like