UE4/CPP Background Blur Widget控件的使用和游戏的暂停与恢复

这是找到的一个视频教程,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++,


具体参考:

官方文档: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);  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值