Unity3D: Reusable Confirmation Box pre uGUI

I recently wrote a small piece of functionality that I was surprisingly proud of: a confirmation box.  In Dojo Storm, when the player tries to exit the game, I wanted to give them an "Are you sure?" prompt, in case it was selected in error.  Rather than make the confirmation box a hardcoded exit confirmation, I decided to have a little fun with it and make it as generic and reusable as possible.  What resulted is a confirm box with a custom title and a custom "Yes" button function.

Code Download

This code is somewhat involved, so if having the entire file would make it easier to follow along, feel free to download it here.  Note that the file has a dependency on another class, GameScreen, so in order to use this code in your own game you would have to modify it a bit.


First, as usual, we initialize our variables.  I've included all the variables in the screenshot, but the two important ones are _title and _confirmationFunction.

_title will be used to determine what title to show (in my case, "Really quit?").  _confirmationFunction is the function that will be called when the player presses "Yes".

C# Actions

_confirmationFunction is what is called an Action Delegate in C#.  It is basically a variable that is a placeholder for a function.  Using just the identifier "Action" means that the function will have no parameters and no return value.

Pass In Variables

Now that we have _title and _confirmationFunction set up, we need some way for other areas of code to set them.  We don't want to simply make them public - that is just bad practice.  How I did it is by creating a "Setup" function that the caller must call before displaying the confirm box.  

How the GUI works in Dojo Storm is that each GUI screen has its own object, and they are turned on and off depending on whether or not they are currently shown.  This method makes the "Setup" function ideal.  You may want to do things differently in your game, but one way or another you need some way for a caller of the confirm box to set the title and confirm function of the box.  Here's my code:


The next thing we want to do is actually display the confirmation box.  This will be different for every game, but here is how I do it in Dojo Storm:

The important take-away here is that wherever we display our title, we are using the _title variable.  This is passed in by whatever is calling the confirmation box, so we want it to be dynamic.

Handle Input

This is where things get a tiny bit tricky.  When the user selects the "Yes" option (or whatever equivalent you use for your confirm box), we want to call whatever function was originally passed in.  Because we're using an Action, this is super simple.

When the player selects the "Yes" option, we simply call _confirmationFunction(), and it will run the logic in whatever function the caller passed in.

That's it for setting up the confirm box!  The only step remaining is to pass everything in from the caller.

Pass in the Function

Now that the confirm box is all set up, we have to actually use it.  I have an InGameMenu class which has a list of items, one of which is "Exit", that the player can select.  When they select Exit, I want to show the confirm box, and then if they confirm I want to quit out of the application.  For debug purposes, I want to also log "Quit", because Application.Quit() doesn't work when you're running inside the editor.

So first, here is my Quit() function.  Note that it has no parameters, and returns no values.  This is important, because the Action that we set up in the Confirm box accepts no parameters and has no return values.

Because of the way I have the GUI system set up in Dojo Storm, there is a GUIState static class that handles all things GUI-related.  So when the player selects Exit, I tell GUIState to show the confirm dialog, passing along my title and the Quit function.

And then GUIState handles setting up the confirm box (calling "Setup" from earlier) and then displaying the box.

And that's it!  When the player opens the in-game menu and selects exit, they are prompted to confirm that they really want to quit.  When they press yes, the Quit() function is called.  A similar method could be used to pass in any function to be called if the player selects "No", but because of the way the GUI system works in Dojo Storm, that is not necessary; I simply show the previous screen.

It would have been a lot quicker to simply write a "Confirm Quit" dialog box without passing in the title or a confirmation function, but then if I need a confirmation dialog again in the future, I'd have to write another one from scratch.  With this generic one, I can reuse it in as many places as I like, which could potentially save a lot of time down the line.