Download

How do I modify a UTexture2D in a package?

Hi,

I already load a texture form asset and I set it to a material instance. It works fine.

next, I changed the texture data and already called tex->UpdateResource(),

And I can see the change in the editor.

now, I have a problem about how to save it to the origin asset?

I tried a lot of ways, but it didn’t work.

It is strange that if I save it to a new asset(UPackage), there will be no problem.download

origin texture:
1.jpg

after changed:
2.jpg

restart editor:
2c328396bc8192fd0f582209b37ebdd4c7712e75.jpeg

the texture in the asset cannot be change by C++ Code?

here is the code:



	//	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());
	}


anyone can offer me aid?

the texture your loaded is considered as have been saved by the ue, so you should reset the sign to unsaved .eg
Result->MarkPackageDirty();

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?

Have you checked that the class you’re using isn’t Transient? I.e, dumps all it’s data when being saved?

Render Targets are probably Transient, for example.

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:

NewTexture->Source.Init(TextureWidth, TextureHeight, 1, 1, ETextureSourceFormat::TSF_BGRA8, Pixels);

I give more details in the attached link.