ive just upgraded a project to 4.9 from 4.7 and am now having issues creating dynamic material instances in c++.
I have a base material with visual scalar parameters as well as a scalar paramter for a dissolve effect.
within the editor i created 3 material instances and set there visual parameters. i.e color red,green,blue.
I have 3 different c++ staticmesh based classes. And multiple random amounts of each color are spawned at rutime.
previously within the constructor of each class i would create an instance of the one of the already created color instances and apply the material like so.
red,
static ConstructorHelpers::FObjectFinder<UMaterialInstanceConstant> MaterialOb(TEXT("MaterialInstanceConstant'/Game/materials/I_Obstical2.I_Obstical2'"));
if (StaticMeshComponent && MaterialOb.Object)
{
DynamicMat = UMaterialInstanceDynamic::Create(MaterialOb.Object, this);
StaticMeshComponent->SetMaterial(0, DynamicMat);
}
green,
static ConstructorHelpers::FObjectFinder<UMaterialInstanceConstant> MaterialOb(TEXT("MaterialInstanceConstant'/Game/materials/I_Obstical3.I_Obstical3'"));
if (StaticMeshComponent && MaterialOb.Object) {
DynamicMat = UMaterialInstanceDynamic::Create(MaterialOb.Object, this);
StaticMeshComponent->SetMaterial(0, DynamicMat);
}
ect…
but reading around it apears we can no longer use UMaterialInstanceDynamic::Create within the constructor.
and are supposed to use (UMaterialInstanceDynamic*)MaterialOb.Object.
i.e,
static ConstructorHelpers::FObjectFinder<UMaterialInstanceConstant> MaterialOb(TEXT("MaterialInstanceConstant'/Game/materials/I_Obstical3.I_Obstical3'"));
if (GetStaticMeshComponent() && MaterialOb.Object) {
DynamicMat = (UMaterialInstanceDynamic*)MaterialOb.Object;
GetStaticMeshComponent()->SetMaterial(0, DynamicMat);
}
The problem with this is in means that all the static mesh actors of a particlular color are now sharing the same material instance.
And adjusting the disovle scalar on one effects them all. ie all the red actors would disovle or all the green actors would disolve.
how are we now supposed to create unique material instances in c++?
Many thanks,