Files
XCEngine/docs/api/components/game-object/game-object.md
ssdfasd f5a34f8adc docs: 重构 API 文档 - components 和 scene 模块
- components: 修复英文标题为中文,添加缺失组件文档
  - 新增 camera-component, light-component, audio-source-component, audio-listener-component 类总览
  - 修复 get-position.md 格式
  - 更新 components.md 模块总览
- scene: 修复方法文档格式,新增缺失方法
  - 修复 find.md, create-game-object.md 英文标题
  - 新增 FindByID, SerializeToString, DeserializeFromString 方法文档
  - 更新 scene.md 类总览方法列表
2026-03-26 01:50:27 +08:00

119 lines
3.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# GameObject
**命名空间**: `XCEngine::Components`
**类型**: `class`
**头文件**: `XCEngine/Components/GameObject.h`
**描述**: ECS 架构中的游戏对象实体,可附加组件并形成层级结构。
## 概述
GameObject 是 XCEngine ECS 架构中的实体Entity代表游戏世界中的一个个对象如角色、道具、光源等。每个 GameObject 可以通过 AddComponent 附加各种组件(如 TransformComponent、AudioSourceComponent来定义其行为和数据。GameObject 支持层级结构(父子关系)、名称查找、标签系统和生命周期管理。
## 类型别名
| 别名 | 类型 | 描述 |
|------|------|------|
| `ID` | `uint64_t` | 游戏对象唯一标识符类型 |
| `INVALID_ID` | `static constexpr ID` | 无效 ID 常量,值为 0 |
## 公共方法
### 基础信息
| 方法 | 描述 |
|------|------|
| [`GetID`](get-id.md) | 获取唯一标识符 |
| [`GetName`](get-name.md) | 获取名称 |
| [`SetName`](set-name.md) | 设置名称 |
| [`GetScene`](get-scene.md) | 获取所属场景 |
### 组件系统
| 方法 | 描述 |
|------|------|
| [`GetTransform`](get-transform.md) | 获取变换组件 |
| [`AddComponent`](add-component.md) | 添加组件(模板方法) |
| [`GetComponent`](get-component.md) | 获取组件(模板方法) |
| [`GetComponents`](get-components.md) | 获取所有指定类型组件(模板方法) |
| [`GetComponentInChildren`](get-component-in-children.md) | 在子对象中查找组件 |
| [`GetComponentInParent`](get-component-in-parent.md) | 在父对象中查找组件 |
| [`GetComponentsInChildren`](get-components-in-children.md) | 获取所有子对象的指定组件 |
### 层级结构
| 方法 | 描述 |
|------|------|
| [`GetParent`](get-parent.md) | 获取父对象 |
| [`SetParent`](set-parent.md) | 设置父对象 |
| [`GetChildCount`](get-child-count.md) | 获取子对象数量 |
| [`GetChild`](get-child.md) | 获取指定索引的子对象 |
| [`GetChildren`](get-children.md) | 获取所有子对象 |
| [`DetachChildren`](detach-children.md) | 分离所有子对象 |
### 激活状态
| 方法 | 描述 |
|------|------|
| [`IsActive`](is-active.md) | 检查自身激活状态 |
| [`SetActive`](set-active.md) | 设置激活状态 |
| [`IsActiveInHierarchy`](is-active-in-hierarchy.md) | 检查在层级中的激活状态 |
### 静态查找
| 方法 | 描述 |
|------|------|
| `static Find(name)` | 查找指定名称的对象 |
| `static FindObjectsOfType()` | 查找所有对象 |
| `static FindGameObjectsWithTag(tag)` | 查找具有指定标签的对象 |
### 生命周期
| 方法 | 描述 |
|------|------|
| [`Awake`](awake.md) | 唤醒对象 |
| [`Start`](start.md) | 开始对象 |
| [`Update`](update.md) | 每帧更新 |
| [`FixedUpdate`](fixed-update.md) | 固定频率更新 |
| [`LateUpdate`](late-update.md) | 晚更新 |
| [`OnDestroy`](on-destroy.md) | 销毁回调 |
| [`Destroy`](destroy.md) | 销毁对象 |
## 使用示例
```cpp
#include <XCEngine/Components/GameObject.h>
#include <XCEngine/Components/TransformComponent.h>
#include <XCEngine/Components/AudioSourceComponent.h>
using namespace XCEngine::Components;
void CreatePlayer() {
GameObject* player = new GameObject("Player");
auto transform = player->AddComponent<TransformComponent>();
transform->SetPosition(Vector3(0, 0, 0));
auto audio = player->AddComponent<AudioSourceComponent>();
audio->SetVolume(0.8f);
player->SetParent(cameraObject);
}
void FindAndOperate() {
GameObject* enemy = GameObject::Find("Enemy");
if (enemy) {
enemy->SetActive(false);
}
}
```
## 相关文档
- [Components 模块总览](../components.md) - Components 模块总览
- [Component](../component/component.md) - 组件基类
- [TransformComponent](../transform-component/transform-component.md) - 变换组件
- [Scene](../../scene/scene.md) - 场景