-
Notifications
You must be signed in to change notification settings - Fork 3
Documentation
How to setup UnityObjectPooler in your Unity project?
Add IEnemy.cs, and ObjectPool.cs to your project.
This step is completely optional but it might be useful to create a base class for your enemy, like I did with PoolBehaviour script which implements MonoBehaviour and IEnemy interface. Look at this script for reference.
Let your Enemy monobehaviour class implement IEnemy interface OR your base class:
public class DroidEnemy : MonoBehaviour, IEnemy
{
}
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 IEnemy and MonoBehaviour. It has all requirements implemented from the interface.
Open up IEnemy.cs and configure your EnemyTypes in the enum.
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(enemy); // Takes IEnemy type so any class that inherits IEnemy class can be passed ObjectPool.Get(EnemyType.YourCustomType); // EnemyType will be your custom enemy types ObjectPool.Get(EnemyType.YourCustomType); // This will cast your IEnemy to the concrete type passed
For some examples on using the ObjectPool class look at the ObjectPoolTests.cs There I have a scenario for all calls.