/// <summary> /// Gets a new object /// </summary> /// <returns>new object</returns> static GameObject GetNewObject(PooledObjectName name) { GameObject obj = null; switch (name) { case PooledObjectName.PlayerBullet: obj = GameObject.Instantiate(prefabPlayerBullet); obj.GetComponent <Bullet>().Initialize(); break; case PooledObjectName.EnemyBullet: obj = GameObject.Instantiate(prefabEnemyBullet); obj.GetComponent <Bullet>().Initialize(); break; case PooledObjectName.Enemy: obj = GameObject.Instantiate(prefabEnemy); obj.GetComponent <Enemy>().Initialize(); break; case PooledObjectName.Explosion: obj = GameObject.Instantiate(prefabExplosion); obj.GetComponent <Explosion>().Initialize(); break; } obj.SetActive(false); GameObject.DontDestroyOnLoad(obj); return(obj); }
public void GenerateSpecificPoolObject(PooledObjectName pooledObject, GameObject thisGameObject, int count) { switch (pooledObject) { case PooledObjectName.VisualTexts: CreateVisualTexts(count, thisGameObject); break; } }
/// <summary> /// Returns a pooled object to the pool /// </summary> /// <param name="name">name of pooled object</param> /// <param name="obj">object to return to pool</param> public static void ReturnPooledObject(PooledObjectName name, GameObject obj) { switch (name) { case PooledObjectName.PlayerBullet: case PooledObjectName.EnemyBullet: obj.GetComponent <Bullet>().StopMoving(); break; case PooledObjectName.Enemy: obj.GetComponent <Enemy>().Deactivate(); break; case PooledObjectName.Explosion: break; default: break; } obj.SetActive(false); pools[name].Add(obj); }
/// <summary> /// Gets a pooled object from the pool /// </summary> /// <returns>pooled object</returns> /// <param name="name">name of the pooled object to get</param> static GameObject GetPooledObject(PooledObjectName name) { List <GameObject> pool = pools[name]; // Check for available object in pool if (pool.Count > 0 && !pools[name][pools[name].Count - 1].activeInHierarchy) { //Return the last enemy in the list if any is available GameObject obj = pools[name][pools[name].Count - 1]; pools[name].RemoveAt(pools[name].Count - 1); return(obj); } else { // Pool empty, so expand pool and return new object (replace code below) // Expand the pool and return a new object if none available, the object will be // eadded to the pool when not in use anymroe pools[name].Capacity++; return(GetNewObject(name)); } }