docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class ObjectPool<T>

    Provides a generic object pool implementation.

    Inheritance
    object
    ObjectPool<T>
    Inherited Members
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: Unity.XR.CoreUtils
    Assembly: Unity.XR.CoreUtils.dll
    Syntax
    public class ObjectPool<T> where T : class, new()
    Type Parameters
    Name Description
    T

    The Type of objects in this pool.

    Fields

    PooledQueue

    All objects currently in this pool.

    Declaration
    protected readonly Queue<T> PooledQueue
    Field Value
    Type Description
    Queue<T>

    Methods

    ClearInstance(T)

    Implement this function in a derived class to automatically clear an instance when Recycle(T) is called.

    Declaration
    protected virtual void ClearInstance(T instance)
    Parameters
    Type Name Description
    T instance

    The object to clear.

    Get()

    Gets an object instance from the pool. Creates a new instance if the pool is empty.

    Declaration
    public virtual T Get()
    Returns
    Type Description
    T

    The object instance

    Recycle(T)

    Returns an object instance to the pool.

    Declaration
    public void Recycle(T instance)
    Parameters
    Type Name Description
    T instance

    The instance to return.

    Remarks

    Object values can be cleared automatically if ClearInstance(T) is implemented. The base ObjectPool class does not clear objects to the pool.

    In This Article
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)