【重构】整理文档目录结构,创建5个分类文件夹,拆分业务开发指南为8个独立文档,添加README入口导航
This commit is contained in:
101
03_核心功能/对象池使用指南.md
Normal file
101
03_核心功能/对象池使用指南.md
Normal file
@@ -0,0 +1,101 @@
|
||||
# StrayFog 框架对象池使用指南
|
||||
|
||||
## 一、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项目业务开发人员
|
||||
Reference in New Issue
Block a user