第 4 天:组件(Component)详解,让 Actor 变得更强大!

🎯 目标:

掌握 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

  1. 在 UE5 中,点击 “工具” → “新建 C++ 类”。
  2. 选择 Actor 作为基类,命名为 MyComponentActor。
  3. 点击 创建并添加到项目,等待 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++ 代码

  1. 在 Visual Studio 中,点击 编译(Build) 或按 Ctrl + Shift + B。
  2. 返回 UE5,点击 编译按钮。
    🔹 在关卡中使用
  3. 回到 UE5 编辑器,在 内容浏览器 中找到 MyComponentActor。
  4. 拖拽 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 游戏!🔥

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bluesonli

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值