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


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

// 加载BP蓝图文件并创建 UUserWidget
UUserWidget* UserWidget = nullptr;
TSubclassOf WidgetClass = LoadClass(this, TEXT("/Game/BP/NewWidgetBlueprint.NewWidgetBlueprint_C"));
if (WidgetClass)
{
	UserWidget = CreateWidget(GetWorld(), WidgetClass);
}

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


示例代码:

	TSharedRef VerticalBox = SNew(SVerticalBox)
	+ SVerticalBox::Slot()
	.HAlign(HAlign_Center)
	.VAlign(VAlign_Center)
	[
		WidgetClass->TakeWidget()
	];

	// SWidget To SDetailWidget 类型转换
	//TSharedRef StaticSDW = StaticCastSharedRef(DetailWidget);
	//TSharedPtr< SWidget > TempAA = DetailWidget->GetEntryBlockSWidget(TEXT("Check1"));
	//TempAA->SetEnabled(false);

	GEngine->GameViewport->AddViewportWidgetForPlayer(GetLocalPlayer(), VerticalBox, 1);




示例代码:

	TSharedPtr TutorialWindow;

	TSubclassOf WidgetClass = LoadClass(nullptr, TEXT("/Game/BP/BP_UMGTutorial.BP_UMGTutorial_C"));
	if (WidgetClass)
	{
		UserWidget = CreateWidget(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/








你可能感兴趣的:(UE4学习笔记)