Download

How to create a texture asset (physical file) with C++? I'm desperate :(

I’ve tried many code snippets, read many blog posts… but I couldn’t achieve it. For example, this does not work for me (the file is not really created although you can see it in the Content tab): UE4 - Como crear una textura como Asset en Unreal Editor con C++

What I want:
1-Create a dynamic 2D texture.
2-Fill its content.
3-Save it as an asset, in the hard drive, that could be referenced in a material afterwards.

It’s something that should be super-easy (I quickly figured out how to do it in other engines) but I can’t find the way in Unreal. I’m thinking about saving the data directly into a PNG with an external library or something like that instead of using the tools provided by the engine.

Please, can anybody lend me a hand? Thanks.

Look into Paper2D plugin code.
All its code is public shipped with the engine, you can see there how they save 2D sprite assets.

I did that but IIRC doing the same in my code required UnrealEd module and adding it produced hundreds of compilation error that I could not understand :frowning:

Hi,

I am doing something similar in my project, maybe it can help. It is a bit more complicated as I am filling a 2D texture in the code with my webcam stream at every frame to display it on a widget in UMG. Here is a global view of what I do :

I have a class UCustomCamera that inherits UObject. In the hpp file I declare a public UPROPERTY(BlueprintReadWrite, Category = Webcam) UTexture2D* VideoTexture;
and a private FUpdateTextureRegion2D* VideoUpdateTextureRegion; which is a pointer to update the texture

In the cpp, I initialize the UTexture2D* and the FUpdateTextureRegion2D* with the width and height of my stream input :
VideoTexture = UTexture2D::CreateTransient(CamSize.X, CamSize.Y);
VideoTexture->UpdateResource();
VideoUpdateTextureRegion = new FUpdateTextureRegion2D(0, 0, 0, 0, CamSize.X, CamSize.Y);

For every frame (in a Tick() function), I fill the texture by calling the UpdateTextureRegions (see http://api.unrealengine.com/INT/API/Runtime/Engine/Engine/UTexture2D/UpdateTextureRegions/index.html) on my UTexture2D where you can pass the buffer containing your image (argument uint8 * SrcData). (If I remember well you can use a TArray<FColor> to create your image and call TArray<FColor>::GetData() to get the buffer data, for me it is a bit different as I use OpenCV matrices)

Then, in my level blueprint, I can access the VideoTexture member from my UCustomCamera blueprint. To create a UMG material I use the MakeSlateBrush node and call SetBrush on my UMG widget (it is an Image). I assume you can use a UTexture2D on a 3D mesh material too.

I hope this can help you

Cheers,
Flora