void CreatPool() { for (int i = 0; i < defaultPoolSize; i++) { RecycleObject obj = GameObject.Instantiate(prefab); obj.gameObject.SetActive(false); pool.Add(obj); } }
void OnMissileDestroyed(RecycleObject missile) { var obj = explosionFactory.Get(); obj.Activate(missile.transform.position); obj.Destroyed += OnExplosionDestryed; missileFactory.Restore(missile); missile.Destroyed -= OnMissileDestroyed; }
public RecycleObject Get() { if (pool.Count == 0) { CreatPool(); } int lastIndex = pool.Count - 1; RecycleObject obj = pool[lastIndex]; pool.RemoveAt(lastIndex); obj.gameObject.SetActive(true); return(obj); }
void OnExplosionDestryed(RecycleObject explosion) { explosionFactory.Restore(explosion); explosion.Destroyed -= OnExplosionDestryed; }
public void Restore(RecycleObject obj) { obj.gameObject.SetActive(false); pool.Add(obj); }
public Factory(RecycleObject prefab, int defaultPoolSize = 5) { this.prefab = prefab; this.defaultPoolSize = defaultPoolSize; }