-
Notifications
You must be signed in to change notification settings - Fork 3
Documentation
How to setup UnityObjectPooler in your Unity project?
Add IPoolable.cs, and ObjectPool.cs to your project.
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.
Let your Poolable monobehaviour class implement IPoolable interface OR your base class:
public class DroidEnemy : MonoBehaviour, IPoolable
{
}
OR
public class DroidEnemy : PoolBehaviour
{
}
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.
Let all poolable objects implement the base class (or interface)
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.