2016-06-22 14 views
0

すべての静的メッシュアクタのMaterialプロパティを変更しようとしています。私は各アクターを繰り返し、静的メッシュコンポーネントを見つける必要があることを知っています。しかし、材料を正しく修正することはできません。ここでUnreal Engine 4 C++ AStaticMeshActorのStaticMeshComponentを変更する

は、私は、反復によって、すべての俳優を変更していますので、それが私に起こっている enter image description here

enter image description here

GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("START Modeling()")); 

//Find Actor and change Material 
UWorld* world = GetWorld(); 

//Material Path 
FString matPath = "Material'/Game/StarterContent/Materials/M_Metal_Gold.M_Metal_Gold'"; 
//Material Instance 
UMaterialInstanceConstant* material = Cast<UMaterialInstanceConstant>(StaticLoadObject(UMaterialInstanceConstant::StaticClass(), nullptr, *(matPath))); 
//Iterate Every Static Mesh Actor 
for (TActorIterator<AStaticMeshActor> ActorItr(world); ActorItr; ++ActorItr) 
{ 
    AStaticMeshActor *Mesh = *ActorItr; 
    //Just for Degbuging Purpose 
    GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Actor: %s"), *(ActorItr->GetName()))); 
    GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Location: %s"), *(ActorItr->GetActorLocation().ToString()))); 
    //Get Static Mesh Component 
    TArray<UStaticMeshComponent*> MaterialComps; 
    Mesh->GetComponents(MaterialComps); 
    //I get this code from community answer. I do not know how it works. 
    for (int32 Index = 0; Index != MaterialComps.Num(); ++Index) 
    { 
     UStaticMeshComponent* targetComp = MaterialComps[Index]; 
     int32 mCnt = targetComp->GetNumMaterials(); 
     for (int i = 0; i < mCnt; i++) 
        //This is the core code which actually changing material. 
      targetComp->SetMaterial(0, material); 
    } 

} 

、私のコードです。
ただし、床の俳優の素材のみが変更されます。

答えて

1

あなたはtargetComp->SetMaterial(i, material);にインデックス0

変更targetComp->SetMaterial(0, material);でのみ材料を変更しています。あなたの問題を解決するはずです。

+0

私の質問にお答えします。 – ibocon

関連する問題