I want to wirte widget to render target by WidgetRenderer->DrawWidget, then transform the render target to texture2D. But when the program run to RenderTarget->ConstructTexture2D,unreal engine got crashed.
Here is some of my code:
UTexture2D* UMainObject::RenderUIToTexture(const FString& Title, const FString& Text)
{
UTextureRenderTarget2D* RenderTarget = NewObject<UTextureRenderTarget2D>();
RenderTarget->InitAutoFormat(128, 128);
TSharedPtr<SMarkWidget> SlateWidget = SNew(SMarkWidget).Title(Title).Text(Text);
FWidgetRenderer* WidgetRenderer = new FWidgetRenderer(true);
FVector2D DrawSize(128, 128);
WidgetRenderer->DrawWidget(
RenderTarget,
SlateWidget->AsShared(),
1.0,
DrawSize,
FApp::GetDeltaTime(),
true);
UTexture2D* Texture2D = nullptr;
RenderTarget->ConstructTexture2D(Texture2D, "texture", EObjectFlags::RF_NoFlags, CTF_DeferCompression);
RenderTarget->CompressionSettings = TextureCompressionSettings::TC_VectorDisplacementmap;
#if WITH_EDITORONLY_DATA
Texture2D->MipGenSettings = TextureMipGenSettings::TMGS_NoMipmaps;
#endif
Texture2D->SRGB = 1;
Texture2D->UpdateResource();
return Texture2D;
}
Anything i can do to fix the crash?