0.头
#include "Components/StaticMeshComponent.h"
1.定义
UPROPERTY(VisibleAnywhere, Category = Body)
UStaticMeshComponent* EnemyBody;
2.创建
EnemyBody = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Body"));
这个括号里接受的是FName类型,注意转换
例如:
FString BlockName = TEXT("block") + FString::FromInt(i); Block[i] = CreateDefaultSubobject<UStaticMeshComponent>(FName(*BlockName));
3.找
static ConstructorHelpers::FObjectFinder<UStaticMesh>CylinderObj(TEXT("'/Game/StarterContent/Shapes/Shape_Cylinder'"));//#include "UObject/ConstructorHelpers.h" if (CylinderObj.Succeeded()) { EnemyBody->SetStaticMesh(CylinderObj.Object); }
4.设置
EnemyBody->SetRelativeScale3D(FVector(0.7f, 0.7f, 1.0f)); EnemyBody->SetRelativeLocation(FVector(0, 0, -50)); EnemyBody->SetupAttachment(RootComponent);
//EnemyBody->SetVisibility(false); wow这个方法可以控制staticmesh组件的可见哈哈哈终于找到了!
5.关于材质
5.1声明
class UMaterialInterface* DefaultMaterial; class UMaterialInterface* VulnerableMaterial;
5.2赋值
5.2.1从Actor实例上获取
DefaultMaterial = EnemyBody->GetMaterial(0);
5.2.2从本地文件获取
static ConstructorHelpers::FObjectFinder<UMaterial>VulnerableMat(TEXT("Material'/Game/Materials/M_Enemy_Vulnerable.M_Enemy_Vulnerable'"));
if (VulnerableMat.Succeeded()) {
VulnerableMaterial = Cast< UMaterialInterface>(VulnerableMat.Object);
}
5.3使用
EnemyBody->SetMaterial(0, VulnerableMaterial); EnemyBody->SetMaterial(0, DefaultMaterial);
以上