2026-03-26 16:45:24 +08:00
|
|
|
# GameObject::GetTransform
|
|
|
|
|
|
2026-04-03 16:11:48 +08:00
|
|
|
返回当前对象的内建 `TransformComponent`。
|
2026-03-26 16:45:24 +08:00
|
|
|
|
2026-04-03 16:11:48 +08:00
|
|
|
该方法当前提供两个公开重载:
|
2026-03-26 16:45:24 +08:00
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
TransformComponent* GetTransform();
|
2026-04-03 16:11:48 +08:00
|
|
|
const TransformComponent* GetTransform() const;
|
2026-03-26 16:45:24 +08:00
|
|
|
```
|
|
|
|
|
|
2026-04-03 16:11:48 +08:00
|
|
|
## 行为说明
|
2026-03-26 16:45:24 +08:00
|
|
|
|
2026-04-03 16:11:48 +08:00
|
|
|
两个重载都直接返回底层 `m_transform` 指针。
|
2026-03-26 16:45:24 +08:00
|
|
|
|
2026-04-03 16:11:48 +08:00
|
|
|
按当前对象模型:
|
2026-03-26 16:45:24 +08:00
|
|
|
|
2026-04-03 16:11:48 +08:00
|
|
|
- 每个 `GameObject` 构造时都会创建一个 `TransformComponent`
|
|
|
|
|
- `AddComponent<TransformComponent>()` 也只是返回这同一份实例
|
|
|
|
|
- `RemoveComponent()` 不允许移除它
|
2026-03-26 16:45:24 +08:00
|
|
|
|
2026-04-03 16:11:48 +08:00
|
|
|
因此在正常存活期内,这个接口应始终返回非空指针。
|
2026-03-26 16:45:24 +08:00
|
|
|
|
2026-04-03 16:11:48 +08:00
|
|
|
## 为什么它不是普通组件查询的简单别名
|
2026-03-26 16:45:24 +08:00
|
|
|
|
2026-04-03 16:11:48 +08:00
|
|
|
虽然 [GetComponent](GetComponent.md) 也可能返回 `TransformComponent`,但 `GetTransform()` 更明确:
|
2026-03-26 16:45:24 +08:00
|
|
|
|
2026-04-03 16:11:48 +08:00
|
|
|
- 它直接表达“我要对象的内建 Transform”
|
|
|
|
|
- 不受模板匹配与搜索顺序歧义影响
|
|
|
|
|
- 对编辑器、脚本桥接和空间变换代码更可读
|
2026-03-26 16:45:24 +08:00
|
|
|
|
|
|
|
|
## 相关文档
|
|
|
|
|
|
2026-04-03 16:11:48 +08:00
|
|
|
- [GetComponent](GetComponent.md)
|
|
|
|
|
- [AddComponent](AddComponent.md)
|
|
|
|
|
- [TransformComponent](../TransformComponent/TransformComponent.md)
|