游戏暂停界面


暂停界面的Pathes
路径、GamePauseView
、GamePauseController
的创建略
Empty4Raycast
Empty4Raycast
脚本挂载在GamePauseView根节点上,它是一个不增加overdraw但是可以阻挡点击事件的脚本。使用这个脚本,可以让我们的背景完全透明但不像Image组件那样增加overdraw。注意,GamePauseView根节点必须还要挂载Canvas Renderer组件。
在Utility文件夹中新建Empty4Raycast
脚本。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| using UnityEngine.UI;
public class Empty4Raycast : MaskableGraphic { public Empty4Raycast() { useLegacyMeshGeneration = false; }
protected override void OnPopulateMesh(VertexHelper vh) { vh.Clear(); } }
|
GameUIController
给Pause按钮添加打开暂停界面的方法
1 2 3 4 5 6 7 8 9 10 11 12
| [BindPrefab(Pathes.GAMEUI_VIEW,Consts.BIND_PREFAB_PRIORITY_CONTROLLER)] public class GameUIController : ControllerBase { protected override void InitChild() { transform.Find("Life").gameObject.AddComponent<LifeController>(); transform.ButtonAction("Pause", () => { UIMgr.Instance.Show(Pathes.PAUSE_VIEW); }); } }
|
GamePauseView
GamePauseController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| [BindPrefab(Pathes.PAUSE_VIEW,Consts.BIND_PREFAB_PRIORITY_CONTROLLER)] public class GamePauseController : ControllerBase { protected override void InitChild() { transform.ButtonAction("Exit", Exit); transform.ButtonAction("Continue", Continue); } private void Exit() { GameStateModel.Instance.TargetScene = SceneName.StartScene; UIMgr.Instance.Show(Pathes.LOADING_VIEW); UIMgr.Instance.Back(); } private void Continue() { GameStateModel.Instance.Pause = false; UIMgr.Instance.Back(); } }
|
游戏暂停逻辑
在GameStateModel
中添加游戏暂停变量。
1 2 3 4 5
| public class GameStateModel : NormalSingleton<GameStateModel> { public bool Pause { get; set; } }
|
我们所有的脚本使用Unity生命周期,都是在LifeCycleMgr
中处理的,所以想要暂停,直接修改LifeCycleMgr
1 2 3 4 5 6 7 8
| private void Update() { if(GameStateModel.Instance.Pause) { return; } LifeCycleConfig.LifeCyclesFuncs[LifeName.UPDATE].Invoke(); }
|