Every desktop window system has its own conventions and common user interface standards. In 1987, IBM created the Common User Access standard which strongly influenced early versions of Windows and several other window managers for a variety of operating systems. This standard has never been fully accepted in its entirety though it continues to influence desktop designers to one degree or another still today.
Because the CUA has been around for so long, we’ve all pretty much gotten used to its general form and function. Thus, when developing a new application or game, it can be easy to forget these common UI elements actually are not in our application by magic. We actually have to put them there.
My first reminder of this doh! moment was when I finally compiled my game for the first time in standalone mode and executed it. Of course, I wanted to experience it in its fullest glory, so I set it for full screen mode. I played my game, patting myself on the back for all its wonderousness, and then discovered I was doomed to play it forever as it was, or reboot my computer. I had not given myself an exit method!
Fortunately, in Unity this is a super quick fix using techniques we already know. We’ve already setup our game to monitor for keystrokes, so all we really need to go with that is the Unity method to close an application. This turns out to be the method “Application.Quit().” We also already have a GameManager game object where we check for the “R” keypress which restarts our game. Seems to me this would be just as good a place to check for an exit request as well.
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
private bool gameOver = false;
public bool GameOver
{
get { return gameOver; }
set
{
gameOver = value;
gameLive = !value;
}
}
private bool gameLive = false;
public bool GameLive
{
get { return gameLive; }
set { gameLive = value; }
}
void Update()
{
if ( Input.GetKeyDown(KeyCode.R)
&& gameOver )
{ SceneManager.LoadScene("Game"); }
if ( Input.GetKeyDown(KeyCode.Escape) )
{ Application.Quit(); }
}
}
On the last line of our code above, once we hit the ‘Escape’ key we invoke Application.Quit()
and walla! we’re done… Well, almost.
If you’ve been following along, you know we recently added a main menu page to our game. That way the player gets a moment before just diving into the game. This was implemented by creating a new scene, just for this splash page. Right now, this scene has only one exit point which is to click the button to start the game. If we want to exit the game from this scene as well, we need to add the same code to its script,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MainMenu : MonoBehaviour
{
void Update()
{
if ( Input.GetKeyDown(KeyCode.Escape) )
{ Application.Quit(); }
}
public void LoadGame()
{ SceneManager.LoadScene("Game"); }
}
The LoadGame()
method is called by the button press and is set in the Inspector in the OnClick() method of the Button Component on the Canvas. Application.Quit()
is called as soon as we hit the escape key. We are no longer trapped and are free to fullscreen!