We had introduced about Singleton, which is a powerful game design pattern when developing games. In this tutorial, I would present another design pattern which would create a system that can reuse the existed gameobjects and without causing any waste of memory to our device by creating too much instant objects.
The concept of Object Pooling pattern is creating an object pool that would contain any object can be used. When a function is in need to use object, it would need to call one from that pool instead of instantiate one by itself.
By the end of using it, the pool would contain any recycled objects and make them ready for the next uses.
To do that, the pool would be a Singleton object to make the cycle easier to call.
In this tutorial, I would create an example with a Player to shoot bullet by hitting space key. That bullet would be provided by pool and once Player finished using it, it would return to pool.
First, we need to create that pool. Create an empty object with a script called PoolManager and make it as a Singleton patter.
Then create a serializable gameobject variable and a serializable list to store bullet later.
Now we would need to create a list to store bullets with limited amount. We will create a return type method to generate inactive bullet gameobjects. The amount of bullet generating would depend on the pass in integer of this method.
To generate a list with 10 bullets, we can call this method in Start() to replace the list variable we created.
Let’s just stop right here and make bullet prefab first.
Now we need to create a bullet to assign to PoolManager as bullet variable.
Create a capsule object and a script called Bullet, then drag this into Project window as a prefab.
Then assign it into PoolManager as bullet.
Now we can continue creating Player to shoot bullet.
Request for a bullet
Before creating Player, we need to create another method for Player to call.
In this method, the Player would request a bullet to use when hitting space key. In the setting of our example, the request would set bullet as active.
We will use foreach loop to look up the list for an unused bullet to set as active.
However, this might cause some error if the list is running out of bullets. We can instantiate some bullets immediately when all bullets in list are active.
Now the PoolManager is ready to go. Let’s create a Player to call it.
Request and recycling
Create a Player script and create a space detecting function under Update(). Then we can call RequestBullet() for a gameobjet to use.
When play and test it with space key, the bullets should appear in the center of screen.
Now we need to make the list recycle those bullets. It’s quite simple, just open the Bullet script and make it set to inactive after few seconds when got called.
Play it again and you would see the recycling of bullets.
Object Pooling pattern could be used to limit or optimize a bunch of objects to reusing without instantiate and destroy them rapidly.
Hope this tutorial would help you!