24 lines
472 B
Markdown
24 lines
472 B
Markdown
|
|
# GameObject::GetComponent
|
||
|
|
|
||
|
|
获取游戏对象上指定类型的组件。
|
||
|
|
|
||
|
|
```cpp
|
||
|
|
template<typename T>
|
||
|
|
T* GetComponent();
|
||
|
|
|
||
|
|
template<typename T>
|
||
|
|
const T* GetComponent() const;
|
||
|
|
```
|
||
|
|
|
||
|
|
使用 dynamic_cast 在组件列表中查找匹配类型的第一个组件。
|
||
|
|
|
||
|
|
**模板参数:**
|
||
|
|
- `T` - 组件类型
|
||
|
|
|
||
|
|
**返回:** `T*` - 找到的组件指针,如果未找到则返回 nullptr
|
||
|
|
|
||
|
|
## 相关文档
|
||
|
|
|
||
|
|
- [GameObject 总览](game-object.md)
|
||
|
|
- [GetComponents](get-components.md)
|