uscenecomponent

USceneComponent

🏛 Unreal Engine - USceneComponent

📚 定义

USceneComponent所有具有空间变换(位置、旋转、缩放)的组件基类

它为 组件的三维空间定位与层级管理 提供支持,是 场景树系统的核心

在 Unreal 中,任何需要在 3D 世界中拥有 Transform(位置、旋转、缩放) 的组件,通常都会继承自 USceneComponent


🏷 类继承

UObject

└── UActorComponent

    └── USceneComponent

  • UObject → 最底层对象系统
  • UActorComponent → 提供生命周期与逻辑管理
  • USceneComponent → 增加 Transform 与层级关系
  • 常见子类包括:UPrimitiveComponentUCameraComponentUMeshComponent

⚡ 关键特性

  • Transform 支持LocationRotationScale
  • 层级系统:Parent / Child 关系(SceneComponent 可以附加到另一个 SceneComponent)
  • Attach/Detach 支持:可在运行时改变组件层级
  • Socket/Attachment Rules:支持相对/绝对变换的挂载规则
  • 更新与传播:变换可自动传递给子组件
  • 物理与渲染的基础:许多渲染和物理组件都继承自 USceneComponent

⚙️ 常见配置

  • RelativeLocation / RelativeRotation / RelativeScale3D → 相对父组件的变换
  • WorldLocation / WorldRotation / WorldScale → 世界空间下的变换
  • AttachParent → 指定父级 SceneComponent
  • AttachChildren → 当前组件的子组件列表
  • Mobility → 移动性设置
    • Static:不可移动
    • Stationary:部分可变(如灯光强度)
    • Movable:完全可移动

🛠️ 使用方法

🔗 创建与挂载

1
2
3
4
5
// 在 Actor 构造函数中
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComp"));

USceneComponent* ChildComp = CreateDefaultSubobject<USceneComponent>(TEXT("ChildComp"));
ChildComp->SetupAttachment(RootComponent);

🔄 获取与修改 Transform

1
2
3
4
5
FVector Location = GetActorLocation();
SetWorldLocation(FVector(100, 0, 50));

FRotator Rot = GetComponentRotation();
AddLocalRotation(FRotator(0, 90, 0));

🧩 附加/分离组件

1
2
ChildComp->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);
ChildComp->DetachFromComponent(FDetachmentTransformRules::KeepWorldTransform);

🏊 典型应用场景

  • 🎮 构建 Actor 层级结构:例如角色的 根组件 → 骨骼网格体 → 武器挂点
  • 🏗 蓝图节点系统:场景中的各种可视化节点都继承 USceneComponent
  • 📸 相机/灯光组件:常见的相机、聚光灯、点光源都是 USceneComponent 的子类
  • ⚙️ 挂载机制:例如武器附着到角色手上

🤖 与其他组件对比

  • UActorComponent
    • 只提供逻辑功能,无 Transform
  • USceneComponent
    • 增加空间变换与层级支持
  • UPrimitiveComponent
    • 继承自 USceneComponent,并增加 渲染 & 碰撞 能力
  • UMeshComponent
    • 专门用于渲染网格

📝 小结

  • USceneComponent场景层级与空间变换的核心基类
  • 提供 Transform(位置、旋转、缩放)管理,并支持 父子层级关系
  • 是许多常用组件(Mesh、Camera、Light)的基础。
  • 在构建复杂 Actor 组件树挂载系统 时必不可少。