Creating a dynamic material instance on C++?

Hello there!

I have an actor with several meshes. I would like to create a dynamic material and then apply it to the components. However, I can’t seem to find how to do this.

Where should the material be instantiated? And, should it be applied on the Constructor? How can the parameters be changed?

Thanks in advance!

4 Likes

O_o

	UPROPERTY(EditAnywhere)
	UMaterialInterface* Material;

	virtual void BeginPlay() override
	{
		//create dynamic material anywhere u like, Constructor or anywhere .
		UMaterialInstanceDynamic* DynMaterial = UMaterialInstanceDynamic::Create(Material, this);
		//set paramater with Set***ParamaterValue
		DynMaterial->SetScalarParameterValue("MyParameter", myFloatValue);
		MyComponent1->SetMaterial(0, DynMaterial);
		MyComponent2->SetMaterial(0, DynMaterial);
		//...
	}
10 Likes

Hi, can you please show an example how would one store created material to disk?

Tank you

The actual material? That’s not directly supported by the engine, but you can dig around engine source to see how they do it (probably something involving the Asset Manager).
You could also just save a texture to disk, but either way it’s not relevant to this thread.
You’re better off just starting your own thread with that question.

If the materials are created could it be possible to loaded them using StaticLoadObject() … I’m guessing not but wanted more insight on instanced materials in C++ can you onle create them? not load already created Mat instance uasset?

You could just store the parameters that you changed/used instead of the entire material