|
思路:1.可通过关卡蓝图获取获取场景中的所有actor,并且可以获得actor的名称 for (int32 ActorIdx = 0; ActorIdx GWorld- GetCurrentLevel()- Actors.Num(); ++ActorIdx)
{
AActor* pActor = GWorld- GetCurrentLevel()- Actors[ActorIdx];
if (pActor == NULL) continue;
FString ActorName = pActor- GetName();
}
}
2.根据蓝图名字查找要修改的东西属于那个actor,再从actor里查找需要修改的组件
UClass*TargetClass = pActor- GetActorClass();
bool const bIsBlueprintClass = (Cast UBlueprintGeneratedClass (TargetClass) != nullptr);
if (bIsBlueprintClass)
{
TInlineComponentArray UActorComponent* SceneComponents;
//获取所有子组件
pActor- GetComponents(SceneComponents);
for (int i = 0; i SceneComponents.Num(); ++i)
{
UActorComponent* SceneComp = SceneComponents;
//替换第一级组件的材质纹理
if (index == 0 SceneComp)
{
// 获取原组件指针
UStaticMeshComponent* StaticActor = CastChecked UStaticMeshComponent (SceneComp);
if (StaticActor)
{
//替换原组件使用的纹理
}
}
3.替换原组件使用的纹理
//根据新路径加载资源
UTexture* newTexture = LoadObject UTexture (nullptr, pAssetPath, nullptr, LOAD_None, nullptr);
TArray UMaterialInterface* UsedMaterials;
//静态网格可能有N个材质
InStaticMeshComponent- GetUsedMaterials(UsedMaterials);
for (int32 MatIndex = 0; MatIndex UsedMaterials.Num(); MatIndex++)
{
UMaterialInterface* Material = UsedMaterials[MatIndex];
UMaterialInstanceDynamic*MaterialInstance = Cast UMaterialInstanceDynamic (Material);
if (MaterialInstance != NULL)
{
MaterialInstance- SetTextureParameterValue( basecolor_texture , newTexture);
}
}
上述代码是替换组件纹理的代码,如果是变量的话,有另外一套写法,不一定是最简单的方法,也是个可行的方案!希望大家共同研究! |
|