Pausing the game

As compelling as your next game will be, you should always let players pause it for a short break. In this recipe, we will implement a simple and effective pause screen, including controls for changing the display's quality settings.

Getting ready

For this recipe, we have prepared a package named BallShooterGame containing a playable scene. The package is in the 0423_10_01 folder.

How to do it...

To pause your game upon pressing the Esc key, perform the following steps:

  1. Import the BallShooterGame package into your project and open the level named RoomLevel (from the Scenes folder).
  2. Add the following C# Script (PauseGame) to First Person Controller:
    // file: PauseGame.cs
    using UnityEngine;
    using System.Collections;
    
    public class PauseGame : MonoBehaviour {
      public bool expensiveQualitySettings = true;
      private bool isPaused = false;
      private void Update() {
        if (Input.GetKeyDown(KeyCode.Escape)) {
          if (isPaused)
            ResumeGameMode();
          else
            PauseGameMode();
        }
      }
      private void ResumeGameMode() {
        Time.timeScale = 1.0f;
        isPaused = false;
        Screen.showCursor = false;
        GetComponent<MouseLook>().enabled = true;
      }
      private void PauseGameMode() {
        Time.timeScale = 0.0f;
        isPaused = true;
        Screen.showCursor = true;
        GetComponent<MouseLook>().enabled = false;
      }
      private void OnGUI() {
        if(isPaused)
          PauseGameGUI();
      }
      private void PauseGameGUI(){
        string[] names = QualitySettings.names;
        string message = "Game Paused. Press ESC to resume or select a new quality setting below.";
        GUILayout.BeginArea(new Rect(0, 0, Screen.width, Screen.height));
        GUILayout.FlexibleSpace();
        GUILayout.BeginHorizontal();
        GUILayout.FlexibleSpace();
        GUILayout.BeginVertical();
        GUILayout.Label(message, GUILayout.Width(200));
        for (int i = 0; i < names.Length; i++) {
          if (GUILayout.Button(names[i],GUILayout.Width(200)))
            QualitySettings.SetQualityLevel(i, expensiveQualitySettings);
        }
        GUILayout.EndVertical();
        GUILayout.FlexibleSpace();
        GUILayout.EndHorizontal();
        GUILayout.FlexibleSpace();
        GUILayout.EndArea();
      }
    }
  3. When you play the scene, you should be able to pause/resume the game by pressing the Esc key.
    How to do it...

How it works...

The value of the Boolean flag variable isPaused is used to decide what to do when Esc is pressed. If the game is paused, it resumes by calling ResumeGameMode(). If the game was not paused, it will be paused by calling the PauseGameMode() method.

To pause the game, we disable the MouseLook component, reveal the mouse cursor, and set the value of the Time.timeScale variable to 0—this sets the frame rate to 0, so if all logic is frame-based, effectively the game is paused. When the game is in pause mode (isPaused is true), OnGUI() will call PauseGameGUI() to use the opportunity to reveal the mouse cursor and display GUI buttons that activate different levels of quality settings.

To resume the game, we hide the mouse cursor, enable the MouseLook component, and set Time.timeScale back to 1.0.

There's more...

You can always add more functionality to the Pause screen by displaying sound volume controls, save/load buttons, and so on.

Learning more about quality settings

Our code for changing quality settings is a slight modification of the example given by Unity's documentation. If you want to learn more about the subject, check it out at docs.unity3d.com/Documentation/ScriptReference/QualitySettings.html.

See also

  • The Implementing slow motion recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.113.163