Version: Unity 6.3 LTS (6000.3)
LanguageEnglish
  • C#

ObjectPool<T0>

class in UnityEngine.Pool

/

Implemented in:UnityEngine.CoreModule


Implements interfaces:IObjectPool<T0>

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

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 UnityEngine;
using UnityEngine.Pool;

public class SimplePoolExample : MonoBehaviour { // The pool holds plain GameObjects (you can swap this for any component type). private ObjectPool<GameObject> pool;

void Awake() { // Create a pool with the four core callbacks. pool = new ObjectPool<GameObject>( createFunc: CreateItem, actionOnGet: OnGet, actionOnRelease: OnRelease, actionOnDestroy: OnDestroyItem, collectionCheck: true, // helps catch double-release mistakes defaultCapacity: 10, maxSize: 50 ); }

void Update() { // Press Space to spawn one pooled object for 1 second. if (Input.GetKeyDown(KeyCode.Space)) { GameObject gameObject = pool.Get(); gameObject.transform.position = Random.insideUnitSphere * 5f;

// Return it to the pool after a short delay. StartCoroutine(ReturnAfter(gameObject, 1f)); } }

// Creates a new pooled GameObject the first time (and whenever the pool needs more). private GameObject CreateItem() { GameObject gameObject = GameObject.CreatePrimitive(PrimitiveType.Cube); gameObject.name = "PooledCube"; gameObject.SetActive(false); return gameObject; }

// Called when an item is taken from the pool. private void OnGet(GameObject gameObject) { gameObject.SetActive(true); }

// Called when an item is returned to the pool. private void OnRelease(GameObject gameObject) { gameObject.SetActive(false); }

// Called when the pool decides to destroy an item (e.g., above max size). private void OnDestroyItem(GameObject gameObject) { Destroy(gameObject); }

private System.Collections.IEnumerator ReturnAfter(GameObject gameObject, float seconds) { yield return new WaitForSeconds(seconds); // Give it back to the pool. pool.Release(gameObject); } }

Properties

Property Description
CountActiveNumber of objects that have been created by the pool but are currently in use and have not yet been returned.
CountAllThe total number of active and inactive objects.
CountInactiveNumber of objects that are currently available in the pool.

Constructors

Constructor Description
ObjectPool_1Creates a new ObjectPool instance.

Public Methods

Method Description
ClearRemoves all pooled items and invokes the pool's action on destroy callback for each item in the pool.
DisposeRemoves all pooled items and invokes the pool's action on destroy callback for each item in the pool.
GetGet an instance from the pool. If the pool is empty then a new instance is created.
ReleaseReturns the instance back to the pool. If the pool has reached maximum size, the returned instance is destroyed.