public void Init(SubPool subPool, PooledObjectInfo objInf) { m_SubPool = subPool; m_ObjInf = objInf; if (!IsInited) { return; } m_ObjInf.OnSpawnEvent += OnSpawnMethod; m_ObjInf.OnDespawnEvent += OnDespawnMethod; }
public GameObject Spawn(Vector3 position, Quaternion rotation, Transform parent) { if (!IsInited) { return(null); } if (m_InactiveObjs.Count < 1) { switch (m_PoolBehaviour) { case PoolBehaviour.CreateNewThings: { CreateObject(); } break; case PoolBehaviour.ReuseActiveThings: { if (m_ActiveObjs.Count < 1) { return(null); } if (!Despawn(m_ActiveObjs[0].Obj)) { return(null); } } break; default: return(null); } } PooledObjectInfo objInfForSpawn = m_InactiveObjs[m_InactiveObjs.Count - 1]; m_InactiveObjs.RemoveAt(m_InactiveObjs.Count - 1); objInfForSpawn.SetParent(parent); objInfForSpawn.SetPosition(position); objInfForSpawn.SetRotation(rotation); objInfForSpawn.SetActive(true); objInfForSpawn.OnSpawn(); m_ActiveObjs.Add(objInfForSpawn); return(objInfForSpawn.Obj); }
public bool Despawn(GameObject obj) { if (!IsInited) { return(false); } if (obj == null) { return(false); } int count = m_ActiveObjs.Count; if (count < 1) { return(false); } PooledObjectInfo objInfForDespawn = null; for (int i = 0; i < count; ++i) { if (m_ActiveObjs[i].Obj == obj) { objInfForDespawn = m_ActiveObjs[i]; m_ActiveObjs.RemoveAt(i); break; } } if (objInfForDespawn == null) { return(false); } objInfForDespawn.OnDespawn(); objInfForDespawn.SetActive(false); objInfForDespawn.SetParent(m_Container); m_InactiveObjs.Add(objInfForDespawn); return(true); }