Created
August 5, 2016 21:23
-
-
Save dunenkoff/6c4adaa0e38166cc9dc14aeaca02531d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2016 People Gotta Play. All rights reserved. | |
#include "ProjectMK.h" | |
#include "UMGExtensionLibrary.h" | |
UTextureRenderTarget2D * UUMGExtensionLibrary::RenderWidgetToTexture(bool UseGamma, TextureFilter Filter, UUserWidget * WidgetToRender, FVector2D DrawSize, float DeltaTime) | |
{ | |
if (!WidgetToRender) return nullptr; | |
if (DrawSize == FVector2D(0, 0)) return nullptr; | |
FWidgetRenderer *r = new FWidgetRenderer(UseGamma); | |
UTextureRenderTarget2D *rt = r->CreateTargetFor(DrawSize, Filter, UseGamma); | |
TSharedRef<SWidget> ref = WidgetToRender->TakeWidget(); | |
r->DrawWidget(rt, ref, DrawSize, DeltaTime); | |
delete r; | |
return rt; | |
} | |
UTextureRenderTarget2D * UUMGExtensionLibrary::CreateRenderTarget(bool UseGamma, TextureFilter Filter, FVector2D DrawSize) | |
{ | |
return FWidgetRenderer::CreateTargetFor(DrawSize, Filter, UseGamma); | |
} | |
void UUMGExtensionLibrary::RenderWidgetToTarget(bool UseGamma, TextureFilter Filter, UUserWidget * WidgetToRender, FVector2D DrawSize, float DeltaTime, UPARAM(ref) UTextureRenderTarget2D * Target) | |
{ | |
if (!WidgetToRender) return; | |
if (DrawSize == FVector2D(0, 0)) return; | |
if (!Target) return; | |
FWidgetRenderer * r = new FWidgetRenderer(UseGamma); | |
TSharedRef<SWidget> ref = WidgetToRender->TakeWidget(); | |
r->DrawWidget(Target, ref, DrawSize, DeltaTime); | |
delete r; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2016 People Gotta Play. All rights reserved. | |
#pragma once | |
#include "Kismet/BlueprintFunctionLibrary.h" | |
#include "UMG.h" | |
#include "SlateBasics.h" | |
#include "SlateCore.h" | |
#include "SWidget.h" | |
#include "WidgetRenderer.h" | |
#include "UMGExtensionLibrary.generated.h" | |
/** | |
* Don't forget to include UMG, Slate and SlateCore modules in Build.cs | |
*/ | |
UCLASS() | |
class PROJECTMK_API UUMGExtensionLibrary : public UBlueprintFunctionLibrary | |
{ | |
GENERATED_BODY() | |
public: | |
//~~~ Widget Renderer ~~~ | |
UFUNCTION(Category = "UMG", BlueprintCallable, Meta = (DeterminesOutputType = "UTextureRenderTarget2D")) | |
static UTextureRenderTarget2D * CreateRenderTarget(bool UseGamma, TextureFilter Filter, FVector2D DrawSize); | |
UFUNCTION(Category = "UMG", BlueprintCallable) | |
static void RenderWidgetToTarget(bool UseGamma, TextureFilter Filter, UUserWidget * WidgetToRender, FVector2D DrawSize, float DeltaTime, UPARAM(ref) UTextureRenderTarget2D * Target); | |
UFUNCTION(Category = "UMG", BlueprintCallable, Meta = (DeterminesOutputType = "UTextureRenderTarget2D")) | |
static UTextureRenderTarget2D* RenderWidgetToTexture(bool UseGamma, TextureFilter Filter, UUserWidget * WidgetToRender, FVector2D DrawSize, float DeltaTime); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code no longer works as of the Unreal Engine 4.20.
FWidgetRenderer->DrawWidget() now runs asynchronously in the rendering thread, so deleting the FWidgetRenderer immediately after calling it will cause the game to crash.
Instead, replace "delete r" on line 16 of the .cpp with:
Furthermore, you now also need the "RenderCore" dependency in Build.cs, alongside UMG, Slate, and SlateCore.
Finally, this code is copyrighted, but doesn't specify a licence.
Is it permitted for use in commercial projects?