As a game developer, you might have experienced performance issues when instantiating new game objects in a game. This is a very usual thing as instantiating an object in the middle of the game costs a severe amount of CPU power. Professional game developers use various methods to avoid instantiating objects like this. Pooling is the most popular method to do this. We gonna target unity engine in this tutorial and this class is created in C# language.
A pool is a set of instantiated game objects but disabled. When a class or method requires a game object of that type pool simply takes one of the game objects from the pool and returns it. We can create pools within each class and that is going to be very hard to manage and time wasting as you have to rewrite the same code again and again. To avoid this we are going to create a custom class called Pool and implement all the functions a pool should contain within it.
As we mentioned above the class is named as pool and we need the constructor to create the pool when an instance of this class is created.
private Queue<GameObject> poolingObjectsQueue = new Queue<GameObject>();
public Pool(GameObject poolingObject, int requiredAmount) { for (int i = 0; i < requiredAmount; i++) { GameObject spawnedObject = Object.Instantiate(poolingObject); spawnedObject.SetActive(false); poolingObjectsQueue.Enqueue(spawnedObject); } _poolingObject = poolingObject; }
The class contains a Queue variable which contains game Objects. When this constructor is called what it does is it loops for the number of times passed into the constructor and Instantiate new GameObjects of the given type and Enqueue them in the Queue. And all the game objects are set deactivated. We create a variable of the type of GameObject named as _poolingObject in the class and assign the given game object to the constructor to that variable for future use.
Now our pool is ready. What we need now are some methods to get GameObjects from the pool and return the game objects to the pool.
The first method we need is a function to get a game object from the pool. This is a simple method. What we do is Dequeue a game object from Queue and return it. We add an extra parameter to the function to activate the game object when returning and this parameter is set to false in default. If the Queue is empty this function throws an Exception.
public GameObject GetObject(bool activateOnReturn = false) { if (poolingObjectsQueue.Count > 0) { GameObject go = poolingObjectsQueue.Dequeue(); if (activateOnReturn) { go.SetActive(true); } return go; } throw new Exception("Queue is empty"); }
Now what we need is a function to add a used game object again to the pool. This method has a parameter of type GameObject and what this does is Enqueue the game object to the Queue again. If the GameObject is active this method deactivates it before the process.
public void ReturnGameObject(GameObject gameObject) { if (gameObject.activeSelf) { gameObject.SetActive(false); } poolingObjectsQueue.Enqueue(gameObject); }
Now our pool is fully functional. We can create a pool and use it. But to make this class more useful we can add some more methods.
We gonna create 4 additional methods and those are
- A method to add a game object to the pool after a delay
- A method to get the number of game objects remaining in the pool
- A method to increase the pool size
- A method to clear the pool
1. Delaying the ReturnGameObject method is easy and what we gonna use here is the Task class, and copy the same code of that method. We name this method as "ReturnGameObjectAfterDelay"
and this method takes two parameters. First one is the game object and the second one is the delay we want in milliseconds. We use a lambda expression in this method to the callback function.
public void ReturnGameObjectAfterDelay(GameObject gameObject , int delayInMiliSeconds) { Task.Delay(delayInMiliSeconds).ContinueWith(t => { if (gameObject.activeSelf) { gameObject.SetActive(false); } poolingObjectsQueue.Enqueue(gameObject); }); }
2. This method simply returns the number of remaining game objects in the pool. It's very simple and returns the Queue.Count as the value.
public int GetRemainingObjectsAmount() { int amount = poolingObjectsQueue.Count; return amount; }
3. This method is used to increase the pool size at any moment we want. We gonna use the _poolingObject variable to instantiate more objects of that type. This parameter takes one parameter of type int and increases the pool by instantiating new game objects and enqueue them in the Queue.
public void IncreasePool(int amount) { for (int i = 0; i < amount; i++) { GameObject spawnedObject = Object.Instantiate(_poolingObject); spawnedObject.SetActive(false); poolingObjectsQueue.Enqueue(spawnedObject); } }
4. This is very simple and what it does is call the Clear method of the Queue.
public void ClearPool() { poolingObjectsQueue.Clear(); }
Final Code should look like this. Leave your ideas and opinions in the comment section.
You can clone the script from GitHub.
You can clone the script from GitHub.
public class Pool { private Queue<GameObject> poolingObjectsQueue = new Queue<GameObject>(); private GameObject _poolingObject; public Pool(GameObject poolingObject, int requiredAmount) { for (int i = 0; i < requiredAmount; i++) { GameObject spawnedObject = Object.Instantiate(poolingObject); _poolingObject = poolingObject; spawnedObject.SetActive(false); poolingObjectsQueue.Enqueue(spawnedObject); } } public GameObject GetObject(bool activateOnReturn = false) { if (poolingObjectsQueue.Count > 0) { GameObject go = poolingObjectsQueue.Dequeue(); if (activateOnReturn) { go.SetActive(true); } return go; } throw new Exception("Queue is empty"); } public void ReturnGameObject(GameObject gameObject) { if (gameObject.activeSelf) { gameObject.SetActive(false); } poolingObjectsQueue.Enqueue(gameObject); } public void ReturnGameObjectAfterDelay(GameObject gameObject , int delayInMiliSeconds) { Task.Delay(delayInMiliSeconds).ContinueWith(t => { if (gameObject.activeSelf) { gameObject.SetActive(false); } poolingObjectsQueue.Enqueue(gameObject); }); } public int GetRemainingObjectsAmount() { int amount = poolingObjectsQueue.Count; return amount; } public void IncreasePool(int amount) { for (int i = 0; i < amount; i++) { GameObject spawnedObject = Object.Instantiate(_poolingObject); spawnedObject.SetActive(false); poolingObjectsQueue.Enqueue(spawnedObject); } } public void ClearPool() { poolingObjectsQueue.Clear(); } } }
Related Articles
Comments
Post a Comment