Download

UMG: Can't center widget in viewport

I’m spawning a window in code, and it’s just staying in the top left. Is this code correct? (WindowWidget is derived from UUserWidget)



    WindowWidget->AddToViewport();

    if (!InWindowParameters.bCenterOfScreen)
        WindowWidget->SetPositionInViewport(InWindowParameters.Position);
    else
    {
        WindowWidget->SetAnchorsInViewport(FAnchors(0.5f, 0.5f, 0.5f, 0.5f));
        WindowWidget->SetAlignmentInViewport(FVector2D(0.5f, 0.5f));
        WindowWidget->SetPositionInViewport(FVector2D(0.0f, 0.0f));
    }

    WindowWidget->SetDesiredSizeInViewport(InWindowParameters.Size);