http://www.dawnarc.com/2016/10/ue4%E5%A6%82%E4%BD%95%E6%9B%BF%E6%8D%A2%E8%A7%92%E8%89%B2mesh%E4%B8%8A%E7%9A%84material%E6%9D%90%E8%B4%A8/
.h (located in pawn header file and assigned in Blueprint editor)
UPROPERTY(EditAnywhere)
TArray<UMaterialInterface*> Materials;
TSubclassOf<class AMyPawn> PawnClass;
FVector spawnLocation = FVector(0.0f, 0.0f, 0.0f);
FRotator spawnRotation = FRotator::ZeroRotator;
AMyPawn* pawn = GetWorld()->SpawnActor<AMyPawn>(PawnClass, spawnLocation, spawnRotation);
if (pawn != nullptr)
{
AMyPawn* defaultPawn = PawnClass.GetDefaultObject();
int32 materialCount = defaultPawn->Materials.Num();
int m = FMath::RandRange(0, materialCount - 1);
const TArray<UActorComponent*>& theComponents = pawn->GetComponents();
int32 componentCount = theComponents.Num();
for (int32 x = 0; x < componentCount; x++)
{
USkeletalMeshComponent* mesh = Cast<USkeletalMeshComponent>(theComponents[x]);
if (mesh != nullptr)
{
mesh->SetMaterial(0, defaultPawn->Materials[m]);
}
}
TAGGED IN
.h (located in pawn manager responsible for spawning them)
.cpp