Unity – How to Show a Confirmation Dialog

Updated: September 23, 2021 By: A Goodman Post a comment

In order to show a confirmation dialog in Unity, you can use the EditorUtility.DisplayDialog() method. Before calling it, you need to add:

using UnityEditor;

Example

The code:

 void ExitGameConfirmation()
  {
    bool decision = EditorUtility.DisplayDialog(
      "Exit Game", // title
      "Are you sure want to exit the game?", // description
      "Yes", // OK button
      "No" // Cancel button
    );

    if(decision){
      Debug.Log("Exit game");
    } else {
      Debug.Log("Continue playing");
    }
}

Screenshot:

If you hit the “Yes” button, you will see:

Exit game

If you click on the “No” button or just close the dialog without doing anything, you will see:

Continue playing

That’s it. Happy coding.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Related Articles