UE4笔记---C++加载BP蓝图及C++BP蓝图文件并创建UUserWidget对象

UE4笔记---C++加载BP蓝图及C++BP蓝图文件并创建UUserWidget对象

// 加载BP蓝图文件并创建 UUserWidget
UUserWidget* UserWidget = nullptr;
TSubclassOf<UUserWidget> WidgetClass = LoadClass<UUserWidget>(this, TEXT("/Game/BP/NewWidgetBlueprint.NewWidgetBlueprint_C"));
if (WidgetClass)
{
    UserWidget = CreateWidget<UUserWidget>(GetWorld(), WidgetClass);
}
 
// 加载C++BP蓝图-基类为C++文件并创建 UUserWidget
UVRSWidgetTutorialContentPanel* UserWidget = nullptr;
TSubclassOf<UVRSWidgetTutorialContentPanel> WidgetClass = LoadClass<UVRSWidgetTutorialContentPanel>(this, TEXT("/Game/BP/NewWidgetBlueprint.NewWidgetBlueprint_C"));
if (WidgetClass)
{
    // UserWidget = CreateWidget<UUserWidget>(GWorld, WidgetClass);
    UserWidget = CreateWidget<UVRSWidgetTutorialContentPanel>(GetWorld(), WidgetClass);
}


示例代码:

    TSharedRef<SVerticalBox> VerticalBox = SNew(SVerticalBox)
    + SVerticalBox::Slot()
    .HAlign(HAlign_Center)
    .VAlign(VAlign_Center)
    [
        WidgetClass->TakeWidget()
    ];
 
    // SWidget To SDetailWidget 类型转换
    //TSharedRef<SDetailWidget> StaticSDW = StaticCastSharedRef<SDetailWidget>(DetailWidget);
    //TSharedPtr< SWidget > TempAA = DetailWidget->GetEntryBlockSWidget(TEXT("Check1"));
    //TempAA->SetEnabled(false);
 
    GEngine->GameViewport->AddViewportWidgetForPlayer(GetLocalPlayer(), VerticalBox, 1);


示例代码:

    TSharedPtr<SWindow> TutorialWindow;
 
    TSubclassOf<UUMGTutorial> WidgetClass = LoadClass<UUMGTutorial>(nullptr, TEXT("/Game/BP/BP_UMGTutorial.BP_UMGTutorial_C"));
    if (WidgetClass)
    {
        UserWidget = CreateWidget<UUMGTutorial>(GWorld, WidgetClass);
    }
 
    FGeometry Geom = FGeometry(FVector2D(12.f, 12.f), FVector2D(10.f, 76.f), FVector2D(311.f, 412.f), 1.f);
 
    SAssignNew(TutorialWindow, SWindow)
        .Title(FText::FromString(TEXT("Totorial")))
        .ClientSize(FVector2D(1280, 800))
        .AutoCenter(EAutoCenter::PreferredWorkArea)
        .SupportsMinimize(false)
        .SupportsMaximize(false)
        [
            SNew(SOverlay)
            + SOverlay::Slot()
            [
                SAssignNew(BorderPulseAnimation, SBorderPulseAnimation, Geom)
            ]
            + SOverlay::Slot()
            [
                SNew(SVerticalBox)
                + SVerticalBox::Slot()
                .HAlign(HAlign_Center)
                .VAlign(VAlign_Center)
                [
                    UserWidget->TakeWidget()
                ]
            ]
        ];
 
    FSlateApplication::Get().AddWindow(TutorialWindow.ToSharedRef());

// 出现 UButton 或其他组件编译错误时,需要引用 "UMG.h"

FSlateFontInfo Slate字体信息
.Font(FSlateFontInfo(FPaths::EngineContentDir() / TEXT("Slate/Fonts/Roboto-Regular.ttf"), 14))

FPaths::EngineDir() : ../../../../UE4.14/Engine/


FString FPaths::EngineContentDir()
{
return FPaths::EngineDir() + TEXT("Content/");
}


FPaths::EngineContentDir() : ../../../../UE4.14/Engine/Content/


--------------------- 
作者:chinahaerbin 
来源:CSDN 
原文:https://blog.csdn.net/chinahaerbin/article/details/74905439 
版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值