🎯 目标:
掌握 Unreal Engine 5 组件(Component)系统,为 Actor 添加组件,增强游戏对象功能!
1️⃣ 什么是组件(Component)?
在 Unreal Engine 5(UE5) 中,组件(Component) 是 Actor 的构建模块,所有 Actor 本身并不具备功能,而是通过 添加组件 实现各种能力,如:
- 网格(Mesh) → 让 Actor 拥有 3D 形态
- 灯光(Light) → 让 Actor 发光
- 摄像机(Camera) → 让 Actor 作为视角
- 声音(Audio) → 让 Actor 播放音效
通过组件,你可以 自定义 Actor 的行为,扩展其能力,让游戏世界更具表现力!🎮
2️⃣ 组件的层级结构
组件存在 层级关系(Hierarchy),每个 Actor 都有一个 根组件(Root Component),其他组件 附加(Attach) 在它上面。例如:
[Actor: MyCharacter]
├── [RootComponent: CapsuleComponent]
├── [MeshComponent: SkeletalMesh]
├── [CameraComponent: FollowCamera]
├── [AudioComponent: FootstepSound]
这样,我们可以让摄像机跟随角色,或让音效随 Actor 移动。
3️⃣ 为 Actor 添加组件(C++ 方式)
🔹 创建自定义 Actor
- 在 UE5 中,点击 “工具” → “新建 C++ 类”。
- 选择 Actor 作为基类,命名为 MyComponentActor。
- 点击 创建并添加到项目,等待 UE5 生成代码并打开 Visual Studio。
🔹 添加组件(Static Mesh & Light)
📌 修改 MyComponentActor.h(头文件),为 Actor 添加 StaticMesh 和 PointLight:
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/StaticMeshComponent.h"
#include "Components/PointLightComponent.h"
#include "MyComponentActor.generated.h"
UCLASS()
class MYFIRSTCPPGAME_API AMyComponentActor : public AActor
{
GENERATED_BODY()
public:
AMyComponentActor();
protected:
virtual void BeginPlay() override;
private:
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* MeshComponent;
UPROPERTY(VisibleAnywhere)
UPointLightComponent* LightComponent;
};
📌 修改 MyComponentActor.cpp(实现文件),初始化并配置组件:
#include "MyComponentActor.h"
#include "UObject/ConstructorHelpers.h"
AMyComponentActor::AMyComponentActor()
{
PrimaryActorTick.bCanEverTick = true;
// 创建静态网格组件(Static Mesh)
MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent"));
RootComponent = MeshComponent; // 设置为根组件
// 加载默认立方体模型
static ConstructorHelpers::FObjectFinder<UStaticMesh> MeshAsset(TEXT("/Engine/BasicShapes/Cube.Cube"));
if (MeshAsset.Succeeded())
{
MeshComponent->SetStaticMesh(MeshAsset.Object);
}
// 创建灯光组件(Point Light)
LightComponent = CreateDefaultSubobject<UPointLightComponent>(TEXT("LightComponent"));
LightComponent->SetupAttachment(MeshComponent); // 让灯光附加到网格
LightComponent->SetLightColor(FLinearColor::Red);
LightComponent->SetIntensity(5000.0f);
}
4️⃣ 编译 & 运行
🔹 编译 C++ 代码
- 在 Visual Studio 中,点击 编译(Build) 或按 Ctrl + Shift + B。
- 返回 UE5,点击 编译按钮。
🔹 在关卡中使用 - 回到 UE5 编辑器,在 内容浏览器 中找到 MyComponentActor。
- 拖拽 MyComponentActor 进入场景,你会看到:
- 一个 立方体 出现在世界中!
- 它带有一个 红色光源!
✅ 你已经成功为 Actor 添加组件,让它变得更强大!🎉
5️⃣ 让组件动态变化
🔹 修改灯光颜色
我们可以在 Tick() 函数中 动态修改灯光颜色:
void AMyComponentActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// 让灯光颜色不断变化
float Time = GetWorld()->GetTimeSeconds();
FLinearColor DynamicColor = FLinearColor(FMath::Sin(Time), FMath::Cos(Time), 0.0f, 1.0f);
LightComponent->SetLightColor(DynamicColor);
}
🎨 运行后,灯光会不断变化颜色,效果超酷!
6️⃣ 其他常用组件
组件类型 | 作用 |
---|---|
UStaticMeshComponent | 显示 3D 模型 |
USkeletalMeshComponent | 角色骨骼动画 |
UCameraComponent | 摄像机视角 |
UPointLightComponent | 点光源 |
UAudioComponent | 播放音效 |
UBoxComponent | 碰撞检测 |
这些组件可以 自由组合,创造各种 复杂的 Actor!🚀
7️⃣ 常见问题 & 解决方案
问题 | 解决方案 |
---|---|
组件没有出现在场景中 | 确保 RootComponent 设置正确 |
立方体未加载 | 确保路径 /Engine/BasicShapes/Cube.Cube 正确 |
光源颜色未改变 | SetLightColor() 需要 FLinearColor 格式 |
🎯 总结:今天你学到了什么?
✅ 理解组件(Component)在 UE5 中的作用
✅ 使用 C++ 创建并管理组件(Static Mesh, Light)
✅ 通过 Tick() 让组件动态变化
✅ 探索常用组件,增强 Actor 的功能
👏 恭喜!你的 Actor 现在更加强大了!🎮 明天我们将探索 Tick() 函数的魔法,让游戏对象动起来!🚀
📌 记得收藏专栏,每天进步一点,最终独立开发属于你的 UE5 游戏!🔥