class in UnityEngine.Pool
/
Implemented in:UnityEngine.CoreModule
Implements interfaces:IObjectPool<T0>
A stack-based pool of object instances of type T, which implements the IObjectPool interface.
Use this class to create a pool of object instances that can be reused to reduce the overhead of instantiating and destroying objects frequently.
The constructor takes delegates as arguments to allow you to define the behavior of the pool when an item is created, taken from the pool, returned to the pool, or destroyed. You can also specify the default capacity of the pool, the maximum size the pool can grow to, and whether to perform collection checks when returning an item to the pool to catch double-release errors.
Use ObjectPool<T0>.Get to get an instance from the pool. If the pool is empty then a new instance is created using the method passed as the createFunc parameter to the constructor. Use ObjectPool<T0>.Release to return an instance back to the pool. If the pool has reached its maximum size then the instance is destroyed using the method passed as the actionOnDestroy parameter to the constructor.
Important: ObjectPool<T> stores its pooled object instances in a stack-like collection, so don't assume physical contiguity of the objects in memory. ObjectPool<T> is not safe to call from background threads.
For more information on the concept and application of object pooling, refer to Pooling and reusing objects in the manual.
Additional resources: IObjectPool<T0>
using System.Text; using UnityEngine; using UnityEngine.Pool;
// This component returns the particle system to the pool when the OnParticleSystemStopped event is received. [RequireComponent(typeof(ParticleSystem))] public class ReturnToPool : MonoBehaviour { public ParticleSystem system; public IObjectPool<ParticleSystem> pool;
void Start() { system = GetComponent<ParticleSystem>(); var main = system.main; main.stopAction = ParticleSystemStopAction.Callback; }
void OnParticleSystemStopped() { // Return to the pool pool.Release(system); } }
// This example spans a random number of ParticleSystems using a pool so that old systems can be reused. public class PoolExample : MonoBehaviour { public enum PoolType { Stack, LinkedList }
public PoolType poolType;
// Collection checks will throw errors if we try to release an item that is already in the pool. public bool collectionChecks = true; public int maxPoolSize = 10;
IObjectPool<ParticleSystem> m_Pool;
public IObjectPool<ParticleSystem> Pool { get { if (m_Pool == null) { if (poolType == PoolType.Stack) m_Pool = new ObjectPool<ParticleSystem>(CreatePooledItem, OnTakeFromPool, OnReturnedToPool, OnDestroyPoolObject, collectionChecks, 10, maxPoolSize); else m_Pool = new LinkedPool<ParticleSystem>(CreatePooledItem, OnTakeFromPool, OnReturnedToPool, OnDestroyPoolObject, collectionChecks, maxPoolSize); } return m_Pool; } }
ParticleSystem CreatePooledItem() { var go = new GameObject("Pooled Particle System"); var ps = go.AddComponent<ParticleSystem>(); ps.Stop(true, ParticleSystemStopBehavior.StopEmittingAndClear);
var main = ps.main; main.duration = 1; main.startLifetime = 1; main.loop = false;
// This is used to return ParticleSystems to the pool when they have stopped. var returnToPool = go.AddComponent<ReturnToPool>(); returnToPool.pool = Pool;
return ps; }
// Called when an item is returned to the pool using Release void OnReturnedToPool(ParticleSystem system) { system.gameObject.SetActive(false); }
// Called when an item is taken from the pool using Get void OnTakeFromPool(ParticleSystem system) { system.gameObject.SetActive(true); }
// If the pool capacity is reached then any items returned will be destroyed. // We can control what the destroy behavior does, here we destroy the GameObject. void OnDestroyPoolObject(ParticleSystem system) { Destroy(system.gameObject); }
void OnGUI() { GUILayout.Label("Pool size: " + Pool.CountInactive); if (GUILayout.Button("Create Particles")) { var amount = Random.Range(1, 10); for (int i = 0; i < amount; ++i) { var ps = Pool.Get(); ps.transform.position = Random.insideUnitSphere * 10; ps.Play(); } } } }
| Property | Description |
|---|---|
| CountActive | Number of objects that have been created by the pool but are currently in use and have not yet been returned. |
| CountAll | The total number of active and inactive objects. |
| CountInactive | Number of objects that are currently available in the pool. |
| Constructor | Description |
|---|---|
| ObjectPool_1 | Creates a new ObjectPool instance. |
| Method | Description |
|---|---|
| Clear | Removes all pooled items and invokes the pool's action on destroy callback for each item in the pool. |
| Dispose | Removes all pooled items and invokes the pool's action on destroy callback for each item in the pool. |
| Get | Get an instance from the pool. If the pool is empty then a new instance is created. |
| Release | Returns the instance back to the pool. If the pool has reached maximum size, the returned instance is destroyed. |