Posted April 08, 2025 by Goutamraj
#Enemy Pool
Added object pooling for enemy game objects.
The EnemyPool
class is a singleton-based implementation of an object pool for managing reusable enemy objects in a game. It dynamically creates and manages object pools for different enemy types, optimizing resource usage and reducing overhead from frequent instantiation and destruction.
To manage and provide a pool of reusable enemy objects, ensuring efficient resource utilization.
public static EnemyPool instance;
Ensures that only one instance of the EnemyPool
exists in the game.
[SerializeField] private Transform pool; private Dictionary<EnemyType, ObjectPool<GameObject>> enemyPool = new Dictionary<EnemyType, ObjectPool<GameObject>>();
pool: A Transform
that serves as the parent container for inactive enemy objects.
enemyPool: A dictionary mapping EnemyType
to their respective object pools.
void Awake()
Ensures the singleton instance is initialized.
private void Awake() { instance = this; }
GameObject GetEnemyObj(EnemyType enemyType, GameObject enemyPrefabRef)
Retrieves an enemy object from the pool or creates a new pool if none exists for the specified type.
enemyType: The type of enemy to retrieve.
enemyPrefabRef: The prefab to use for instantiating a new enemy if required.
A GameObject
representing the requested enemy.
public GameObject GetEnemyObj(EnemyType enemyType, GameObject enemyPrefabRef)
If the pool for the specified type does not exist, a new pool is created.
Returns an enemy object from the relevant pool.
void ReturnEnemyObj(EnemyType enemyType, GameObject enemyPrefabRef)
Returns an enemy object to its pool.
enemyType: The type of enemy to return.
enemyPrefabRef: The enemy object being returned to the pool.
public void ReturnEnemyObj(EnemyType enemyType, GameObject enemyPrefabRef)
If the pool exists for the specified type, the object is released back into the pool.
If no pool exists, the object is destroyed as a fallback.
ObjectPool<GameObject> CreateNewPool(GameObject enemyPrefabRef)
Creates a new object pool for the specified enemy prefab.
enemyPrefabRef: The prefab for the enemy type to pool.
A new ObjectPool<GameObject>
instance.
public ObjectPool<GameObject> CreateNewPool(GameObject enemyPrefabRef)
Specifies the creation, release, and destruction behaviors for the pool.
void OnGet(GameObject obj)
Defines the behavior when an object is retrieved from the pool.
private void OnGet(GameObject obj) { obj.SetActive(true); }
Placeholder for activating the object.
void OnRelease(GameObject obj)
Defines the behavior when an object is returned to the pool.
private void OnRelease(GameObject obj) { obj.transform.SetParent(pool); obj.SetActive(false); }
Deactivates the object and assigns it to the designated pool container.
void OnDestroyPooledObject(GameObject obj)
Defines the behavior for destroying a pooled object.
private void OnDestroyPooledObject(GameObject obj) { Destroy(obj); }
Permanently destroys the object if it is no longer needed.
Dynamic Pool Creation: Automatically creates pools for new enemy types.
Reusable Objects: Reduces instantiation overhead by reusing enemy objects.
Resource Management: Ensures efficient memory and performance management.
Check out this for code implementation: EnemyPool