Skip to content

Documentation

Venomous edited this page Dec 18, 2017 · 26 revisions

How to setup UnityObjectPooler in your Unity project?

Step 1).

Add IPoolable.cs, and ObjectPool.cs to your project.


Step 2).

This step is completely optional but it might be useful to create a base class for your Poolable, like I did with PoolBehaviour script which implements MonoBehaviour and IPoolable interface. Look at this script for reference.


Step 3).

Let your Poolable monobehaviour class implement IPoolable interface OR your base class:

public class DroidEnemy : MonoBehaviour, IPoolable {

}

OR

public class DroidEnemy : PoolBehaviour {

}


Step 3).

If you went with the interface solely you must implement all required methods from the interface. For references look at the PoolBehaviour script. It is a base class that implements IPoolable and MonoBehaviour. It has all requirements implemented from the interface.


Step 4).

Open up IPoolable.cs and configure your PoolableTypes in the enum.


Step 5).

(If you went with the base class way) Override GetPoolableType method on your poolable class like so:

public override PoolableType GetPoolableType() { return PoolableType.YourCustomType; }

(otherwise implement the GetPoolableType method from the interface and return your type)


Step 6).

Use the ObjectPool's static methods

ObjectPool can be accessed at all times.

ObjectPool.Clear(); // Clears the pool (should be used on scene reload or new scene load because its static)

ObjectPool.Add(poolable); // Takes IPoolable type so any class that inherits IPoolable class can be passed

ObjectPool.Get(PoolableType.YourCustomType); // PoolableType will be your custom enemy types

ObjectPool.Get<ConcreteType>(PoolableType.YourCustomType); // This will cast your IPoolable to the concrete type passed

ObjectPool.GetUniqueId(); // Get's a unique id

ObjectPool.GetAmountInPool(PoolableType.YourCustomType); // Retrieves an int based on how many of this type are in the pool

For some examples on using the ObjectPool class look at the ObjectPoolTests.cs There I have a scenario for all calls.

Clone this wiki locally