Enemy Pool
Added object pooling for enemy game objects.
Overview
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.
1. EnemyPool Class
Purpose
To manage and provide a pool of reusable enemy objects, ensuring efficient resource utilization.
Singleton Pattern
public static EnemyPool instance;
Ensures that only one instance of the EnemyPool
exists in the game.
Fields
[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.
2. Methods
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.
Parameters:
-
enemyType: The type of enemy to retrieve.
-
enemyPrefabRef: The prefab to use for instantiating a new enemy if required.
Returns:
-
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.
Parameters:
-
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.
Parameters:
-
enemyPrefabRef: The prefab for the enemy type to pool.
Returns:
-
A new
ObjectPool<GameObject>
instance.
public ObjectPool<GameObject> CreateNewPool(GameObject enemyPrefabRef)
-
Specifies the creation, release, and destruction behaviors for the pool.
Pool Behaviors
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.
Summary
-
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
Files
Maze Escape
Navigate the maze, grab the diamond, survive the hunt!
Status | Prototype |
Author | Goutamraj |
Genre | Survival |
Tags | Singleplayer, Top down shooter |
More posts
- Add Precision Aim for Player and Update Guard Enemy with Laser Mechanics4 days ago
- Guard Enemy Implementation10 days ago
- Guard Enemy14 days ago
- Enemy Handling System18 days ago
- CellsPool (ObjectPool for Maze Cells)20 days ago
- Game Loop21 days ago
- Diamond Pickup22 days ago
- Entry and Exit path22 days ago
- Player State Machine25 days ago
Leave a comment
Log in with itch.io to leave a comment.