Skip to content

Object Pool

Oxters Wyzgowski edited this page Jun 11, 2019 · 1 revision

About Object Pool

The Object Pool is a class that lets you create and store many objects for reuse.

Create Pool

ObjectPool<YourCustomType> pool;
pool = new ObjectPool<YourCustomType>(prefab);

You need to replace YourCustomType here with your own class that inherits Component (so Transform and anything that inherits MonoBehaviour works too). There are more parameters in the constructor that change the behavior of the pool.

Get Object

YourCustomType instance = pool.Get();

Get has optional parameters that allow you to place the object in the proper position, orientation, and scale.

Return Object

pool.Return(instance);

If you have reuseObjectsInUse set to true, then you don't need to return old objects. The pool will reuse the first object after getting past the pool size.

Clone this wiki locally