下雪了

【UE4】加载材质、形状的方法

利用ConstructorHelpers::FObjectFinder模板来从UE4的文件引用中获取所需素材,然后对Actor、组件等进行设置。

以Tetris_UE中的IBlock为例,加载以下材质。

//加载形状
static ConstructorHelpers::FObjectFinder<UStaticMesh> Basic_Block(TEXT("StaticMesh'/Game/Shapes/Basic_Block.Basic_Block'"));
if (Basic_Block.Succeeded())
{
    IB_Left->SetStaticMesh(Basic_Block.Object);
    IB_Center->SetStaticMesh(Basic_Block.Object);
    IB_Right->SetStaticMesh(Basic_Block.Object);
    IB_RightRight->SetStaticMesh(Basic_Block.Object);
}

//加载材质
static ConstructorHelpers::FObjectFinder<UMaterial> IBlock_Material(TEXT("Material'/Game/Materials/M_IBlock.M_IBlock'"));
if (IBlock_Material.Succeeded())
{
    IB_Left->SetMaterial(0,IBlock_Material.Object);
    IB_Center->SetMaterial(0,IBlock_Material.Object);
    IB_Right->SetMaterial(0, IBlock_Material.Object);
    IB_RightRight->SetMaterial(0, IBlock_Material.Object);
}

如需在蓝图中调用,除了要加上宏 UFUNCTION(BlueprintCallable) 之外,参数类型应该是 UMaterialInterface* (材质接口)

//设定方块材质
void ABlock::SetBlockMaterial(UMaterialInterface* material)
{
    for (int i = 0; i < MeshList.Num(); i++)
    {
        MeshList[i]->SetMaterial(0, material);
    }
}

 


Add Your Comment

* Indicates Required Field

Your email address will not be published.

*