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.
For this recipe, we have prepared a package named BallGame
containing a playable scene. The package is in the 1362_11_01
folder.
To pause your game upon pressing the Esc key, follow these steps:
BallGame
package into your project and, from the Project view, open the level named BallGame_01
.ball
in Prefabs
folder, and save the scene.QualityPanel
.QualitySlider
.QualityLabel
. Also, from the Inspector view, Rect Transform, change its Pos Y to -25.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]; } }
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.
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.
You can always add more functionality to the pause screen by displaying sound volume controls, save/load buttons, and so on.
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.
3.144.82.21