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
版权声明:本文为博主原创文章,转载请附上博文链接!