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

发表于2018-01-26
评论0 5.1k浏览
下面就来看看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()  
];  
// <span style="color:#cc0000;">SWidget To SDetailWidget 类型转换</span>  
//TSharedRef<SDetailWidget> StaticSDW = StaticCastSharedRef<SDetailWidget>(DetailWidget);  
//TSharedPtr< SWidget > TempAA = DetailWidget->GetEntryBlockSWidget(TEXT("Check1"));  
//TempAA->SetEnabled(false);  
<span style="color:#330033;"></span>GEngine->GameViewport->AddViewportWidgetForPlayer(GetLocalPlayer(), VerticalBox, 1);<span style="color:#ffffff;"></span>  

示例代码:
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()  
            ]  
        ]  
    ];  
<span style="color:#330033;">FSlateApplication::Get().AddWindow(TutorialWindow.ToSharedRef());</span>  

// 出现 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/

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

标签: