新建两个sprite renderer附上开始和暂停的按键
然后挂上脚本
using UnityEngine; using System.Collections; public class GamePause : MonoBehaviour { void OnMouseUpAsButton() { //用GameManager里的TransformGameState方法去调用按钮功能 GameManager._instance.TransformGameState(); } }
在gamemanager脚本里调用
public enum GameState//枚举两个游戏状态 { Running, Pause } public class GameManager : MonoBehaviour { public GameState gameState=GameState.Running;//声明一个共有变量存储游戏状态,默认为运行 public GameObject gamePuase; public GameObject gameContinue; void Start() { gameContinue.SetActive(false); gamePuase.SetActive(true); } public void TransformGameState() { if (gameState==GameState.Running) {PuaseGame(); gameContinue.SetActive(true); gamePuase.SetActive(false); } else if (gameState==GameState.Pause) { ContinueGame(); gameContinue.SetActive(false); gamePuase.SetActive(true); } } public void PuaseGame() { Time.timeScale = 0;//将时间暂停 gameState=GameState.Pause; } public void ContinueGame() { Time.timeScale = 1;//开始时间 gameState=GameState.Running; } }
这样就完成按键切换与控制时间,特别需要注意在暂停的时候不让角色移动需要在主角移动的方法内加上
if (isMouseDown&&GameManager._instance.gameState==GameState.Running)