111 lines
3.0 KiB
Markdown
111 lines
3.0 KiB
Markdown
|
|
# 对象池与运行时对象管理
|
|||
|
|
|
|||
|
|
## 分类
|
|||
|
|
|
|||
|
|
- 核心框架
|
|||
|
|
|
|||
|
|
## 目的
|
|||
|
|
|
|||
|
|
提供类实例池、行为对象池、脚本对象池、实体池和 RT 展示对象的统一复用机制,减少 GC 和实例化开销。
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 核心路径
|
|||
|
|
|
|||
|
|
- 内存管理:`Assets/Game/GameHFScripte/CopyToHF/MemoryManager/`
|
|||
|
|
- 运行时对象:`Assets/Game/GameHFScripte/GameFunction/RTObjectManager/`
|
|||
|
|
- 调用池:`Assets/Game/GameHFScripte/GameFunction/Logic/CallPoolManager/`
|
|||
|
|
- 实体池:`Assets/Game/GameHFScripte/GameFunction/GameEntity/EntityPool/`
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 池类型
|
|||
|
|
|
|||
|
|
### HFClassEntityManager
|
|||
|
|
|
|||
|
|
类实例池,按类型 hash 维护 `Stack<object>`。
|
|||
|
|
|
|||
|
|
```text
|
|||
|
|
GetClassEntity<T>()
|
|||
|
|
-> 优先从 Stack 弹出复用
|
|||
|
|
-> 否则 Activator.CreateInstance<T>()
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### HFSimulateBehaviourPool<T>
|
|||
|
|
|
|||
|
|
行为对象池,用于继承 `AbsHFSimulateBehaviour` 的对象。
|
|||
|
|
|
|||
|
|
- 维护未使用队列和使用中列表。
|
|||
|
|
- `GiveBack()` 隐藏 GameObject 并调用 `Recycle()`。
|
|||
|
|
|
|||
|
|
### HFScriptMemPool<T>
|
|||
|
|
|
|||
|
|
脚本对象池,用于 `IHFRecycle` 的普通对象或 MonoBehaviour。
|
|||
|
|
|
|||
|
|
- 若类型继承 MonoBehaviour,自动创建 GameObject 挂载组件。
|
|||
|
|
- 若类型为普通类,直接 `Activator.CreateInstance`。
|
|||
|
|
|
|||
|
|
### 实体池
|
|||
|
|
|
|||
|
|
`HFEntityPool` + 各类 `AbsHFEntityPool` 实现,见 [实体系统](../实体系统/README.md)。
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 运行时展示对象(RT)
|
|||
|
|
|
|||
|
|
RT 对象用于在独立相机上展示模型、特效等:
|
|||
|
|
|
|||
|
|
- `RTObject`:运行时展示对象基类
|
|||
|
|
- `RTGameObjectRes`:从资源 id 创建展示对象
|
|||
|
|
- `RTEntity`:实体展示对象
|
|||
|
|
- `RTCameraItem`:RT 相机项
|
|||
|
|
- `RTUIEffectCameraItem`:UI 特效相机项
|
|||
|
|
|
|||
|
|
创建流程:
|
|||
|
|
|
|||
|
|
```text
|
|||
|
|
RTGameObjectRes
|
|||
|
|
-> 从资源 id 创建 GameObject
|
|||
|
|
-> 资源加载走 HFABMgr.Instance.LoadAsset
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 回收接口
|
|||
|
|
|
|||
|
|
| 接口 | 职责 |
|
|||
|
|
|---|---|
|
|||
|
|
| `IHFRecycle` | 基础回收接口,声明 `Recycle()` |
|
|||
|
|
| `IHFRecycleEventHandler` | 回收事件处理 |
|
|||
|
|
| `OnAfterRecycleHandler` | 回收到类实例池 |
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 使用规则
|
|||
|
|
|
|||
|
|
- 频繁创建/销毁的对象优先使用对象池。
|
|||
|
|
- 归还对象时确保来源正确,避免泄漏。
|
|||
|
|
- 对象池内对象在归还后应重置状态。
|
|||
|
|
- RT 对象注意及时释放相机 seat 和资源引用。
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 相关模块
|
|||
|
|
|
|||
|
|
- [实体系统](../实体系统/README.md)
|
|||
|
|
- [资源管理](../资源管理/README.md)
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 文档关联
|
|||
|
|
|
|||
|
|
> 当本文档发生变更时,应同步检查以下关联文档,避免信息不一致。
|
|||
|
|
|
|||
|
|
| 文档 | 关联原因 |
|
|||
|
|
|---|---|
|
|||
|
|
| [modules/模块索引.md](../../模块索引.md) | 模块索引是项目知识总入口 |
|
|||
|
|
| [modules/frameworks/实体系统/README.md](../实体系统/README.md) | 实体池是对象池的一种特化 |
|
|||
|
|
| [modules/frameworks/资源管理/README.md](../资源管理/README.md) | RT 对象资源加载依赖资源管理 |
|
|||
|
|
| [rules/10-架构说明.md](../../../rules/10-架构说明.md) | 架构说明定义总体分层和依赖方向 |
|
|||
|
|
| [rules/30-模块登记规则.md](../../../rules/30-模块登记规则.md) | 模块登记规则定义分类和状态口径 |
|