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

文章目录

  • Actor和组件介绍
    • Actor
    • 组件(Component)
  • 在蓝图中创建Actor,添加组件
  • 在C++中创建Actor,添加组件

Actor和组件介绍

Actor

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

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

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

在C++中,AActor是所有Actor的基类。
UE5 C++(八)— 创建Actor、添加组件_第1张图片

组件(Component)

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

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

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

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

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

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

UE5 C++(八)— 创建Actor、添加组件_第2张图片

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

UE5 C++(八)— 创建Actor、添加组件_第3张图片
UE5 C++(八)— 创建Actor、添加组件_第4张图片
UE5 C++(八)— 创建Actor、添加组件_第5张图片
UE5 C++(八)— 创建Actor、添加组件_第6张图片
UE5 C++(八)— 创建Actor、添加组件_第7张图片

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

创建一个ActorC++类 — MyCustomActor
UE5 C++(八)— 创建Actor、添加组件_第8张图片
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
UE5 C++(八)— 创建Actor、添加组件_第9张图片

你可能感兴趣的:(UE5,C++,入门开发,ue5,c++,开发语言)