Is it possible to wrap game viewport in a UMG widget? I have this custom widget class:
Header
UCLASS()
class MYPROJECT_API UMyViewportWidget : public UContentWidget
{
GENERATED_BODY()
UPROPERTY(EditAnywhere, Category = Appearance)
FLinearColor BackgroundColor;
virtual void ReleaseSlateResources(bool bReleaseChildren) override;
protected:
UFUNCTION(BlueprintCallable)
void InitializeViewport();
TSharedPtr<class SViewport> ViewportWidget;
virtual TSharedRef<SWidget> RebuildWidget() override;
};
Source
void UMyViewportWidget::ReleaseSlateResources(bool bReleaseChildren)
{
Super::ReleaseSlateResources(bReleaseChildren);
ViewportWidget.Reset();
}
void UMyViewportWidget::InitializeViewport()
{
UGameInstance* MyGameInstance = GetWorld()->GetGameInstance();
SViewport* View = MyGameInstance->GetGameViewportClient()->GetGameViewportWidget().Get();
View->SetActive(false);
ViewportWidget->SetContent(View->AsShared());
}
TSharedRef<SWidget> UMyViewportWidget::RebuildWidget()
{
if (IsDesignTime())
{
return SNew(SBox)
.HAlign(HAlign_Center)
.VAlign(VAlign_Center)
SNew(STextBlock).Text(NSLOCTEXT("", "", "My Viewport"))
];
}
else
{
ViewportWidget = SNew(SViewport);
ViewportWidget->SetRenderDirectlyToWindow(false);
return ViewportWidget.ToSharedRef();
}
}
But I get the below crash. Attached image is the call stack.
Exception thrown at 0x00007FFB433704DA (UE4Editor-Engine.dll) in UE4Editor-Win64-DebugGame.exe: 0xC00000FD: Stack overflow (parameters: 0x0000000000000001, 0x0000002BB8A03F60).
Unhandled exception at 0x00007FFB433704DA (UE4Editor-Engine.dll) in UE4Editor-Win64-DebugGame.exe: 0xC00000FD: Stack overflow (parameters: 0x0000000000000001, 0x0000002BB8A03F60).
https://forums.unrealengine.com/core/image/gif;base64
I added the viewport widget to UMG and called InitializeViewport and above crash happens.