No announcement yet.

How do I modify a UTexture2D in a package?

  • Filter
  • Time
  • Show
Clear All
new posts

  • How do I modify a UTexture2D in a package?


    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

    origin texture:
    Click image for larger version

Name:	1.jpg
Views:	1
Size:	15.7 KB
ID:	1179622

    after changed:
    Click image for larger version

Name:	2.jpg
Views:	1
Size:	14.6 KB
ID:	1179623

    restart editor:
    Click image for larger version

Name:	3.jpg
Views:	1
Size:	16.5 KB
ID:	1179624
    Last edited by ckcfcc; 04-04-2016, 03:18 AM.

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


    • #3
      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);
      	//	update data
      	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());


      • #4
        anyone can offer me aid?


        • #5
          the texture your loaded is considered as have been saved by the ue, so you should reset the sign to unsaved .eg


          • #6
            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?


            • #7
              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.


              • #8
                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
                Last edited by isaratech; 11-14-2017, 06:50 AM.


                • #9
                  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?


                  • #10
                    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.
                    Creating the UTexture2D object. Let’s say we have generated a UTexture2D from C++ within the editor. It could be interesting to save it in order to use later. Let’s have a look on how t…