/// <summary> /// 物体第一次引进对象池 /// </summary> /// <param name="o">对象</param> private void OneTimeInitObject(GameObject obj) { IPoolableObject o = obj.GetComponent <IPoolableObject>(); if (o == null) { Debug.LogError("未在内存池中找到此对象:" + obj.name); return; } //加入对象池 for (int i = 0; i < m_PoolList.Count; i++) { if (m_PoolList[i].PrefabName == o.PrefabName) { m_PoolList[i].PoolObjects.Add(o); break; } } //第一次出生初始化函数回调 o.OneTimeInitAction(); //物体隐藏 o.gameObject.SetActive(false); if (!m_IsUpdateHideTime) { m_HideObjs.Add(o); } else { m_ExpandHideObjs.Add(o); } }
/// <summary> /// 卸载释放一个对象(从对象池中) /// </summary> /// <param name="o">对象</param> public void ReleaseObject(IPoolableObject o) { //总池删除 for (int i = 0; i < m_PoolList.Count; i++) { if (m_PoolList[i].PrefabName == o.PrefabName) { //维护活动指针! //正好是当前活动指针的元素,则不需要维护。 //大于当前活动指针的元素,不维护 //当前活动指针大于--,维护 int oindex = m_PoolList[i].PoolObjects.IndexOf(o); if (oindex < m_PoolList[i].NextAvailableIndex) { --m_PoolList[i].NextAvailableIndex; } m_PoolList[i].PoolObjects.Remove(o); break; } } //隐藏池删除 if (m_HideObjs.Contains(o)) { if (m_IsUpdateHideTime) { m_HideObjs.Remove(o); } else { m_ReduceHideObjs.Add(o); } } Destroy(o.gameObject, .5f); }
/// <summary> /// 回收一个物体 /// </summary> /// <param name="o">物体</param> public void RecycleObject(IPoolableObject o) { //物体回收 for (int i = 0; i < m_PoolList.Count; i++) { if (m_PoolList[i].PrefabName == o.PrefabName) { if (m_PoolList[i].PoolObjects.Contains(o)) { m_PoolList[i].PoolObjects.Remove(o); } m_PoolList[i].PoolObjects.Add(o); //更新活动指针 if (m_PoolList[i].NextAvailableIndex >= m_PoolList[i].PoolObjects.Count) { m_PoolList[i].NextAvailableIndex = m_PoolList[i].PoolObjects.Count - 1; } break; } } //回收物体委托回调 o.RecycleAction(); //物体隐藏 o.gameObject.SetActive(false); if (!m_IsUpdateHideTime) { m_HideObjs.Add(o); } else { m_ExpandHideObjs.Add(o); } }