Article directory
Preparation
- Unity version: 2019.2.14
- Texture Material: Default Material
- Others: None
Program default
- Set the m_isPause flag, and then all actions need to judge in advance whether the current flag is in a paused state, and if so, stop the function. (Option 1 is suitable for timing games)
- When entering the pause state, set Time.timeScale = 0, the time in the game always needs to be multiplied by this parameter, so setting it to 0 means the pause state, this parameter can also be used to slow down or fast forward. (Option 2 needs to be reset to Time.timeScale = 1 when exiting the pause)
UI design
This blog post mainly focuses on realizing functions, and the UI design is simple
-
Look at the final effect:
-
Create a Canvas and name it UI
-
Add an Empty object under Canvas, named PauseCavans, to store all UI components of the pause menu
-
Set the anchor of PauseCanvas to expand around
-
Add a Panel under the pause function for the grayscale background when paused, and set its anchor as a surrounding expansion shape> where Color can set the background color
-
添加PauseMenu/OptionMenu/RestartMenu/QuitGameMenu分所
MenuBackGround is a beautified menu bar background, or not
-
There are three buttons under PauseMenu, and after clicking, you can switch to the three columns of option/restart/quitgame
-
The main contents are as follows
Attachment at the end of the article
Code
- master manager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
///
/// </summary>
public class Manager : MonoBehaviour
{
// Single instance
public static Manager Instance
{
get;
protected set;
}
public bool m_IsPaused;
private void Start()
{
Instance = this;
m_IsPaused = false;
}
private void Update()
{
if (Input.GetButtonDown("Cancel"))
{
PauseMenu.Instance.SwitchDisplay();
}
}
// public void DisplayCursor(bool display)
// {
// }
}
- pause function
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
#if UNITY_EDITOR
using UnityEditor;
#endif
/// <summary>
///
/// </summary>
public class PauseMenu : MonoBehaviour
{
public static PauseMenu Instance {
get; protected set; }
public GameObject optionMenu;
public GameObject restartMenu;
public GameObject quitGameMenu;
public GameObject pauseMenu;
public void Start()
{
Instance = this;
gameObject.SetActive(false);
}
public void SwitchDisplay()
{
bool isPaused = Manager.Instance.m_IsPaused;
Manager.Instance.m_IsPaused = !isPaused;
gameObject.SetActive(!isPaused);
Time.timeScale = Manager.Instance.m_IsPaused ? 0 : 1;
}
// UI button
public void ShowPauseMenu()
{
pauseMenu.SetActive(true);
}
public void HidePauseMenu()
{
pauseMenu.SetActive(false);
}
public void ShowOptionMenu()
{
optionMenu.SetActive(true);
HidePauseMenu();
}
public void OptionMenuBack()
{
optionMenu.SetActive(false);
ShowPauseMenu();
}
public void ShowRestartMenu()
{
restartMenu.SetActive(true);
HidePauseMenu();
}
public void RestartMenuBack()
{
restartMenu.SetActive(false);
ShowPauseMenu();
}
public void ShowQuitGameMenu()
{
quitGameMenu.SetActive(true);
HidePauseMenu();
}
public void QuitGameMenuBack()
{
quitGameMenu.SetActive(false);
ShowPauseMenu();
}
// Function button
public void RestartGame()
{
Time.timeScale = 1;
SceneManager.LoadScene("test");
}
public void QuitGame()
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
}
- other game features
The above section explained Blend tree as an example, which can simulate the effects of two pause methods. The
commented out code is to detect the pause method of the flag bit, and the other is to directly use TimeScale
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
///
/// </summary>
public class PlayerMove : MonoBehaviour
{
public float speed = 3;
private Animator moveAnimator;
private void Awake()
{
moveAnimator = GetComponent<Animator>();
}
private void Update()
{
// if (Manager.Instance.m_IsPaused)
// {
// return;
// }
playerMove();
}
private void playerMove()
{
Vector3 movement = getMovement() * speed * Time.deltaTime;
float hor = getHorizontal();
float ver = getVertical();
moveAnimator.SetFloat("Horizontal", hor);
moveAnimator.SetFloat("Vertical", ver);
float moveValue = movement.sqrMagnitude;
moveAnimator.SetFloat("Movement", moveValue);
this.transform.position += movement;
}
private float getHorizontal()
{
float hor = Input.GetAxis("Horizontal");
return hor;
}
private float getVertical()
{
float ver = Input.GetAxis("Vertical");
return ver;
}
private Vector3 getMovement()
{
float hor = getHorizontal();
float ver = getVertical();
Vector3 movement = Vector3.forward * ver + Vector3.right * hor;
// 做成单位向量,后面使用自定义的speed可以精确控制移动速度
movement.Normalize();
return movement;
}
}
button binding code
Drag the code of PauseMenu to PauseCanvas
Take the Option button of the pause main interface as an example
- In the On Click () list below, click +
- Drag the PauseCanvas with the code attached to ⭕️ in the On Click() list
- Select Runtime On in the first drop-down box
- The second drop-down box selects the ShowOptionMenu function in the script code
The same goes for other buttons
Follow the steps above
Feature Demos and Packages
Link: Baidu Netdisk
Password: tone