Another Unity Object Pool Update

It’s time for another update to my Unity Object Pool library. I previously wrote about it here and here, and this update streamlines the code a bit and makes it even easier to use. I am also going to open source this library (more on that later).

I’ve been using this version for some time and have had no issues, so it’s time to make it available. Feel free to grab a copy to use in your projects. If you like this library, and you want to see some features added, please let me know, or consider sharing your updates.

What Was Updated

  • This update no longer requires a Unity GameObject for every pool. All pools are stored in it’s own Queue and the Queues are placed in a Dictionary. When the library is started, it will create nested game objects under the main pool object and will then populate each pool with the correct objects. This makes it easier to see what’s going on in the Inspector.
  • When items are placed into the pool, the object name is appended with “(Inactive)”. When objects are taken from the pool, that is removed. Again, this makes is easier to see what objects are being used at any given time within the Inspector. Inactive objects are in the pool waiting to be used.
  • The library has been created as a Singleton so that it can be used throughout your project via the static Instance variable.

How To Use

  1. Download and import this asset into your Unity Project.
  2. Drag the GBC Object Pooler prefab into your Unity scene.
  3. Click on the prefab in your scene, and in the Inspector you will notice a script named GBCObjectPooler.
  4. Enter the number of pools you wish to create.
  5. For each pool, drag in a prefab, enter the number of items you wish to create in the pool, and the name of the pooled items.

As of this post, there are 3 methods.

GetPooledObject(string poolName)

This will return an item from the pool specified.
If the pool does not exist, or if there are no more items in the pool, null is returned.

ReturnToPool(GameObject object, string PoolName)

This will place the specified item back into the correct pool, and will disable it.

ItemsInPool(string poolName)

This will return the number of items still available in the specified pool.
If the pool does not exist, null is returned.

Open Sourced

I am placing the source code (with example) on github for all to use and update. This is a simple “quick and dirty” library and I would like to keep it that way, but I am sure there are a lot of other useful features that can be added to enhance functionality. I don’t want to add a lot of unnecessary bloat… there are some very good pooling libraries on the Asset Store that contain a ton of features, but my intent is to provide an easy to use pooling library, but let’s see where this goes.

It’s a small project, but it is my first open source attempt, so this is a learning experience for me as well.

Link to GitHub project

[divider]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: