[docs][refactor]: 将旧 01~06 中文教程合并整理为 modules/frameworks 文件夹结构
- 删除 01~05 旧中文教程目录 - 将 03 核心功能教程拆分为各 framework 模块的子文档 - 06 工具脚本移入 modules/frameworks/编辑器工具/UI工具/ - 非框架内容合并进 rules/20-开发规则.md 与 rules/40-工作流清单.md - 更新所有交叉链接与文档关联表 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
110
modules/frameworks/内存对象管理/README.md
Normal file
110
modules/frameworks/内存对象管理/README.md
Normal file
@@ -0,0 +1,110 @@
|
||||
# 对象池与运行时对象管理
|
||||
|
||||
## 分类
|
||||
|
||||
- 核心框架
|
||||
|
||||
## 目的
|
||||
|
||||
提供类实例池、行为对象池、脚本对象池、实体池和 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) | 模块登记规则定义分类和状态口径 |
|
||||
116
modules/frameworks/内存对象管理/使用指南.md
Normal file
116
modules/frameworks/内存对象管理/使用指南.md
Normal file
@@ -0,0 +1,116 @@
|
||||
# 对象池使用指南
|
||||
|
||||
## 一、GameObject 对象池
|
||||
|
||||
### 1.1 基本用法
|
||||
|
||||
```csharp
|
||||
public class BulletManager : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private GameObject bulletPrefab;
|
||||
private HFObjectMemPool m_BulletPool;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
// 创建对象池
|
||||
m_BulletPool = new HFObjectMemPool(bulletPrefab, transform);
|
||||
}
|
||||
|
||||
public void Fire(Vector3 position, Quaternion rotation)
|
||||
{
|
||||
bool isNew;
|
||||
GameObject bullet = m_BulletPool.GetItem(out isNew);
|
||||
|
||||
bullet.transform.position = position;
|
||||
bullet.transform.rotation = rotation;
|
||||
|
||||
// 如果是新创建的对象,初始化组件
|
||||
if (isNew)
|
||||
{
|
||||
bullet.GetComponent<BulletComponent>().Initialize();
|
||||
}
|
||||
}
|
||||
|
||||
public void RecycleBullet(GameObject bullet)
|
||||
{
|
||||
m_BulletPool.GiveBack(bullet);
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
// 清理对象池
|
||||
m_BulletPool.Recycle();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 二、脚本对象池
|
||||
|
||||
### 2.1 基本用法
|
||||
|
||||
```csharp
|
||||
public class EventArgPool : MonoBehaviour
|
||||
{
|
||||
private HFScriptMemPool<GameEventArgs> m_ArgPool;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
m_ArgPool = new HFScriptMemPool<GameEventArgs>();
|
||||
}
|
||||
|
||||
public GameEventArgs GetEventArgs(GameEventId eventId)
|
||||
{
|
||||
bool isNew;
|
||||
GameEventArgs arg = m_ArgPool.GetItem(out isNew);
|
||||
arg.eventId = eventId;
|
||||
return arg;
|
||||
}
|
||||
|
||||
public void RecycleEventArgs(GameEventArgs arg)
|
||||
{
|
||||
m_ArgPool.GiveBack(arg);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 三、使用注意事项
|
||||
|
||||
### 3.1 对象池设计特点
|
||||
|
||||
| 特性 | 说明 |
|
||||
|-----|------|
|
||||
| **双池结构** | 空闲池 + 使用池,高效管理对象状态 |
|
||||
| **内存泄漏检测** | 归还时校验来源,及时发现问题 |
|
||||
| **泛型支持** | `HFScriptMemPool<T>` 提供类型安全 |
|
||||
| **Mono 支持** | 自动创建 GameObject,无缝集成 Unity |
|
||||
|
||||
### 3.2 性能优化建议
|
||||
|
||||
- 频繁创建销毁的对象必须使用对象池
|
||||
- 合理设置对象池初始容量
|
||||
- 定期清理空闲对象避免内存泄漏
|
||||
|
||||
---
|
||||
|
||||
**版本**: v1.0
|
||||
**生效日期**: 2026年
|
||||
**适用范围**: GiftGameX 项目业务开发人员
|
||||
|
||||
---
|
||||
|
||||
## 文档关联
|
||||
|
||||
> 当本文档发生变更时,应同步检查以下关联文档,避免信息不一致。
|
||||
|
||||
| 文档 | 关联原因 |
|
||||
|---|---|
|
||||
| [对象池与运行时对象管理入口](README.md) | 本文件是内存对象管理模块的子文档 |
|
||||
| [实体系统](../实体系统/README.md) | 实体池是对象池的一种特化 |
|
||||
| [资源管理](../资源管理/README.md) | RT 对象资源加载依赖资源管理 |
|
||||
| [modules/模块索引.md](../../模块索引.md) | 模块索引是项目知识总入口 |
|
||||
| [rules/10-架构说明.md](../../../rules/10-架构说明.md) | 架构说明定义总体分层和依赖方向 |
|
||||
| [rules/30-模块登记规则.md](../../../rules/30-模块登记规则.md) | 模块登记规则定义分类和状态口径 |
|
||||
Reference in New Issue
Block a user