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.
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
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);
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.