I’ve got a UMG widget (ContainerWidget) that has a canvas panel as its root. Inside the canvas panel is another widget (CenterWidget) that is anchored to the center position. If I add the ContainerWidget to the viewport everything works as expected: The CenterWidget is anchored to the center of the screen.
I’m now trying to create a MasterWidget that has and Overlay as its root which other widgets will be added to dynamically (from code). The MasterWidget is just used to show/hide all of the HUD UI but should not change any of the positioning/anchoring.
But if I use AddChild to add the ContainerWidget to the MasterWidget, the anchoring within the MasterWidget no longer works (it sticks to the top left of the screen). I’ve tried changing the root of the MasterWidget to CanvasPanel, Overlay, etc. but nothing seems to preserve the anchoring of the children.
Is there a way to add a widget to another widget but preserve the screen anchoring or the internal one?
As ClockworkOcean mentions, you do have to make changes to the slot of the child widget within the master widget for it to work. Luckily it is pretty simple. Here is the code that adds a childWidget to the overlay of the RootWidget. It then sets the horizontal and vertical alignment to Fill which allows the childWidget to take up the full screen area and make it’s internal anchoring work properly.
UOverlay* overlay = Cast<UOverlay>(_rootWidget->GetRootWidget());
UOverlaySlot* slot = overlay->AddChildToOverlay(childWidget);
// Set the child widget to fill overlay area to allow internal anchoring to work