方法一
在蓝图关卡中将蓝图控件添加到视口:
方法二
在PlayerController类中创建并添加到视口:
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "TPSPlayerController.generated.h"
UCLASS()
class NETEASETPS_API ATPSPlayerController : public APlayerController
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TSubclassOf<UUserWidget>HUDAssert;
UUserWidget* HUD;
protected:
virtual void BeginPlay() override;
};
#include "TPSPlayerController.h"
#include "Blueprint/UserWidget.h"
void ATPSPlayerController::BeginPlay()
{
Super::BeginPlay();
if (HUDAssert)
{
HUD = CreateWidget<UUserWidget>(this, HUDAssert);
}
if (HUD)
{
HUD->AddToViewport();
}
}
然后新建PlayerController子类蓝图并指定控件蓝图,在世界场景中指定该PlayerController蓝图类。
参考:https://blog.csdn.net/Norths_/article/details/123105415