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

2023-12-21 11:40:27

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
在这里插入图片描述

文章来源:https://blog.csdn.net/qq_40120946/article/details/135124651
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。