Pooling System

When you have a game with lots of projectiles, hit effects and various objects being continually created in the scene, it can significantly affect the performance of your game. That's why it's a great idea to re-use as many items as possible rather than create a new one each time.

The Pooling System enables easy re-use of objects in your game. To return an object after it is used, simply deactivate the game object.

The Pool Manager

To get started, add a Pool Manager component anywhere in your scene. This is a singleton, which means there should only be one of them in the scene at any time.

The Pool Manager makes it very easy to get a new object. Just pass the prefab game object reference to the Pool Manager's Get function. Here are some examples of how to get an object:

// Simply get an object
GameObject myNewObject = PoolManager.Instance.Get(myObjectPrefab);
// Get an object at a specified world position and rotation
GameObject myNewObject = PoolManager.Instance.Get(myObjectPrefab, Vector3.zero, Quaternion.identity);
// Get an object at a specified world position and rotation, and parent it to this transform
GameObject myNewObject = PoolManager.Instance.Get(myObjectPrefab, Vector3.zero, Quaternion.identity, transform);

It's also possible to get an object by name, if it exists in a Resources folder. Here's an example for a prefab named 'Projectile'.

// Simply get an object
GameObject myNewObject = PoolManager.Instance.Get("Projectile");
// Get an object at a specified world position and rotation
GameObject myNewObject = PoolManager.Instance.Get("Projectile", Vector3.zero, Quaternion.identity);
// Get an object at a specified world position and rotation, and parent it to this transform
GameObject myNewObject = PoolManager.Instance.Get("Projectile", Vector3.zero, Quaternion.identity, transform);

Object Pools

When you get an object from the Pool Manager, it creates an Object Pool, which is basically a group of a specific type of object. When this happens, it may affect performance because a certain number of objects need to be created for the pool.

To avoid this, you can create your own object pools with a specified number of starting units, which will be created before the scene starts. The pool manager will automatically find and use them.

To create an object pool:

  1. Add an Object Pool component to the scene.

  2. Drag a prefab into the Prefab property in the inspector.

  3. Set the number of default units in the inspector. This is how many will be created at the start.

Returning An Object To The Pool

When an objects game object is disabled, it is considered no longer used and is available for the next use. Make sure your object is deactivated as soon as possible after it's not needed.

The UVC kits include a Deactivate After Lifetime component, which can be used on projectiles, hit effects and other effects to disable them after a specified time and return them to the pool.

Important: If an object is being re-used, it may be necessary to reset some of its attributes each time it is used. If you need to do this, add an OnEnable function (which will be called by Unity when a gameobject is activated) to a script on the object, and do it there.