Files
XCEngine/docs/api/XCEngine/Components/GameObject/GetComponentInParent.md

31 lines
802 B
Markdown
Raw Normal View History

2026-03-26 16:45:24 +08:00
# GameObject::GetComponentInParent
2026-04-03 16:11:48 +08:00
沿父链向上查找第一个匹配组件。
2026-03-26 16:45:24 +08:00
```cpp
2026-04-03 16:11:48 +08:00
template<typename T>
T* GetComponentInParent();
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
1. 先看当前对象是否存在父节点
2. 对父对象调用 [GetComponent](GetComponent.md)
3. 若父对象未命中,则继续递归向更高层父节点查询
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
- 搜索范围不包含当前对象自己
- 若父对象存在匹配项,就不会继续往更高祖先找
- 对宽类型查询,父对象的 `TransformComponent` 仍可能优先命中
2026-03-26 16:45:24 +08:00
## 相关文档
2026-04-03 16:11:48 +08:00
- [GetComponent](GetComponent.md)
- [GetParent](GetParent.md)
- [GetComponentInChildren](GetComponentInChildren.md)