31 lines
912 B
Markdown
31 lines
912 B
Markdown
# GameObject::GetComponentInChildren
|
|
|
|
在当前对象及其子树中查找第一个匹配组件。
|
|
|
|
```cpp
|
|
template<typename T>
|
|
T* GetComponentInChildren();
|
|
```
|
|
|
|
## 搜索顺序
|
|
|
|
当前实现按深度优先方式搜索:
|
|
|
|
1. 先对当前对象自身调用 [GetComponent](GetComponent.md)
|
|
2. 若当前对象没找到,再按 `m_children` 当前顺序递归搜索每个子对象
|
|
3. 找到第一个命中项后立即返回
|
|
|
|
## 当前语义边界
|
|
|
|
- 搜索范围包含“当前对象自己”
|
|
- `TransformComponent` 仍然可能因 [GetComponent](GetComponent.md) 的特例路径而最先命中
|
|
- 返回的是第一个匹配项,不会收集全部结果
|
|
|
|
如果你需要收集整个子树中的所有匹配项,应使用 [GetComponentsInChildren](GetComponentsInChildren.md)。
|
|
|
|
## 相关文档
|
|
|
|
- [GetComponent](GetComponent.md)
|
|
- [GetComponentsInChildren](GetComponentsInChildren.md)
|
|
- [GetChild](GetChild.md)
|