Skip to content

Documentation

Venomous edited this page Mar 11, 2018 · 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 to how to implement the IPoolable interface methods.


Step 3).

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

public class DroidEnemy : MonoBehaviour, IPoolable {

}

OR (preferred way)

public class DroidEnemy : PoolBehaviour {

}


Step 4).

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 interface can be passed

ObjectPool.Get(typeof(yourtype).Name); // Will return one of your type from the pool (null if none in the pool)

ObjectPool.Get<yourtype>(typeof(yourtype).Name); // This will return and cast your IPoolable to the type passed if one is found

ObjectPool.GetAmountInPool<yourtype>(); // 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