ObjectPool Class

Summary

Various utilities and extension methods for Weaver.ObjectPool`1.

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
graph BT Type-->Base0["Object"] Type["ObjectPool"] class Type type-node

Syntax

public static class ObjectPool

Methods

Name Value Summary
CreateComponentPool<T>(T, int, bool) ObjectPool<T>
Creates an Weaver.ObjectPool`1 that creates new items by instantiating a specified UnityEngine.Component. The objects will not be destroyed by scene loading.

In the Unity Editor the instantiated objects will be grouped under a common parent to keep the hierarchy view tidy, but this step is skipped in runtime builds for efficiency.
static
CreateDefaultPool<T>(ICollection<T>, int) ObjectPool<T>
Creates an Weaver.ObjectPool`1 that creates new items using a parameterless constructor.
static
CreateDefaultPool<T>(int) ObjectPool<T>
Creates an Weaver.ObjectPool`1 that creates new items using a the default constructor of T and uses a System.Collections.Generic.HashSet`1 to keep track of its Weaver.ObjectPool`1.ActiveObjects.
static
CreatePrefabPool(GameObject, int, bool) ObjectPool<GameObject>
Creates an Weaver.ObjectPool`1 that creates new items by instantiating a specified UnityEngine.GameObject. The objects will not be destroyed by scene loading.

In the Unity Editor the instantiated objects will be grouped under a common parent to keep the hierarchy view tidy, but this step is skipped in runtime builds for efficiency.
static
DestroyExcess<T>(ObjectPool<T>, int) void
Removes and destroys items from the pool until the System.Collections.Generic.List`1.Count reaches the `remainingSize`.
static
GetCurrentPool<T>() 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).

Also registers the Weaver.IPoolable.OnRelease of T as the Weaver.ObjectPool`1.OnRelease callback if nothing was previously registered.
static
GetFunctionToInstantiateComponent<T>(T) Func<T>
Creates a delegate that instantiates an inactive copy of the `original` object and calls UnityEngine.Object.DontDestroyOnLoad(UnityEngine.Object) on it.

In the Unity Editor the copies are also grouped under a parent object to keep the hierarchy view clean.
static
GetFunctionToInstantiatePrefab(GameObject) Func<GameObject>
Creates a delegate that instantiates an inactive copy of the `original` object and calls UnityEngine.Object.DontDestroyOnLoad(UnityEngine.Object) on it.

In the Unity Editor the copies are also grouped under a parent object to keep the hierarchy view clean.
static
GetSharedComponentPool<T>(T, int, bool) ObjectPool<T>
The first time this method is called for a particular `original` it will call Weaver.ObjectPool.CreateComponentPool``1(``0,System.Int32,System.Boolean) and cache the returned pool so that subsequent calls using the same `original` will return the same pool.
static
GetSharedPrefabPool(GameObject, int, bool) ObjectPool<GameObject>
The first time this method is called for a particular `original` it will call Weaver.ObjectPool.CreatePrefabPool(UnityEngine.GameObject,System.Int32,System.Boolean) and cache the returned pool so that subsequent calls using the same `original` will return the same pool.
static
ReleaseAllOnSceneUnload<T>(ObjectPool<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.
static
TryRelease<T>(ObjectPool<T>, T) bool
If the `pool` isn't null this method gives the `item` to it and returns true.
static
TryReleaseOrDestroy<T>(ObjectPool<T>, T) bool
If the `pool` isn't null this method gives the `item` to it and returns true. Otherwise this method destroys it and returns false.

Note that this method is likely not what you want to use for UnityEngine.Components. Use Weaver.ObjectPool.TryReleaseOrDestroyGameObject``1(Weaver.ObjectPool{``0},``0) instead.
static
TryReleaseOrDestroyGameObject(ObjectPool<GameObject>, GameObject) bool
If the `pool` isn't null this method gives the `component` to it, disables its UnityEngine.GameObject, and returns true. Otherwise this method destroys the UnityEngine.GameObject and returns false.
static
TryReleaseOrDestroyGameObject<T>(ObjectPool<T>, T) bool
If the `pool` isn't null this method releases the `component` to it, disables its UnityEngine.GameObject, and returns true. Otherwise this method destroys the UnityEngine.GameObject and returns false.
static
TryReleaseOrDestroyGameObject<T>(T) bool
If the `behaviour` was created by an Weaver.ObjectPool`1 this method releases the `behaviour` to it, disables its UnityEngine.GameObject, and returns true. Otherwise this method destroys the UnityEngine.GameObject and returns false.
static