Unreal
- 🏛 Unreal Engine - USceneComponent
- 📚 定義
- 🏷 類繼承
- ? 關鍵特性
- ?? 常見配置
- 🛠? 使用方法
- 🔗 創建與掛載
- 🔄 獲取與修改 Transform
- 🧩 附加/分離組件
- 🏊 典型應用場景
- 🤖 與其他組件對比
- 📝 小結
🏛 Unreal Engine - USceneComponent
📚 定義
USceneComponent
是 所有具有空間變換(位置、旋轉、縮放)的組件基類。
它為 組件的三維空間定位與層級管理 提供支持,是 場景樹系統的核心。
在 Unreal 中,任何需要在 3D 世界中擁有 Transform(位置、旋轉、縮放) 的組件,通常都會繼承自 USceneComponent
。
🏷 類繼承
UObject└── UActorComponent└── USceneComponent
UObject
→ 最底層對象系統UActorComponent
→ 提供生命周期與邏輯管理USceneComponent
→ 增加 Transform 與層級關系- 常見子類包括:
UPrimitiveComponent
、UCameraComponent
、UMeshComponent
? 關鍵特性
- ? Transform 支持:
Location
、Rotation
、Scale
- ? 層級系統:Parent / Child 關系(SceneComponent 可以附加到另一個 SceneComponent)
- ? Attach/Detach 支持:可在運行時改變組件層級
- ? Socket/Attachment Rules:支持相對/絕對變換的掛載規則
- ? 更新與傳播:變換可自動傳遞給子組件
- ? 物理與渲染的基礎:許多渲染和物理組件都繼承自
USceneComponent
?? 常見配置
RelativeLocation
/RelativeRotation
/RelativeScale3D
→ 相對父組件的變換WorldLocation
/WorldRotation
/WorldScale
→ 世界空間下的變換AttachParent
→ 指定父級 SceneComponentAttachChildren
→ 當前組件的子組件列表Mobility
→ 移動性設置- Static:不可移動
- Stationary:部分可變(如燈光強度)
- Movable:完全可移動
🛠? 使用方法
🔗 創建與掛載
// 在 Actor 構造函數中
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComp"));USceneComponent* ChildComp = CreateDefaultSubobject<USceneComponent>(TEXT("ChildComp"));
ChildComp->SetupAttachment(RootComponent);
🔄 獲取與修改 Transform
FVector Location = GetActorLocation();
SetWorldLocation(FVector(100, 0, 50));FRotator Rot = GetComponentRotation();
AddLocalRotation(FRotator(0, 90, 0));
🧩 附加/分離組件
ChildComp->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);
ChildComp->DetachFromComponent(FDetachmentTransformRules::KeepWorldTransform);
🏊 典型應用場景
- 🎮 構建 Actor 層級結構:例如角色的 根組件 → 骨骼網格體 → 武器掛點
- 🏗 藍圖節點系統:場景中的各種可視化節點都繼承
USceneComponent
- 📸 相機/燈光組件:常見的相機、聚光燈、點光源都是
USceneComponent
的子類 - ?? 掛載機制:例如武器附著到角色手上
🤖 與其他組件對比
- UActorComponent
- 只提供邏輯功能,無 Transform
- USceneComponent
- 增加空間變換與層級支持
- UPrimitiveComponent
- 繼承自
USceneComponent
,并增加 渲染 & 碰撞 能力
- 繼承自
- UMeshComponent
- 專門用于渲染網格
📝 小結
USceneComponent
是 場景層級與空間變換的核心基類。- 提供 Transform(位置、旋轉、縮放)管理,并支持 父子層級關系。
- 是許多常用組件(Mesh、Camera、Light)的基礎。
- 在構建復雜 Actor 組件樹 和 掛載系統 時必不可少。