Download

Using a UMG widget in slate

I’ve been reading through all of the earlier posts on this for a few days but I’m unable to get this to work.


    FStringAssetReference ToolkitUIPath("EditorUtilityWidgetBlueprint'/HyperFoliagePlugin/UI/UI_HFToolkitPanel.UI_HFToolkitPanel'");
    ToolkitUI = (UHyperFoliageEditorWidget *)ToolkitUIPath.TryLoad();
    SAssignNew(ToolkitWidget, SBorder);
    ToolkitWidget->SetContent(ToolkitUI->TakeWidget());

ToolkitUI is valid but TakeWidget() fails because RebuildWidget doesn’t work.

RebuildWidget() fails. I’m not quite sure what to do here to get the widget to instantiate itself.

Hello @Antidamage I actually was doing the same 2 weeks ago. I wrote this example: lets asume you want to create a new SWindow and use a UMG widget inside of it:



void CreateWindowFromUMG(UWidgetBlueprint* Blueprint)
{
    if (!Blueprint) return;

    //~ Gets actual widget class from the blueprint
    TSubclassOf<UEditorUtilityWidget> WidgetClass = Blueprint->GeneratedClass;

    //~ We need da world
    UWorld* World = GEditor->GetEditorWorldContext().World();
    check(World);

    //~ Create widget 
    UEditorUtilityWidget* CreatedUMGWidget = CreateWidget<UEditorUtilityWidget>(World, WidgetClass);
    if (CreatedUMGWidget)
    {

        //~ Create Slate window
        TSharedPtr<SWindow> EditorTab = SNew(SWindow)
            .AutoCenter(EAutoCenter::None)
            .Title(FText::FromString("my win"))
            .IsInitiallyMaximized(false)
            .ClientSize(FVector2D(300,300))
            .SizingRule(ESizingRule::UserSized)
            .SupportsMaximize(true)
            .SupportsMinimize(true)
            .CreateTitleBar(true)
            .HasCloseButton(true);

        //~ Add Windows to slate app
        FSlateApplication & SlateApp = FSlateApplication::Get();
        SlateApp.AddWindow(EditorTab.ToSharedRef(), true);

        //~ Use UMG in slate
        EditorTab->SetContent(CreatedUMGWidget->TakeWidget());
    }
}


if you want to use the path to your asset like in your example, I suggest you to use FAssetData instead, it is easier.
Cheers!

Awesome, thanks dude! Hopefully this comes up as the first search result from now on.

I’m getting some strange stuff happening.

First, this seems to work:


    UBlueprint * Blueprint = Cast<UBlueprint>(FindObject<UObject>(NULL, *FString(TEXT("EditorUtilityWidgetBlueprint'/HyperFoliagePlugin/UI/UI_HFToolkitPanel.UI_HFToolkitPanel'"))));
    TSubclassOf<UEditorUtilityWidget> WidgetClass = (TSubclassOf<UEditorUtilityWidget>)Blueprint->GeneratedClass;

    UWorld * World = GEditor->GetEditorWorldContext().World();
    check(World);
    UEditorUtilityWidget * CreatedUMGWidget = CreateWidget<UEditorUtilityWidget>(World, WidgetClass);

    SAssignNew(ToolkitWidget, SBorder);
    ToolkitWidget->SetContent(CreatedUMGWidget->TakeWidget());

I couldn’t get LoadBlueprintAssetDataFromPath() to find anything in the path, which was odd, so I tried FindObject instead and it worked.

From here the following happened:

  1. Trying to instantiate the UI in the tab results in “Ensure(InWidgetTree)” failing to pass during CreateWidget.
  2. Recompiling the blueprint crashes due to a cast failing: CastLogError()
  3. Reload, duplicate blueprint, delete old blueprint, rename new blueprint to the same. Show tab. It works.
  4. Reload, back to 1.

I would just assume that it’s not being instantiated or loaded properly, but then the blueprint is corrupted on reload, yet is somehow still OK when duplicated replacing the old one.

What’s going on here?