Skip to content

ScriptablePool<T> Class

A UnityEngine.ScriptableObject that holds a pool of values.

C#
public abstract class ScriptablePool<T> : ScriptablePool, INotifyPropertyChanging, INotifyPropertyChanged, IStackTraceProvider, IPoolCallbacks<T> where T : class

T
The type of to pool. Must be a class.

Inheritance object → UnityEngine.Object → UnityEngine.ScriptableObject → RuntimeScriptableObjectScriptablePool

Implements INotifyPropertyChanging, INotifyPropertyChanged, IStackTraceProvider, IPoolCallbacks<T>

How many total objects that the pool is keeping track of.

C#
public override sealed int CountAll { get; protected set; }

How many objects that are currently active.

C#
public override sealed int CountActive { get; protected set; }

How many objects that are currently inactive.

C#
public override sealed int CountInactive { get; protected set; }

Gets an object from the pool.

C#
public T Get()

T
The newly acquired object from the pool.

Returns an object to the pool.

C#
public void Release(T item)

item T
The item to return.

Clears the pool. All objects, both active and inactive, will be destroyed.

C#
public void Clear()

Called when a new object needs to be created.

C#
protected abstract T CreateObject()

T

Called when an object needs to be destroyed.

C#
protected abstract void DestroyObject(T item)

item T
The object to be destroyed.

Called when an object is retrieved from the pool.

C#
protected virtual void OnGet(T item)

item T
The object that was retrieved from the pool.

Called when an object is returned to the pool.

C#
protected virtual void OnReturn(T item)

item T
The object that was returned to the pool.

Called when the game starts, before OnStart().

You should not subscribe to events in this method! Use OnStart() instead. OnPreStart() should be used for preparing the scriptable object.

C#
protected override void OnPreStart()

If this isn’t called when the game starts, it’s called once this scriptable object is loaded and instantiated.

Called when the game is ending, after OnPreDisabled().

You should not unsubscribe from events in this method! Use OnPreDisabled() instead. OnDisabled() should be used for cleaning up the scriptable object.

C#
protected override void OnDisabled()

This can also be called during the game is the scriptable object is destroyed and/or unloaded.

Warns if there are any left-over subscribers to the events.

C#
protected override void WarnIfLeftOverSubscribers()

This will only be called in the Unity editor and builds with the DEBUG flag.

Removes any subscribers from the event.

C#
public void ClearSubscribers(bool warnIfLeftOver = false)

warnIfLeftOver bool
If true, a warning will be printed in the console if there are any subscribers. The warning will only be printed in the editor and debug builds.

C#
[Obsolete("Use 'Release' instead. This will be removed in build.", true)]
[ExcludeFromCodeCoverage]
public void Return(T item)

item T

C#
public event PoolChangedArgs<T>? OnPoolChanged

PoolChangedArgs<T>

Called when an object is created.

C#
[Obsolete("Use 'OnPoolChanged' instead. This will be removed in build.", true)]
public event Action<T>? OnCreateObject

Action<T>

Called when an object is destroyed.

C#
[Obsolete("Use 'OnPoolChanged' instead. This will be removed in build.", true)]
public event Action<T>? OnDestroyObject

Action<T>

Called when an object is retrieved.

C#
[Obsolete("Use 'OnPoolChanged' instead. This will be removed in build.", true)]
public event Action<T>? OnGetObject

Action<T>

Called when an object is put back into the pool.

C#
[Obsolete("Use 'OnPoolChanged' instead. This will be removed in build.", true)]
public event Action<T>? OnReturnObject

Action<T>