In essence you will want to create the UUserWidget and add it to the viewport:
UUserWidget* MenuWidget = CreateWidget<UUserWidget>(GetWorld(), Menu);
if(!MenuWidget->IsInViewport())
MenuWidget->AddToViewport();
And remove the widget when it is no longer needed:
MenuWidget->RemoveFromViewport();
I have a lot going on in my code so I am trying to simplify it here. In my game mode class I have a UPROPERTY TSubclassOf< UUserWidget > that I pass in to a function which creates the widgets for me:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "GameMenu")
TSubclassOf<UUserWidget> GameMenu;
During the game mode BeginPlay I bind Start and Back to functions that will pause the game and show the menu; resume the game and hide the menu as needed:
APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();
if (PlayerController)
{
EnableInput(PlayerController);
InputComponent->BindAction("Start", EInputEvent::IE_Pressed, this, &AMyGameMode::Start).bExecuteWhenPaused = true;
InputComponent->BindAction("Back", EInputEvent::IE_Pressed, this, &AMyGameMode::Back).bExecuteWhenPaused = true;
}
And when showing the game menu:
GetWorld()->GetFirstPlayerController()->bShowMouseCursor = true;
GetWorld()->GetFirstPlayerController()->SetPause(true);
GetWorld()->GetFirstPlayerController()->SetInputMode(UiInputMode);
// call the function (or add code here) to create the UUserWidget and add it to the viewport
And hide the game menu and resume playing:
GetWorld()->GetFirstPlayerController()->bShowMouseCursor = false;
GetWorld()->GetFirstPlayerController()->SetPause(false);
GetWorld()->GetFirstPlayerController()->SetInputMode(GameInputMode);
// remove the widget from the viewport
This does work in 4.27. As to how I implemented it. . . your mileage may vary.