UE5 C++(八)— 创建Actor、添加组件

Actor和组件介绍

Actor

详细介绍可以去看 Actor和几何体官方文档

还有大钊的这篇文章《InsideUE4》GamePlay架构(一)Actor和Component

所有可以放入关卡的对象都是 Actor,比如摄像机、静态网格体、玩家起始位置。Actor支持三维变换,例如平移、旋转和缩放。你可以通过游戏逻辑代码(C++或蓝图)创建(生成)或销毁Actor。

在C++中,AActor是所有Actor的基类。
请添加图片描述

组件(Component)

详细介绍可以去看 组件官方文档

还有大钊的这篇文章《InsideUE4》GamePlay架构(一)Actor和Component

组件(Component) 是可以添加到Actor上的一项功能。

当你为Actor添加组件后,该Actor便获得了该组件所提供的功能。例如:

  • 聚光灯组件(Spot Light Component)允许你的Actor像聚光灯一样发光,
  • 旋转移动组件(Rotating Movement Component)能使你的Actor四处旋转,
  • 音频组件(Audio Component)将使你的Actor能够播放声音。

组件必须绑定在Actor身上,它们无法单独存在。

请添加图片描述

在蓝图中创建Actor,添加组件

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在C++中创建Actor,添加组件

创建一个ActorC++类 — MyCustomActor
在这里插入图片描述
MyCustomActor.h

#pragma once

#include "CoreMinimal.h"
// 引入组件
#include "Components/SceneComponent.h"
#include "Components/StaticMeshComponent.h"
#include "Components/BoxComponent.h"
#include "Components/AudioComponent.h"
#include "Particles/ParticleSystemComponent.h"

#include "GameFramework/Actor.h"
#include "MyCustomActor.generated.h"

UCLASS()
class DEMO_API AMyCustomActor : public AActor
{
	GENERATED_BODY()

public:
	// Sets default values for this actor's properties
	AMyCustomActor();

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")
	USceneComponent *SceneComponent;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MySceneComponent")
	UStaticMeshComponent *StaticMeshComponent;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MySceneComponent")
	UBoxComponent *BoxComponent;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MySceneComponent")
	UAudioComponent *AudioComponent;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MySceneComponent")
	UParticleSystemComponent *ParticleSystemComponent;
};

MyCustomActor.cpp


#include "MyCustomActor.h"

// Sets default values
AMyCustomActor::AMyCustomActor()
{
	// 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;

	// 创建组件
	SceneComponent = CreateDefaultSubobject<USceneComponent>(TEXT("CustomScene"));
	StaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CustomStaticMesh"));
	BoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("CustomBox"));
	AudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("CustomAudio"));
	ParticleSystemComponent = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("CustomParticleSystem"));

	// 把组件添加到根组件
	RootComponent = SceneComponent;
	StaticMeshComponent->SetupAttachment(SceneComponent);
	BoxComponent->SetupAttachment(SceneComponent);
	AudioComponent->SetupAttachment(BoxComponent);
	ParticleSystemComponent->SetupAttachment(SceneComponent);
}

// Called when the game starts or when spawned
void AMyCustomActor::BeginPlay()
{
	Super::BeginPlay();
}

// Called every frame
void AMyCustomActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

编译后,创建蓝图BP_MyCustomActor
在这里插入图片描述

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值