在ue中创建actorc++类,在actor的.h文件中添加五个组件
又由上到下的作用分别为:
获取下SceneComponent,用于操作其Transform等相应接口。
获取静态模型组件。
获取盒子碰撞组件。
获取粒子特效组件。
获取音频组件。
#include"Components/SceneComponent.h"
#include"Components/StaticMeshComponent.h"
#include"Components/BoxComponent.h"
#include"Particles/ParticleSystemComponent.h"
#include"Components/AudioComponent.h"
actor的.h在class里面声明我们的变量
UCLASS()
class CFIRST_API AMyActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
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;
};
在CPP里面声明
AMyActor::AMyActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
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"));
}
然后也是在AMyActor::AMyActor()里设置父子关系
//设置父子级关系
//设置MySence设置为默认根组件
RootComponent = MyScene;
//设置MyMesh为MySence的儿子
MyMesh->SetupAttachment(MyScene);
MyParticle->SetupAttachment(MyScene);
MyBox->SetupAttachment(MyScene);
MyAudio->SetupAttachment(MyBox);
实现的效果为:
笔记总结来源于:【【虚幻5】UE5C++零基础全网全流程开发从入门到进阶教程合集(持续跟新中)】https://www.bilibili.com/video/BV1Dc411f7nx?vd_source=90cb1ac44856e5e826e2bee8aa9d8a41
标签:actor,TEXT,AMyActor,C++,组件,Actor,UE,MyScene,class From: https://blog.csdn.net/weixin_65376163/article/details/137604257