【问题标题】:Menu pause C++ UE4菜单暂停 C++ UE4
【发布时间】:2021-10-26 14:33:47
【问题描述】:

你知道如何在 C++ 中暂停虚幻吗? 我有一个从 UUserWidget 派生的带有 RESUME 按钮和脚本 C++ 的 Widget 蓝图。 我想按 Tab 键暂停游戏。 请问有人有解决办法吗?

这是我的 MenuPause.h :


#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "Components/Button.h"
#include "MenuPause.generated.h"

/**
 * 
 */
UCLASS()
class PROJET2_API UMenuPause : public UUserWidget
{
    GENERATED_BODY()

    public:
    virtual bool Initialize() override;

    UPROPERTY(meta = (BindWidget))
    class UButton* ResumeButton;

    UFUNCTION()
void OnClickResume(bool bPaused);
}

这是我的 MenuPause.cpp:

#include "MenuPause.h"
#include "Kismet/GameplayStatics.h"

bool UMenuPause::Initialize()
{
    Super::Initialize();
    ResumeButton->OnClicked.AddDynamic(UMenuPause::OnClickResume(bool bPaused));
    return true;
}

void UMenuPause::OnClickResume(bool bPaused)
{
    UGameplayStatics::SetGamePaused(ResumeButton, bPaused);
    
    //UGameplayStatics::OpenLevel(this, FName(*GetWorld()->GetName()), false);
}

感谢您的理解:)

【问题讨论】:

    标签: c++ user-interface widget unreal-engine4


    【解决方案1】:
    //PlayerController
    
    .h
    
    virtual void SetupInputComponent() override;
    
    UFUNCTION()
        void TogglePauseEvent();
    .cpp
    void SetupInputComponent()
    {
        Super::SetupInputComponent();
        if (InputComponent)
        {
            this->PlayerInput->AddActionMapping(FInputActionKeyMapping("TogglePause", EKeys::Tab));
            InputComponent->BindAction("TogglePause", EInputEvent::IE_Pressed, this, &ThisClass::TogglePauseEvent);
        }
    }
    
    void TogglePauseEvent()
    {
        MenuPause->ChangeTogglePause();
    }
    
    //UMenuPause
    .h
    
    bool bTogglePause = false;
    
    .cpp
    
    void UMenuPause::ChangeTogglePause()
    {
    
        UGameplayStatics::SetGamePaused(ResumeButton, bTogglePause);
      
        bTogglePause = !bTogglePause;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多