-
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.
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.
Open up IPoolable.cs and configure your PoolableTypes in the enum.
(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)
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.