How to construct UMG widget in C++?

void UMyUserWidget::Initialize()
{
Super::Initialize();

// Initialize called after hot-reload with WidgetTree = NULL, skip this unexpected call, or you will get CanvasPanel is not packaged error.
if(WidgetTree)
{
     auto MyCanvas = WidgetTree->ConstructWidget<UCanvasPanel>(UCanvasPanel::StaticClass());
     auto MyButton = WidgetTree->ConstructWidget<UButton>(UButton::StaticClass());
     MyCanvas->AddChildToCanvas(MyButton);
     WidgetTree->RootWidget = MyCanvas;
}

 }

Well, now i got UMG widget constructed and visible, but i dont know yet how to do that AFTER Initialize call, for instance inside NativeConstruct function, which is called after Initialize.

1 Like