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.