Skip to content

Documentation

Venomous edited this page Dec 10, 2017 · 26 revisions

How to setup UnityObjectPooler in your Unity project?

Step 1).

Add IEnemy.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 enemy, like I did with PoolBehaviour script which implements MonoBehaviour and IEnemy interface. Look at this script for reference.


Step 3).

Let your Enemy monobehaviour class implement IEnemy interface OR your base class:

public class DroidEnemy : MonoBehaviour, IEnemy {

}

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 IEnemy and MonoBehaviour. It has all requirements implemented from the interface.


Step 4).

Open up IEnemy.cs and configure your EnemyTypes in the enum.


Step 5).

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

public override EnemyType GetEnemyType() { return EnemyType.YourCustomType; }

(otherwise implement the GetEnemyType 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(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<ConcreteType>(EnemyType.YourCustomType); // This will cast your IEnemy to the concrete type passed

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

ObjectPool.GetAmountInPool(EnemyType.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