[Create Pause Menu] of Unity

Preparation

  1. Unity version: 2019.2.14
  2. Texture Material: Default Material
  3. Others: None

Program default

  1. 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)
  2. 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

  1. Look at the final effect:insert image description here

  2. Create a Canvas and name it UIinsert image description here

  3. Add an Empty object under Canvas, named PauseCavans, to store all UI components of the pause menuinsert image description here

  4. Set the anchor of PauseCanvas to expand around
    insert image description here

  5. 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 colorinsert image description here

  6. 添加PauseMenu/OptionMenu/RestartMenu/QuitGameMenu分所
    insert image description here

    MenuBackGround is a beautified menu bar background, or not

  7. There are three buttons under PauseMenu, and after clicking, you can switch to the three columns of option/restart/quitgame
    insert image description here

  8. The main contents are as follows

    Attachment at the end of the article
    insert image description here

Code

  1. 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)
    // {
    
    

    // }
}

  1. 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
    }

}

  1. 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

insert image description here


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

  1. In the On Click () list below, click +
  2. Drag the PauseCanvas with the code attached to ⭕️ in the On Click() list
  3. Select Runtime On in the first drop-down box
  4. The second drop-down box selects the ShowOptionMenu function in the script code
    insert image description here

The same goes for other buttons

Follow the steps above

Feature Demos and Packages

Link: Baidu Netdisk
Password: tone

Guess you like

Origin blog.csdn.net/qq_37768971/article/details/107167545