using System.Collections; using System.Collections.Generic; using UnityEngine; namespace TFramework { public class ObjectPoolMananger : BaseManager { public Transform m_poolTransform; private Dictionary _poolDic = new Dictionary(); /// /// 注册对象池 /// /// 对象池名字 /// 对象模板 /// 池子最大量 /// 取出事件 /// 回收事件 public void RegisterPool(string poolName, GameObject objTemplate, int maxCount, TAction takeOutEvent = null, TAction recycleEvent = null) { if (_poolDic.ContainsKey(poolName)) Log.Error($"对象池【{poolName}】已注册,请勿重复注册!"); else { new GameObject(poolName).transform.parent = m_poolTransform; _poolDic.Add(poolName, new ObjectPool(objTemplate, maxCount, takeOutEvent, recycleEvent)); } } /// /// 取出对象 /// /// /// public GameObject TakeOutObj(string poolName) { if (_poolDic.ContainsKey(poolName)) return _poolDic[poolName].TakeOutObj(); else { Log.Error($"不存在名字为【{poolName}】的对象池!"); return null; } } /// /// 回收对象 /// /// 对象池名字 /// 对象 public void RecycleObj(string poolName, GameObject obj) { if (_poolDic.ContainsKey(poolName)) { obj.transform.SetParent(m_poolTransform.Find(poolName),false); _poolDic[poolName].RecycleObj(obj); } else Log.Error($"不存在名字为【{poolName}】的对象池!"); } /// /// 批量回收对象 /// /// /// public void RecycleObjs(string poolName, IEnumerable objs) { foreach (var item in objs) { RecycleObj(poolName, item); } } /// /// 检查是否存在对象池 /// /// /// public bool IsHaveObjPool(string poolName) => _poolDic.ContainsKey(poolName); /// /// 清空指定对象池 /// public void ClearObjpool(string poolName) { if (IsHaveObjPool(poolName)) _poolDic[poolName].Clear(); } /// /// 清空所有对象池 /// public void ClearAll() { foreach (var item in _poolDic) item.Value.Clear(); } } }