版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/I_itaiit/article/details/80386978
这是找到的一个视频教程,Background Blur Widget控件的使用,需要梯子,
这里录制了一份:https://pan.baidu.com/s/1-Nn_miRipVSlFdRjrjwORw,有需要的可以观看。
游戏的暂停和恢复,首先设计UMG界面:
然后使用C++绑定按钮事件,具体可以参考另一篇文章:https://blog.csdn.net/i_itaiit/article/details/80344864
我是当玩家按下ESC键的时候,弹出此菜单,如果玩家点击取消,则继续游戏,如果点击确定,则退出游戏。
因此我把这个功能写在了MyCharacter类中,理解是玩家按下ESC键之后才弹出菜单(初学者,不知道这么安排是否合理:)_)
MyCharacter类中的部分
在MyCharacter.h中定义属性:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Widgets")
TSubclassOf<class UUserWidget> wPauseMenu;
// Variable to hold the widget After Creating it.
UUserWidget* MyPauseMenu;
void Pause();
MyCharacter.cpp中绑定Escape键的部分如下:
PlayerInputComponent->BindAction("Pause", IE_Released, this, &AMyCharacter::Pause);
Pause()函数的实现,这段的功能主要是暂停游戏
,弹出暂停菜单
,显示鼠标
,设置输入模式
:
void AMyCharacter::Pause()
{
UGameplayStatics::SetGamePaused(this, true);
if (wPauseMenu) // Check if the Asset is assigned in the blueprint.
{
// Create the widget and store it.
MyPauseMenu = CreateWidget<UUserWidget>(GetWorld(), wPauseMenu);
// now you can use the widget directly since you have a referance for it.
// Extra check to make sure the pointer holds the widget.
if (MyPauseMenu)
{
//let add it to the view port
MyPauseMenu->AddToViewport();
}
APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GWorld, 0);
AMyPlayerController* MyPlayerController = Cast<AMyPlayerController>(PlayerController);
//Show the Cursor.
MyPlayerController->bShowMouseCursor = true;
MyPlayerController->SetInputMode(FInputModeUIOnly());
}
}
PauseWidget类中的部分
分别将函数QueDingFun()绑定给按钮QueDingBtn,QuXiaoFun()绑定给QuXiaoBtn
void UPauseWidget::QueDingFun()
{
UKismetSystemLibrary::QuitGame(this, nullptr, EQuitPreference::Quit);
}
void UPauseWidget::QuXiaoFun()
{
GetOwningPlayer()->SetInputMode(FInputModeGameOnly());
RemoveFromParent();
GetOwningPlayer()->bShowMouseCursor = false;
UGameplayStatics::SetGamePaused(this, false);
}
主要是在QuXiaoFun的函数中遇到问题,此处是找到了官方的文档,蓝图实现的游戏的暂停和恢复,改写成的C++,
扫描二维码关注公众号,回复:
3086836 查看本文章
具体参考:
官方文档:http://api.unrealengine.com/CHN/Engine/UMG/QuickStart/5/index.html
如何在PlayerController的类即子类外获得PlayerController,参考的:https://blog.csdn.net/chinahaerbin/article/details/74905452
// UE4笔记---获取 PlayerController 对象
APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GWorld, 0);
ATutorialsPlayerController* TutorialsPlayerController = Cast<ATutorialsPlayerController>(PlayerController);