how can I set or change SkeletalMesh UPROPERTY “Materials” by c++ in editor mode,
l_TargetMat.MaterialInterface = *mat;
this will have bug
void UFL_CopyMat::AutoCopyMat(UObject* Sourse, TArray<UObject*> Targets) {
if (Sourse == nullptr ||
Targets.Num() < 1 ||
!(Sourse->IsA(UStaticMesh::StaticClass()) || Sourse->IsA(USkeletalMesh::StaticClass()))
)
{
return;
}
//
TMap<FName, UMaterialInterface*> l_Slot2Mat;
if (UStaticMesh* l_StaticMeshSourse = Cast<UStaticMesh>(Sourse)) {
for (auto l_SourseMat : l_StaticMeshSourse->StaticMaterials) {
if (!l_SourseMat.MaterialSlotName.IsNone()) {
l_Slot2Mat.Add(l_SourseMat.MaterialSlotName, l_SourseMat.MaterialInterface);
}
}
}
else if (USkeletalMesh* l_SkeletalMeshSourse = Cast<USkeletalMesh>(Sourse)) {
for (auto l_SourseMat : l_SkeletalMeshSourse->Materials) {
if (!l_SourseMat.MaterialSlotName.IsNone()) {
l_Slot2Mat.Add(l_SourseMat.MaterialSlotName, l_SourseMat.MaterialInterface);
}
}
}
//
if (l_Slot2Mat.Num() > 0) {
for (auto l_target : Targets) {
if (UStaticMesh* l_StaticMeshTarget = Cast<UStaticMesh>(l_target)) {
TArray<FStaticMaterial> l_Mats = l_StaticMeshTarget->StaticMaterials;
for (int i = 0; i < l_Mats.Num(); ++i) {
if (UMaterialInterface** mat = l_Slot2Mat.Find(l_Mats[i].MaterialSlotName)) {
l_StaticMeshTarget->SetMaterial(i, *mat);
}
}
}
else if (USkeletalMesh* l_SkeletalMeshTarget = Cast<USkeletalMesh>(l_target)) {
for (auto& l_TargetMat : l_SkeletalMeshTarget->Materials) {
if (UMaterialInterface** mat = l_Slot2Mat.Find(l_TargetMat.MaterialSlotName)) {
l_TargetMat.MaterialInterface = *mat;
}
}
}
}
}
}