docs: expand gameobject helper docs

This commit is contained in:
2026-04-03 16:11:48 +08:00
parent ee44e14960
commit 2c2e1fab1c
34 changed files with 597 additions and 603 deletions

View File

@@ -1,31 +1,44 @@
# GameObject::AddComponent
添加元素或建立关联
为当前对象添加一个普通组件,或返回已有的内建 `Transform`
```cpp
template<typename T, typename... Args> T* AddComponent(Args&&... args);
template<typename T, typename... Args>
T* AddComponent(Args&&... args);
```
该方法声明于 `XCEngine/Components/GameObject.h`,当前页面用于固定 `GameObject` 类目录下的方法级 canonical 路径。
## 行为说明
**参数:**
- `args` - 参数语义详见头文件声明。
当前模板实现分两条路径:
**返回:** `template<typename T, typename... Args> T*` - 返回值语义详见头文件声明。
- 如果 `T` 继承自 `TransformComponent`
- 直接返回已有 `m_transform`
- 不会创建第二个 `Transform`
- 否则
- 构造一个新的组件实例
- 回填 `component->m_gameObject = this`
- 放入 `m_components`
- 返回新组件指针
**示例:**
## 当前不会自动发生什么
```cpp
#include <XCEngine/Components/GameObject.h>
运行时添加普通组件时,当前实现**不会自动补发**
void Example() {
XCEngine::Components::GameObject object;
// 根据上下文补齐参数后调用 GameObject::AddComponent(...)。
(void)object;
}
```
- `Awake()`
- `Start()`
- `OnEnable()`
这是当前对象系统最重要的现实边界之一。组件挂载完成,不等于它已经追平生命周期进度。
## 所有权语义
- 普通组件的所有权由 `GameObject` 内部 `std::unique_ptr<Component>` 接管
- 返回值只是观察指针
- 调用方不应自行 `delete`
## 相关文档
- [返回类总览](GameObject.md)
- [返回模块目录](../Components.md)
- [GameObject](GameObject.md)
- [GetComponent](GetComponent.md)
- [RemoveComponent](RemoveComponent.md)
- [GetTransform](GetTransform.md)