// back.uasset it is a texture
FString Path = TEXT("/Game/back");
// load it
UTexture2D* tmpTex = Cast<UTexture2D>(StaticLoadObject(UTexture2D::StaticClass(), NULL, *Path));
// changne all Pixels to RED
int32 Row = tmpTex->GetSizeY();
int32 Coloum = tmpTex->GetSizeX();
FColor* MipData = static_cast<FColor*>(tmpTex->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE));
for (int32 RowNum = 0; RowNum < Row; ++RowNum)
{
for (int32 ColNum = 0; ColNum < Coloum; ++ColNum)
{
FColor& CurColor = MipData(ColNum + (RowNum * Coloum))];
// change color
CurColor = FColor(255, 0, 0, 255);
}
}
tmpTex->PlatformData->Mips[0].BulkData.Unlock();
// update data
tmpTex->UpdateResource();
tmpTex->MarkPackageDirty();
UPackage* tmpPkg = Cast<UPackage>(tmpTex->GetOuter());
if (tmpPkg)
{
// save package, now I can see "back.uasset" color is changed.
// BUT!!! it's not work. if I restart UE editor, the change is not save, why?
UPackage::SavePackage(tmpPkg, tmpTex, tmpTex->GetFlags(), *tmpPkg->GetPathName());
}
Hello, I’m having a similar issue and marking the package as dirty doesn’t solve the problem : I try to save a texture procedurally generated, but when I re-open the editor, the texture asset is here but the texture is black. It’s like the changes to the texture PlatformData have not been saved. Have you solved this?
I don’t think it is transient, I’m trying to save a UTexture2D created by NewObject<UTexture2D>. It appears well in the editor after being generated (so I think it is, at least, partly saved), but it disappears when the editor is being reloaded.
I’ve posted details and code on AnswerHub https://answers.unrealengine.com/que…d-texture.html
I’m still looking at it and I may have found something: it seems that the PlatformData (FTexturePlatformData*) where I store the data of the UTexture2D is not saved (it explains why the dimensions appear as 0x0 and why the PixelFormat is “unknown” at loading -> I store this data in the PlatformData field). Could it be possible that it is transient? If so, is there a field of UTexture2D which is supposed to persistently store the data so that we can procedurally edit a texture and save the result?
Finally, I’ve found the answer: indeed PlatformData is not saved with UPackage::SavePackage. I had use UTexture::Source.Init() to initialize data in the texture that can be persisted.
So, the line I’ve added (before the call to UpdateResource) is: