1.1在UE中新建C++Actor类,并添加头文件(Actor.h)
#include "Components/SceneComponent.h"//场景组件
#include "Components/StaticMeshComponent.h"//静态网格体组件
#include "Components/BoxComponent.h"//盒体碰撞组件
#include "Particles/ParticleSystemComponent.h"//粒子系统组件
#include "Components/AudioComponent.h"//音频组件
1.2在Public声明组件(Actor.h)
//在组件内声明场景组件
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = MySceneComponent)
class USceneComponent* MyScene;
//在组件内声明静态网格体组件
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = MySceneComponent)
class UStaticMeshComponent* MyMesh;
//在组件内声明粒子系统组件
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = MySceneComponent)
class UParticleSystemComponent* MyParticle;
//在组件内声明盒体碰撞组件
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = MySceneComponent)
class UBoxComponent* MyBox;
//在组件内声明音频组件
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = MySceneComponent)
class UAudioComponent* MyAudio;
1.3在构造函数里初始化(Actor.cpp)
//初始化组件
MyScene = CreateDefaultSubobject<USceneComponent>(TEXT("MyCustomScene"));
MyMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyCustomStaticMesh"));
MyParticle = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("MyCustomParticleSystem"));
MyBox = CreateDefaultSubobject<UBoxComponent>(TEXT("MyCustomBox"));
MyAudio = CreateDefaultSubobject<UAudioComponent>(TEXT("MyCustomAudio"));
//将场景组件设置为根组件
RootComponent = MyScene;
MyMesh->SetupAttachment(MyScene);//将网格体组件设置为MyScene子组件
MyParticle->SetupAttachment(MyScene);//将粒子组件设置为MyScene子组件
MyBox->SetupAttachment(MyScene);//将盒体碰撞组件设置为MyScene子组件
MyAudio->SetupAttachment(MyBox);//将网格体组件设置为MyBox子组件
1.4基于C++创建蓝图类
1.5 父子级关系
2.1 使用模板(MyActor.h)
//模板声明变量
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = MyClass)
TSubclassOf<AActor>MyActor;
2.2静态加载资源必须写在构造函数里(MyActor.cpp)
//静态加载资源(复制引用对象)
static ConstructorHelpers::FObjectFinder<UStaticMesh>TempStaticMesh(TEXT("/Script/Engine.StaticMesh'/Game/StarterContent/Shapes/Shape_Cone.Shape_Cone'"));
MyMesh->SetStaticMesh(TempStaticMesh.Object);//设置静态网格体组件
//静态加载资源(复制引用对象)
static ConstructorHelpers::FObjectFinder<UParticleSystem>TempParticleSystem(TEXT("/Script/Engine.ParticleSystem'/Game/StarterContent/Particles/P_Explosion.P_Explosion'"));
MyParticle->SetTemplate(TempParticleSystem.Object);//设置粒子系统组件
//静态加载资源(复制引用对象)
static ConstructorHelpers::FObjectFinder<USoundWave>TempSound(TEXT("/Script/Engine.SoundWave'/Game/StarterContent/Audio/Collapse01.Collapse01'"));
MyAudio->SetSound(TempSound.Object);//设置音频组件
//静态加载类(复制引用对象,引用结尾必须加"_C")
static ConstructorHelpers::FClassFinder<AActor>TempMyActor(TEXT("/Script/Engine.Blueprint'/Game/StarterContent/Blueprints/Blueprint_CeilingLight.Blueprint_CeilingLight_C'"));
MyActor = TempMyActor.Class;
3.1动态加载资源写在BeginPlay里(MyActor.cpp)
//动态加载资源
UStaticMesh* MyTmpStaticMesh = LoadObject<UStaticMesh>(nullptr, TEXT("/Script/Engine.StaticMesh'/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube'"));
if (MyTmpStaticMesh) {//
MyMesh->SetStaticMesh(MyTmpStaticMesh);
}
//动态加载类资源(复制引用对象,引用结尾必须加"_C")
UClass* MyTmpClass = LoadClass<AActor>(this, TEXT("/Script/Engine.Blueprint'/Game/StarterContent/Blueprints/Blueprint_WallSconce.Blueprint_WallSconce_C'"));
if (MyTmpClass) {
AActor* SpawnActor = GetWorld()->SpawnActor<AActor>(MyTmpClass, FVector::ZeroVector, FRotator::ZeroRotator);
}