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

34 lines
907 B
Markdown
Raw Normal View History

2026-03-26 16:45:24 +08:00
# GameObject::DetachChildren
2026-04-03 16:11:48 +08:00
把当前对象的所有直接子对象全部分离出去,并保持它们的世界空间变换。
2026-03-26 16:45:24 +08:00
```cpp
void DetachChildren();
```
2026-04-03 16:11:48 +08:00
## 行为说明
2026-03-26 16:45:24 +08:00
2026-04-03 16:11:48 +08:00
当前实现会先复制一份 `m_children`,然后对副本中的每个子对象调用:
2026-03-26 16:45:24 +08:00
2026-04-03 16:11:48 +08:00
```cpp
child->SetParent(nullptr, true);
```
2026-03-26 16:45:24 +08:00
2026-04-03 16:11:48 +08:00
这么做的目的,是避免在遍历过程中直接修改原始 `m_children` 容器导致迭代失效。
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
调用完成后:
- 当前对象的直接子列表会被清空
- 每个原子对象都会变成根对象,或在无场景时变成无父独立对象
- 每个子对象都会尽量保持原世界空间变换
`tests/Components/test_game_object.cpp` 中的 `DetachChildren` 已验证父对象的 `GetChildCount()` 会从原值变成 `0`
2026-03-26 16:45:24 +08:00
## 相关文档
2026-04-03 16:11:48 +08:00
- [DetachFromParent](DetachFromParent.md)
- [SetParent](SetParent.md)
- [GetChildren](GetChildren.md)