Object Pool Pattern — Design Patterns for Unity #3

Make your code more efficient by useful patterns

OnurKiris
4 min readJun 16, 2022
Photo by Raimond Klavins on Unsplash

Object Pool Pattern

Lets say we have a shooting game or space arcade game. When we actually shooting, we are creating bullets from gun position and destroying them when they collide with something or out of range. While numbers and processes of these bullets increases in seconds, CPU and memory will have hard time to deal with it.

Picture by Mark Placzek from raywenderlich.com

What we can do to prevent this performance issue is using Object Pooling. Basically it means, instantiate bullets and when they collide: deactivate bullet game objects and send them back to the pool for re-use.

Picture by Mike Geig

There are few ways to implement this pattern but I will cover the new one which is new feature that has come to Unity recently — Object Pool<T0>

In my example, I will explain it with 2 scripts; Asteroid (as Bullet) and Asteroid Spawner (as Gun) so lets take a look below steps;

  • Create a script (Asteroid) and attach it to Asteroid game object.
  • Open the script and write the following within the class definition then assign a number for asteroid speed from inspector:
  • Create a script (AsteroidSpawner) and attach it to Asteroid Spawner game object.
  • Open the script and write the following within the class definition then assign asteroid prefab from inspector:

So when the game has started, Asteroid Spawner will create an empty pool of Asteroid prefab at the very beginning. Every time we press space bar, it will start to fill that pool by instantiating Asteroids. When instantiated asteroid move away by its own mechanic and finally became invisible, it will release (deactivate) itself and then turn to the pool for re-use, over and over again.

To be more specific, lets go deeper about what these extends of Object Pool means.

Function and actions to control the object pool

As you can see from above picture: createFunc, actionOnGet, actionOnRelease, actionOnDestroy etc. These functions and actions are defines what to do when related events happens so you can fill inside as you need.

  • First one is createFunc which is ‘CreateAsteroid’ method in our example. Its instantiating asteroid and adding it to the pool by ‘SetPool’ public method in the Asteroid script while there are no available asteroids in the pool. If there are, it will just activate them for re-use instead of instantiate new one.
  • actionOnGet which is ‘ActionOnGet’ method in our example. We call this method by pressing space bar — asteroidPool.Get(); it gets the first available asteroid from the pool, resets the position of asteroid to the Asteroid Spawner’s position and activate the asteroid for re-use.
  • actionOnRelease which is ‘ActionOnRelease’ method in our example. This is called from ‘OnBecameInvisible’ method in Asteroid script. It deactivates the asteroid game object and return it to the pool.
  • actionOnDestroy which is ‘ActionOnDestroy’ method in our example. When the number of the instantiated objects exceeds max size of the pool, Unity will keep instantiating asteroids as we need but when things comes to return to the pool, it will destroy excess ones instead of keeping.
  • collectionCheck which we didn’t use on this one. They are performed when an instance is returned back to the pool. An exception will be thrown if the instance is already in the pool. Collection checks are only performed in the Editor.
  • defaultCapacity defines capacity the stack will be created with.
  • maxSize is the max size of the object pool. As I mentioned above, when the number of the instantiated objects exceeds max size of the pool, it will keep instantiating asteroids as we need but when things comes to return to the pool, it will destroy excess ones instead of keeping.

You can ignore maxSize, then Unity will keep instantiating asteroids as we need and adding them to the pool unless there are no available asteroids in the pool and also you won’t have to implement actionOnDestroy which is won’t needed anymore. You can also ignore collectionCheck and defaultCapacity but createFunc, actionOnGet and actionOnRelease are must as they forming main functionality of Object Pool system.

Thanks for reading!

Feel free to reach me out on LinkedIn if you have questions.

See you on next part — Model-View-Presenter Pattern (MVP)

--

--