UE4中使用蓝图实现简易的游戏暂停功能
本文主要为个人笔记性质,小白怕忘知识点,如果可以帮助更多和我一样的UE4初学者,也算功德一件【】
分析暂停功能主要流程为
按暂停按键 > 游戏暂停>出现暂停菜单> 再次按下暂停按键> 暂停菜单关闭> 游戏再次运行
所以,开始
首先设定暂停按键:
暂停功能首先要在Project Setting中找到input,添加一个暂停按键,名称为Pause。

打开Player蓝图(主角),右键,找到设定好的暂停键——Pause,键入蓝图。
3. 选中暂停键,在右上角找到Execute when Paused,勾上,表示游戏暂停时该按键也有效,如果没勾上游戏暂停了,暂停也不好使了。

PS:因为暂停功能会一直可用,无论到哪个关卡,所以最好写在Player蓝图,而非关卡蓝图。
之后制作一个简单的Widget:
1. 右键选择User Interface,Widget Blueprint,新建一个Widget,命名PauseMenu。

2. 在左侧左侧Palette搜索Canvas Panel,下拉到Hierarchy中,新建一个画布,之后再次搜索Text,下拉到Canvas Panel的子级下。
3. 选中Text,在右侧Details中找到Anchors,选择中心样式。之后找到Content,在Text栏后键入“Paused”

4. 在画布中间把text框移动到画面中央。

制作完成。
之后制作暂停键Function:
思路:方法要判断游戏是否已经暂停,如果暂停了如何操作,如果未暂停如何操作。
所以蓝图如下:
1. 在Player蓝图添加新Function,命名PauseGame,进入该Function。
2. Function连接Branch,判断条件为 Is Game Paused。

3. [endif]条件为假的时候(游戏正在运行,未暂停),连接Set Game Paused,Paused为真,这样游戏就暂停了,之后创建Widget,在输出引脚连接Create Widget,Class选择刚刚创建的PauseMenu。后在Owning Player拉出引脚,Get Player Controller,使这个Widget出现在玩家屏幕。

4. 右键Return Value,新建变量,将变量命名PauseMenu,连接引脚到Add to Viewpoint,连接Add引脚至Set Input Mode Game And UI,Player Controller连接参数Get Player Controller,In Widget to Focus参数为刚刚新建的变量PauseMenu,In Mouse Lock Mode选择不锁定,Do Not Lock,之后隐藏鼠标在截图时候勾上对号,让截图不被鼠标影响。
这段是为了让暂停Widget输出到屏幕,而候暂停的时候只有玩家的输入是有效的(刚刚也勾上了Execute when Paused),之后截图鼠标消失。

5. Set Input Mode Game And UI引脚连接Show Mouse Cursor,勾上。Targat也是Get Player Controller。暂停时鼠标存在。
6. 条件为真时候(游戏已经暂停),执行游戏,引脚连接Set Game
Paused,Paused为假,引脚连接Remove All Widgets,去掉暂停界面。
7. 引脚连接Set Input Mode Game Only,参数连接依旧Get Player Controller,之后Show Mouse Cursor,取消勾选,参数Get Player Controller。
禁用鼠标,消去指针。

至此,暂停Function完成。
PS:Remove All Widget会移除所有Widget,所以如果你的HP等其他UI不见了,请把负责此功能的蓝图挂到Tick后面,保证一直刷新,不会被删掉。
最后
回到Player蓝图的Event Graph界面,指针直接连接即可。

游戏中按P就可以暂停游戏了。
笔记结束。