简体   繁体   中英

How to add widget in Unreal Engine

How can I add widget to the screen in Unreal Engine? By some reason variable blackLinesWidgetClass is always null.

    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();
    }

Looks like UE4 isn't loading the class successfully from the class reference path you gave it. Try adding a _C here: UI/blackLines.blackLines_C'" .

This worked for me

.h

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

.cpp

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM