ObjectPool<T> Class

Summary

A collection of objects that can create new items as necessary. Get an object from the pool with Weaver.ObjectPool`1.Acquire. Return it to the pool with Weaver.ObjectPool`1.Release(`0).

The non-generic Weaver.ObjectPool class contains some useful methods of creating commonly used pools.

More detailed instructons on how to use this class and those related to it can be found at https://kybernetik.com.au/weaver/docs/misc/object-pooling.
Assembly
Weaver.dll
Namespace
Weaver
Base Types
  • Object
Derived Types
graph BT Type-->Base0["Object"] Type["ObjectPool<T>"] class Type type-node Derived0["PooledList<T>"]-->Type click Derived0 "/weaver/api/Weaver/PooledList_1"

Syntax

public class ObjectPool<T> where T : class

Type Parameters

Name Description
T

Constructors

Name Summary
ObjectPool(Func<T>, int) Creates a new Weaver.ObjectPool`1 which uses the `createItem` function to create new objects when the pool is empty. The pool immediately creates a number of items specified by `preAllocate`.
ObjectPool(ICollection<T>, Func<T>, int) Creates a new Weaver.ObjectPool`1 which uses the `createItem` function to create new objects when the pool is empty. The pool immediately creates a number of items specified by `preAllocate`.

Fields

Name Constant Value Summary
ActiveObjects
The objects currently in use. May be null if this pool doesn't track objects acquired from it. This will be a System.Collections.Generic.HashSet`1 unless a different collection was provided in the constructor.
CreateItem
The factory delegate which is used to create new items if there are none in the pool.
InactiveObjects
The objects currently in the pool waiting to be reused.

Properties

Name Value Summary
Current ObjectPool<T>
Returns the pool currently creating a new item, or null at all other times. This allows the created item's constructor to determine which pool it came from (if any).

If T implements Weaver.IPoolable you should use Weaver.ObjectPool.GetCurrentPool``1 instead.
static
InactiveCount int
The number of items in the Weaver.ObjectPool`1.InactiveObjects list.
OnRelease Action<T>
An optional callback which is triggered by Weaver.ObjectPool`1.Release(`0).

Methods

Name Value Summary
Acquire() T
Returns an available item, either by removing it from the Weaver.ObjectPool`1.InactiveObjects list if it contains any or by creating a new one. Also adds that item to the Weaver.ObjectPool`1.ActiveObjects collection.
AssertNotAlreadyReleased(T) void
[Editor-Conditional] Logs an error if the `item` is already the last object added to the Weaver.ObjectPool`1.InactiveObjects list.
DoInspectorGUI() void
[Editor-Only] Draws the details of this pool in the GUI.
Release(T) void
Puts an item back into the pool to be available for future use.
Release(T) void
Puts an item back into the pool to be available for future use. Also sets the item to to avoid accidental use afterwards.
ReleaseAll() void
Releases all elements of this list back to the pool and clears it.

Throws a System.NullReferenceException if Weaver.ObjectPool`1.ActiveObjects is null.
ReleaseRange(IEnumerable<T>) void
Puts a collection of items back into the pool to be available for future use.
SetMinCount(int) void
Creates new items in the pool until the number of Weaver.ObjectPool`1.InactiveObjects reaches the specified `count`.
ToString() string
Returns a string describing the contents of this pool.

Extension Methods

Name Value Summary
ReleaseAllOnSceneUnload<T>() UnityAction<Scene>
Registers a UnityEngine.SceneManagement.SceneManager.sceneUnloaded callback to call Weaver.ObjectPool`1.ReleaseAll.

The returned delegate can be stored to later unregister from the event if necessary.
TryRelease<T>(T) bool
If the `pool` isn't null this method gives the `item` to it and returns true.