Based on this post, I built my own function to load a texture from disk.
(Why this instead of a reference to the content browser? I have my reasons. )
UTexture2D* AMyHUD::LoadTexture(FString TextureFilename)
{
TArray<uint8> RawFileData;
UTexture2D *mytex = NULL;
IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
// Note: PNG format. Other formats are supported
IImageWrapperPtr ImageWrapper = ImageWrapperModule.CreateImageWrapper(EImageFormat::PNG);
if (FFileHelper::LoadFileToArray(RawFileData, *TextureFilename))
{
if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(RawFileData.GetData(), RawFileData.Num()))
{
const TArray<uint8>* UncompressedBGRA = NULL;
if (ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, UncompressedBGRA))
{
mytex = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_B8G8R8A8);
}
void* TextureData = mytex->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
FMemory::Memcpy(TextureData, UncompressedBGRA->GetData(), UncompressedBGRA->Num());
mytex->PlatformData->Mips[0].BulkData.Unlock();
// Update the rendering resource from data.
mytex->UpdateResource();
}
}
return mytex;
}
This is only used to show a texture on an AHUD actor like so:
void AMyHUD::DrawHUD()
{
DrawTexture(MyTexturePtr, 0, 0,Dim.X, Dim.Y, 0, 0, 1, 1);
}
So nothing is deliberately destroyed during the process that would cause a side-effect to the life-cycle of the texture, everything works fine up to a few seconds (5 to 20) until the garbage collector kicks in and calls Texture2D::BeginDestroy(). And I canât understand why. The pointer is constantly used for display so I canât see why the GC decides itâs time to give it up.
Maybe the creation method UTexture2D::CreateTransient() isnât adequate and puts a âtimerâ of sorts in the life cycle of this object.
Any help appreciated.