准备工作
- Unity版本:2019.2.14
- 纹理材质:默认材质
- 其他:暂无
方案预设
- 设置m_isPause标志位,之后所有动作均需要提前判断当前标志位是否为暂停状态,如果是,则停止功能进行。(方案一适用于计时游戏)
- 当进入暂停状态时,设置Time.timeScale = 0,游戏中的时间总是需要乘以该参数,因此设置为0即表示为暂停状态,这个参数也可以用于慢放或者快进。(方案二需要在退出暂停时重新设置为Time.timeScale = 1)
UI设计
本篇博文主要以实现功能为主,UI设计从简
-
看下最终效果:
-
创建Canvas,并命名为UI
-
在Canvas下面添加一个Empty物体,命名为PauseCavans,用来存放暂停菜单的所有UI组件
-
设置PauseCanvas的锚为四周拓展形
-
在暂停功能下面添加Panel,用于暂停时灰度背景,同时将其锚设置为四周拓展形> 其中Color可以设置背景颜色
-
添加PauseMenu/OptionMenu/RestartMenu/QuitGameMenu分栏
MenuBackGround 是美化的菜单栏背景,也可以不用
-
PauseMenu下面设置三个按钮,点击之后可以切换到option/restart/quitgame三个分栏下面
-
包含的内容主要如下
文末附包
代码实现
- 主管理器
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)
// {
// }
}
- 暂停功能
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
}
}
- 其他游戏功能
以上一节讲解Blend tree为例,可以模拟两种暂停方式效果
注释掉的代码是以检测标志位的暂停方式,另一种是直接使用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;
}
}
按钮绑定代码
将PauseMenu 的代码拖到 PauseCanvas
以暂停主界面的Option按钮为例
- 在下方的On Click()列表中点 +
- 将附着有代码的PauseCanvas拖到On Click()列表中的⭕️中
- 第一个下拉框选择Runtime On
- 第二个下拉框选择脚本代码中的ShowOptionMenu函数
其他按钮也是一样的
照着上面的步骤
功能演示和包
链接: 百度网盘
密码: tone