Pause Menu¶
Since Micro Mayhem is not a multiplayer game, players should be able to pause the game while they are in the middle of their playthrough. In this pause menu can be put a couple of things to adjust to improve the player’s experiencem, think of adjusting the volume, disabling music for example or a simple press of a button to restart the entire level.
Creating Pause Menu Overlay¶
For a pause menu it’s probably best to have a screen which sits on top of the gameplay so the player would still be able to view te gameplay while being in the pause menu, so let’s create a screen overlay.
In order to do this we must first navigate to the HUD script to specify we want to create a new visible overlay.
Within this HUD script we first create a public static void and call it CreatePauseScreen(). In here we create a new gameobject and give it the name PauseScreen. Then, we want to adjust the size of this object with transform.SetParent(Canvas.transform) to let it fill the whole Canvas.
Next, we are giving the overlay a black color with PauseScreen.gameObject.AddComponent
Note: The last RGB code is for adjusting the opacity. Note: Make sure to use the correct namespace (using UnityEngine.UI;, using TMPro;) otherwise your text won’t pop up.
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public static class HUD
{
public static Canvas Canvas { get; private set; }
public static PauseScreen PauseScreen { get; private set; }
public static void Initialize()
{
CreatePauseScreen();
}
private static void CreatePauseScreen()
{
PauseScreen = new GameObject("Pause Screen").AddComponent<PauseScreen>();
PauseScreen.gameObject.AddComponent<RectTransform>();
PauseScreen.transform.SetParent(Canvas.transform);
PauseScreen.transform.SetOffsetAllCorners(0f);
PauseScreen.gameObject.AddComponent<Image>().color = Color.black;
// Opacity
PauseScreen.GetComponent<Image>().material.color = new Color(1.0f, 1.0f, 1.0f, 0.6f);
}
}
Creating Pause Menu Screen¶
Now that we’ve made the overlay we also want something to be displayed on this overlay to let the player know what kind of screen overlay this is.
First, you want to make sure you’re using the correct namespace to prevent future errors.
Now we can start creating 3 new variables (_pause _retryText _backText).
We create a new game object and store it into _pause which will act for displaying pause on the screen. Next we want to adjust the size, position & anchors. Store some text in _pause with _pause.text = “Paused”; and give it a white color. Make sure to write this line of code _pause.enableWordWrapping = false; since it will prevent your sentence/word to split up and create a new line.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.SceneManagement;
public class PauseScreen : MonoBehaviour
{
public new RectTransform transform => base.transform as RectTransform;
private TMP_Text _pause, _retryText, _backText;
void Start ()
{
// Pause text
_pause = new GameObject("Pause text").AddComponent<TextMeshProUGUI>();
RectTransform textRectTransform = _pause.gameObject.GetComponent<RectTransform>();
textRectTransform.SetParent(transform);
textRectTransform.pivot = textRectTransform.anchorMin = textRectTransform.anchorMax = Vector2.one * 0.5f;
textRectTransform.anchoredPosition = Vector2.up * 100f;
_pause.text = "Paused";
_pause.color = Color.white;
_pause.alignment = TextAlignmentOptions.Center;
_pause.fontSize = 100f;
_pause.enableWordWrapping = false;
Creating Retry Button¶
Next we want to create a button which our player can press to be navigated to the main menu screen/try again.
We start with creating a new button as registering it as a new gameobject Button button = new GameObject("Retry Button").AddComponent<Button>();
.
Then you can adjust the size/position of your button again, just like we did with the text above.
Now we want some text in this button which indicates what this button will do when pressed.
Let’s store some text in our created variable _retryText above. Next we write RectTransform retryTextRectTransform = _retryText.gameObject.GetComponent<RectTransform>();
to store our text in a new variable to be able to give it position/scale.
Now we want our text to be glued onto our created button. In order to do this we write retryTextRectTransform.SetParent(button.transform); to set the text equal to the position of our button specified within the parenthesis.
// Retry Button
Button button = new GameObject("Retry Button").AddComponent<Button>();
RectTransform buttonTransform = button.gameObject.AddComponent<RectTransform>();
Image buttonImage = button.gameObject.AddComponent<Image>();
buttonImage.transform.localScale = new Vector2(1.5f,0.5f);
buttonTransform.SetParent(transform);
buttonTransform.pivot = buttonTransform.anchorMin = buttonTransform.anchorMax = Vector2.one * 0.5f;
buttonTransform.anchoredPosition = Vector2.up * -100f;
_retryText = new GameObject("Retry text").AddComponent<TextMeshProUGUI>();
RectTransform retryTextRectTransform = _retryText.gameObject.GetComponent<RectTransform>();
_retryText.text = "Retry";
retryTextRectTransform.SetParent(button.transform);
retryTextRectTransform.pivot = retryTextRectTransform.anchorMin = retryTextRectTransform.anchorMax = Vector2.one * 0.5f;
retryTextRectTransform.anchoredPosition = Vector2.up;
_retryText.color = Color.black;
_retryText.alignment = TextAlignmentOptions.Center;
_retryText.fontSize = 50f;
_retryText.enableWordWrapping = false;
Create Unpause Button¶
Apart from having a retry button, we also want to be able to unpause the game to resume your gameplay.
You can create a new button just the way you did here in Creating Retry Button.
// Unpause button
Button backButton = new GameObject("Retry Button").AddComponent<Button>();
RectTransform backButtonTransform = backButton.gameObject.AddComponent<RectTransform>();
Image backButtonImage = backButton.gameObject.AddComponent<Image>();
backButtonImage.transform.localScale = new Vector2(1.5f,0.5f);
backButtonTransform.SetParent(transform);
backButtonTransform.pivot = backButtonTransform.anchorMin = backButtonTransform.anchorMax = Vector2.one * 0.5f;
backButtonTransform.anchoredPosition = Vector2.up * -5f;
_backText = new GameObject("Back text").AddComponent<TextMeshProUGUI>();
RectTransform backTextRectTransform = _backText.gameObject.GetComponent<RectTransform>();
_backText.text = "Back";
backTextRectTransform.SetParent(backButton.transform);
backTextRectTransform.pivot = backTextRectTransform.anchorMin = backTextRectTransform.anchorMax = Vector2.one * 0.5f;
backTextRectTransform.anchoredPosition = Vector2.up;
_backText.color = Color.black;
_backText.alignment = TextAlignmentOptions.Center;
_backText.fontSize = 50f;
_backText.enableWordWrapping = false;
Button functions¶
At the end of this class we want to register an on click function to specify what to do when the button is pressed.
Make sure to write gameObject.SetActive(false);
this will cause your overlay to not appear on the screen, which you want when you’re playing the game.
Here in the button functions we want to set the gameobject active again to trigger our overlay being displayed. In order to do this we begin with creating a new function called Retry. In here you want to acces the SceneManager which you will be using to switch between your different scenes. In here you also want to set the Time.timeScale = 1f; to 1 again since this will unfreeze the game.
Next in Resume() you want to unfreeze the game with Time.timeScale = 1f; and hide the pause menu overlay with gameObject.SetActive(false);.
Last we want to create a PauseGame() function to let the player actually able to pause the game. In here we want the gameplay to freeze when this function is triggered, we dot his with Time.timeScale = 0f; and make the pause screen overlay pop up with gameObject.SetActive(true);.
button.onClick.AddListener(Retry);
backButton.onClick.AddListener(Resume);
gameObject.SetActive(false); // false
}
public void Retry()
{
SceneManager.LoadScene(0);
Time.timeScale = 1f;
}
public void Resume()
{
Time.timeScale = 1f;
gameObject.SetActive(false);
}
public void PauseGame(bool pause)
{
// pauses game
Time.timeScale = 0f;
gameObject.SetActive(true);
}
}
Adjustments in player script¶
Wihtin the player update function we want to be able to pause the game by pressing Escape. We will specify this here.
We create a new if statement to check whether we pressed the escape button with if (Input.GetKey(KeyCode.Escape)). If this statement is true we want the pause screen overlay to appear, in order to do this we write HUD.PauseScreen.PauseGame(true);.
And that’s it!
private void Update()
{
if (Input.GetKey(_bindings.shoot))
{
_weapon.TryFire(this);
}
// Pauses game
if (Input.GetKey(KeyCode.Escape))
{
HUD.PauseScreen.PauseGame(true);
Debug.Log("Game was paused");
}
}