unity做单机游戏,暂停游戏,显示菜单(毕设过程中的一点小心得)
Time.timescale=0;//暂停游戏
Time.timescale=1//恢复在听的游戏
游戏暂停的过程中仍然可以对UI进行操作,比如按键的修改,声音大小的控制。代码也不是很难,还是给大家看一下例子,灵活运用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StopGame : MonoBehaviour {
bool isStop=true ;//标志位,来判断游戏是否需要被暂停
public GameObject option;//这是我的设置UI界面
// Update is called once per frame
void Update () {
//游戏需要被暂停,按下ESC,游戏暂停,显示我的设置UI界面,然后将标志位设置成false,等待下次点击ESC启动游戏
if (isStop == true)
{
if (Input.GetKeyDown(KeyCode.Escape))
{
Time.timeScale = 0;
isStop = false;
option.SetActive(true);
}
}
//游戏不需要被暂停,按下ESC,游戏启动,隐藏我的设置UI界面,然后将标志位设置成true,等待下次点击ESC暂停游戏
else {
if (Input.GetKeyDown(KeyCode.Escape))
{
Time.timeScale = 1;
isStop = true;
option.SetActive(false);
}
}
}
}