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 BallGame containing a playable scene. The package is in the 1362_11_01 folder.

How to do it...

To pause your game upon pressing the Esc key, follow these steps:

  1. Import the BallGame package into your project and, from the Project view, open the level named BallGame_01.
  2. In the Inspector, create a new tag Ball, apply this tag to prefab ball in Prefabs folder, and save the scene.
  3. From the Hierarchy view, use the Create drop-down menu to add a Panel to the UI (Create | UI | Panel). Note that it will automatically add it to the current Canvas in the scene. Rename the panel QualityPanel.
  4. Now use the Create drop-down menu to add a Slider to the UI (Create | UI | Slider). Rename it QualitySlider.
  5. Finally, use the Create drop-down menu to add a Text to the UI (Create | UI | Text). Rename it QualityLabel. Also, from the Inspector view, Rect Transform, change its Pos Y to -25.
  6. Add the following C# script PauseGame to First Person Controller:
    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;
    
    public class PauseGame : MonoBehaviour {
      public GameObject qPanel;
      public GameObject qSlider;
      public GameObject qLabel;
      public bool expensiveQualitySettings = true;
      private bool isPaused = false;
    
      void Start () {
        Cursor.visible = isPaused;
        Slider slider = qSlider.GetComponent<Slider> ();
        slider.maxValue = QualitySettings.names.Length;
        slider.value = QualitySettings.GetQualityLevel ();
        qPanel.SetActive(false);
      }
    
      void Update () {
        if (Input.GetKeyDown(KeyCode.Escape)) {
          isPaused = !isPaused;
          SetPause ();
        }
      }
    
      private void SetPause(){
        float timeScale = !isPaused ? 1f : 0f;
        Time.timeScale = timeScale;
        Cursor.visible = isPaused;
        GetComponent<MouseLook> ().enabled = !isPaused;
        qPanel.SetActive (isPaused);
      }
    
      public void SetQuality(float qs){
        int qsi = Mathf.RoundToInt (qs);
        QualitySettings.SetQualityLevel (qsi);
        Text label = qLabel.GetComponent<Text> ();
        label.text = QualitySettings.names [qsi];
      }
    }
  7. From the Hierarchy view, select the First Person Controller. Then, from the Inspector, access the Pause Game component and populate the QPanel, QSlider, and QLabel fields with the game objects QualityPanel, QualitySlider, and QualityLabel respectively, as shown in the following screenshot:
    How to do it...
  8. From the Hierarchy view, select QualitySlider. Then, from the Inspector view, Slider component, find the list named On Value Changed (Single), and click on the + sign to add a command.
  9. Drag the First Person Controller from the Hierarchy view into the game object field of the new command. Then, use the function selector to find the SetQuality function under Dynamic float (No Function | PauseGame | Dynamic float | SetQuality), as shown in the following screenshot:
    How to do it...
  10. When you play the scene, you should be able to pause/resume the game by pressing the Esc key, also activating a slider that controls the game's quality settings.
    How to do it...

How it works...

Pausing the game is actually an easy, straightforward task in Unity: all we need to do is set the game's Time Scale to 0 (and set it back to 1 to resume). In our code, we have included such a command within the SetPause() function, which is called whenever the player presses the Esc key, also toggling the isPaused variable. To make things more functional, we have included a GUI panel featuring a QualitySettings slider that is activated whenever the game is paused.

How it works...

Regarding the behavior for the QualitySettings slider and text, their parameters are adjusted at the start based on the game's variety of quality settings, their names, and its current state. Then, changes in the slider's value redefine the quality settings, also updating the label text accordingly.

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 QualitySettings

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 out http://docs.unity3d.com/ScriptReference/QualitySettings.html.

See also

Refer to the Implementing slow motion recipe in this chapter for more information.

..................Content has been hidden....................

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