I am trying to set Dynamic material instance in C++ , the example is provided in blueprint script image:
How to do this properly and if possible I want to do it on a very cheap way so it will not effect the performance.
Thank You
I am trying to set Dynamic material instance in C++ , the example is provided in blueprint script image:
How to do this properly and if possible I want to do it on a very cheap way so it will not effect the performance.
Thank You
Creating a dynamic material instance on C++? - Unreal Engine / Asset Creation - Unreal Engine Forums
Sir I am unable to understand this solution and don’t get any idea from it to implement my own function.
Their is nothing about material slot name, material name, and creation flags information.
void AMCharacter:Myfunc()
{
UMaterialInstanceDynamic* MaterialRef = UMaterialInstanceDynamic::Create(Material, this);
if (MaterialRef != nullptr)
{
auto Material = ConstructorHelpers::FObjectFinder<UMaterial>(TEXT("/Game/material location"));
MaterialRef = GetMesh()->CreateDynamicMaterialInstance(0, Material.Object);
if (Material.Object != nullptr)
{
GetMesh()->SetMaterialByName("material slot name", MaterialRef);
}
return;
}
return;
Sir, material is not successfully applied to the mesh, I am trying to apply on a skeletal mesh.
On your mesh you have a slot name, set this in the code.
Still not working
Okay, I see you’re asking quite a lot of C++ questions here. Community’s help is nice and all, but you need to be able to find ways to do stuff on your own using the Engine code available to you.
Look at the nodes you have. SetMaterialByName states that Target is PrimitiveComponent. Just open PrimitiveComponent.h and/or PrimitiveComponent.cpp in your IDE and look for the function in question. You can find SetMaterialByName()
there, and BTW there’s another CreateDynamicMaterialInstance()
function that creates a dynamic material in another way.
Now, there can be some classes you’re not familiar with, e.g. UMaterialInterface and UMaterialInstanceDynamic. But if you just google them, you’ll easily find them in the official documentation (the engine prefixes make it really easy to find classes you need). On the documentation page, the most important things to look for are Include, because oftentimes you won’t be able to compile without it, and Module, because certain modules are not enabled by default, but in this case you’re fine, that’s a topic of another discussion.
Now, in MaterialInstanceDynamic.h you can find several Create()
functions, and note that they are static; (BTW, once again, you can consult KismetMaterialLibrary.cpp to see what functions it uses. To know that you need to look there, you can hover the CreateMaterialInstanceDynamic node and see what its target is).
Now, when you know what to include to make it work, you can:
class UMaterialInterface* ParentMaterial;
class UMaterialInstanceDynamic* DynamicMaterial;
If you’re going to use them in the constructor, or if you want to set ParentMaterial in bp, and not through ConstructorHelpers, make sure to mark them as UPROPERTY() with respective flags;
Remember those static functions? Static means that you can call them like this:
DynamicMaterial = UMaterialInstanceDynamic::Create(ParentMaterial, this, FName("MyDynMat"));
Mesh->SetMaterialByName(FName("MySlotName"), DynamicMaterial);
As for UObject* InOuter
, I’m not entirely sure what that is, but “this” works, and I’m fine with that.
Anyway, that’s my workflow when it comes to something unfamiliar. Sure, you can ask the community and hope someone will provide you the answer, but if you have undertaken C++, you need to be able to analyze the engine code yourself. It’ll pay out in future.
Hope this helps.
Thank You very very much Sir for solving my problem and helping in my studies, I have learned lots of stuff from the community and very thankful for this.
I am learning analyzing the engine code, but its a little hard for me the limited syntax examples on the official documentation .
Thank You very very much Sir for solving my problem and helping in my studies, I have learned lots of stuff from the community and very thankful for this.
I am learning analyzing the engine code, but its a little hard for me the limited syntax examples on the official documentation .