UEC++Actor添加组件初始化和静动态加载资源

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);
}

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Woody_Dark

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值