The Escape Button is a Feature
We have a basic build, but there are no means to break the flow of the game, nor do we have an option to quit the game(If we were to build and run the game in fullscreen, then the player would have to rely on the system to forcefully quit the game).
Objective : Create a pause menu that gives the player an option to pause or quit the game.
We’ll be building upon the UI articles mentioned here(Article-1, Article-2).
For starters, I created a UI Image object, then added two buttons as children to this Image object. Which looks like the following,
We now have a basic pause menu UI with resume and quit application buttons. But these buttons have yet to be configured to perform the desired action. To bring these buttons to life, we’ll have to configure the UIManager and the GameManager to perform actions that reflect when the buttons are interacted with.
So let’s start by creating a simple behavior where, when the player presses the “Escape” key, the PauseMenu UI Shows up.
By default, we’ll have the PauseMenu game object toggled OFF. The UIManager will contain a method to alter the status of this game object(toggle ON or OFF).
Now let’s create a Pause and Resume behavior.
This is used to control the flow of time in our game. Depending on the value associated with it, we can slow down the game or even increase the speed. Mainly used to create slow-motion effects. But can also be used to stop time in-game. Setting timeScale to 0 stops time, creating an illusion that the game is paused.
Note: This only works for time-independent functions. Update() will continue to be called every frame. To learn more about this click here.
Using this, we can add the following behavior,
Now, let’s move on to adding a quit application behavior. Unity makes this easy as the process to quit application is as simple as doing the following,
With this, we have all the behaviors ready. All that’s left is to link them with their corresponding behaviors and have the pause menu show up when the Escape key is pressed.
Now all that’s left is to link the behaviors with the button presses. Done by creating an On Click event that contains our UIManager through which we can assign the associated button with its associated behavior. Implying that when the button is clicked the associated behavior is executed.
This gives us,
Thank you for reading. More to come!!!