Although it breaks the player’s immersion, one of the hallmarks of a game is the ability to pause and resume the game whenever they want to. This helps the player “recharge” while not resetting the current game state from the point of pausing the game. Even though there are different ways to implement a pause menu into the game, one simple way is to literally stop time.
Getting Started
Pause menu can be as simple as a black screen with simple buttons for the player to push to something as complex as a video playing in the background. In this case, we’re going for a more simplistic pause menu as it requires no external resources outside from what Unity will provide.
Creating the Background
First, we need to create an image to encompass the background of our pause menu. To achieve this, in the Hierarchy, we’ll add an image from the UI category, which in turn, will generate a Canvas.
After creating the canvas, select the Canvas and view its properties in the Inspector. We want to change the Scale Factor to Scale with Screen Size in order to fill the entire screen regardless of the player’s resolution.
Next, we’ll set the anchor point to stretch, which is located in the lower right hand corner of the Anchor Presets. This will allow us to stretch the image to cover the entire screen.
By setting all the image’s position to zero and changing the color to black, we’ve completed the background needed for our pause menu!
Adding Text and Buttons
Similar to adding an Image to serve as our background, we’ll be adding a Text object to serve as the title and Buttons to serve as the options the player can choose from.
Even though you can customized the size of the buttons, it is recommended to keep them relativity the same size for uniformity.
The Simple yet Complex Logic behind the Pause Menu
The code behind the pause menu is very simple. By changing the time scale, we essentially can stop and start anything from moving.
Therefore, just this one line of code can allow us to stop and start “everything” when paused:
Time.timeScale = 0; //Stops everything that are time dependent
Time.timeScale = 1; //Starts everything that are time dependent
The complexity of the Pause Menu is when do we allow the player to pause the game. They shouldn’t be able to pause the game whenever there’s a cutscene, but should at any other time. As a result, we have to incorporate this into the placement of when can the player pause the game.
Regardless, because the Pause Menu is incorporating both a UI and the game state in our scene, I’ve implemented its logic in the UIManager and the UIManager.
The UIManager determines what the buttons do by creating three public methods we will need to assign to the buttons created earlier:
While the GameManager determines when the player can pause and when the game is paused or unpaused.
Lastly, because we don’t want the player to pause the game whenever there’s a cutscene, we need to change the Boolean that allows the player to pause the game to false. Luckily, due to the fact that all of the cutscenes are activated via script, we can achieve this by setting the Boolean to false after enabling the cutscene.
Assigning the Button’s Logic
Finally, it’s time to give the buttons we’ve created some logic. Onthe Button’s properties in the Inspector panel, there’s an option near the bottom that says On Click{}. This window determines what the Button will do whenever the player presses it.
By clicking on the + symbol in the lower right hand corner of this section, you can add a game object to it (in this case, the UI Manager) and select the public function we want to execute when the player clicks on the button.
Do this for all three and viola! You have a functional pause menu!