I’m trying to create a UTexture2D from a CanvasRenderTarget2D instance which I’ve accomplished via CanvasRenderTarget2D::ConstructTexture2D in editor builds. When building for console, however, that function requires WITH_EDITORONLY_DATA be true because it uses PostEditChange(). So the function returns NULL.
So my question is, how can I make this work in console builds?
Edit: The most I could get working was this terribly performing function (needs to be called every tick ideally):
//Test
UTexture2D* URuntimeCanvasRenderTarget2D::ConstructRuntimeTexture2D(UCanvasRenderTarget2D *target)
{
int Width = 256;
int Height = 256;
class UTexture2D* Result = UTexture2D::CreateTransient(
Width,
Height,
PF_B8G8R8A8
);
FRenderTarget* RenderTarget = target->GameThread_GetRenderTargetResource();
if (RenderTarget == NULL)
{
return Result;
}
// Lock the texture so it can be modified
uint8* MipData = static_cast<uint8*>(Result->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE));
TArray<FColor> SurfData;
RenderTarget->ReadPixels(SurfData);
// Create base mip.
uint8* DestPtr = NULL;
const FColor* SrcPtr = NULL;
for (int32 y = 0; y<Height; y++)
{
DestPtr = &MipData[(Height - 1 - y) * Width * sizeof(FColor)];
SrcPtr = const_cast<FColor*>(&SurfData[(Height - 1 - y) * Width]);
for (int32 x = 0; x<Width; x++)
{
*DestPtr++ = SrcPtr->B;
*DestPtr++ = SrcPtr->G;
*DestPtr++ = SrcPtr->R;
*DestPtr++ = SrcPtr->A;
SrcPtr++;
}
}
// Unlock the texture
Result->PlatformData->Mips[0].BulkData.Unlock();
Result->UpdateResource();
return Result;
}