如何在 Unreal Engine 中添加小部件

How to add widget in Unreal Engine

如何在 Unreal Engine 中将小部件添加到屏幕? 由于某种原因,变量 blackLinesWidgetClass 始终为空。

    FStringClassReference blackLinesWidgeClasstRef(TEXT("WidgetBlueprint'/Game/Blueprints/UI/blackLines.blackLines'"));
    UClass* blackLinesWidgetClass = blackLinesWidgeClasstRef.TryLoadClass<UUserWidget>();
    if (blackLinesWidgetClass)
    {
        UUserWidget* blackLinesWidget = CreateWidget<UUserWidget>(this->GetGameInstance(), blackLinesWidgetClass);
        if (blackLinesWidget)
            blackLinesWidget->AddToViewport();
    }

看起来 UE4 没有从您提供的 class 参考路径成功加载 class。尝试在此处添加 _CUI/blackLines.blackLines_C'".

这对我有用

.h

UPROPERTY(EditAnywhere) TSubclassOf<UUserWidget> widgetBlackLines;
UUserWidget* widgetBlackLinesInstance;

.cpp

void AAct_31::BeginPlay()
{
    widgetBlackLinesInstance = CreateWidget<UUserWidget>(GetWorld(), widgetBlackLines);
    widgetBlackLinesInstance->AddToViewport();
}