Class ObjectPool
A pool that can be used to store old instances of objects when not in use, and can be renabled if needed.
This is done to reduce the amount of calls to GameObject.Instantiate and GameObject.Destroy
NOTE: Every object used through this pool must have a PoolableObject component attached
Namespace: WeaverCore
Assembly: WeaverCore.dll
Syntax
public sealed class ObjectPool : MonoBehaviour
Fields
| Edit this page View SourceMultiThreaded
Can the pool work with multiple threads?
Declaration
public static readonly bool MultiThreaded
Field Value
Type | Description |
---|---|
bool |
ResetComponents
Should the pool reset all components on an object to a clean slate when returned back to the pool?
Declaration
public bool ResetComponents
Field Value
Type | Description |
---|---|
bool |
ResetPositions
Should the pool reset the transform data on an object back to a clean slate when returned back to the pool?
Declaration
public bool ResetPositions
Field Value
Type | Description |
---|---|
bool |
Properties
| Edit this page View SourceAmountInPool
Declaration
public int AmountInPool { get; }
Property Value
Type | Description |
---|---|
int |
Prefab
The prefab this pool is instantiating
Declaration
public PoolableObject Prefab { get; set; }
Property Value
Type | Description |
---|---|
PoolableObject |
Methods
| Edit this page View SourceClearPool()
Clears the pool of all it's stored objects
Declaration
public void ClearPool()
Create(Component)
Creates a new pool for a prefab
Declaration
public static ObjectPool Create(Component prefab)
Parameters
Type | Name | Description |
---|---|---|
Component | prefab | The prefab the pool will be instantiating |
Returns
Type | Description |
---|---|
ObjectPool | Returns a new pool for the prefab |
Exceptions
Type | Condition |
---|---|
Exception | Throws if the prefab is null |
Create(GameObject)
Creates a new pool for a prefab
Declaration
public static ObjectPool Create(GameObject prefab)
Parameters
Type | Name | Description |
---|---|---|
GameObject | prefab | The prefab the pool will be instantiating |
Returns
Type | Description |
---|---|
ObjectPool | Returns a new pool for the prefab |
Exceptions
Type | Condition |
---|---|
Exception | Throws if the prefab is null |
Create(PoolableObject)
Creates a new pool for a prefab
Declaration
public static ObjectPool Create(PoolableObject prefab)
Parameters
Type | Name | Description |
---|---|---|
PoolableObject | prefab | The prefab the pool will be instantiating |
Returns
Type | Description |
---|---|
ObjectPool | Returns a new pool for the prefab |
FillPool(int)
Fills the pool with a certain amount of objects
Declaration
public void FillPool(int amount)
Parameters
Type | Name | Description |
---|---|---|
int | amount | The amount to fill the pool with |
FillPoolAsync(int)
Fills the pool with a certain amount of objects. This function will add 1 object to the pool each frame to stagger instantiations
Declaration
public void FillPoolAsync(int amount)
Parameters
Type | Name | Description |
---|---|---|
int | amount | The amount to fill the pool with |
Instantiate()
Instantiates a new object from the pool
Declaration
public GameObject Instantiate()
Returns
Type | Description |
---|---|
GameObject | The new object instance |
Instantiate(Transform)
Instantiates a new object from the pool
Declaration
public GameObject Instantiate(Transform parent)
Parameters
Type | Name | Description |
---|---|---|
Transform | parent | The parent transform of the instantiated object |
Returns
Type | Description |
---|---|
GameObject | The new object instance |
Instantiate(Transform, bool)
Instantiates a new object from the pool
Declaration
public GameObject Instantiate(Transform parent, bool instantiateInWorldSpace)
Parameters
Type | Name | Description |
---|---|---|
Transform | parent | The parent transform of the instantiated object |
bool | instantiateInWorldSpace | Should the object be positioned in world space? If false, it will be positioned relative to the parent |
Returns
Type | Description |
---|---|
GameObject | The new object instance |
Instantiate(Vector3, Quaternion)
Instantiates a new object from the pool
Declaration
public GameObject Instantiate(Vector3 position, Quaternion rotation)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | position | The position of the instantiated object |
Quaternion | rotation | The rotation of the instantiated object |
Returns
Type | Description |
---|---|
GameObject | The new object instance |
Instantiate(Vector3, Quaternion, Transform)
Instantiates a new object from the pool
Declaration
public GameObject Instantiate(Vector3 position, Quaternion rotation, Transform parent)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | position | The position of the instantiated object |
Quaternion | rotation | The rotation of the instantiated object |
Transform | parent | The parent transform of the instantiated object |
Returns
Type | Description |
---|---|
GameObject | The new object instance |
Instantiate<T>()
Instantiates a new object from the pool
Declaration
public T Instantiate<T>()
Returns
Type | Description |
---|---|
T | The new object instance |
Type Parameters
Name | Description |
---|---|
T | The component type to get from the instantiated object |
Instantiate<T>(Transform)
Instantiates a new object from the pool
Declaration
public T Instantiate<T>(Transform parent)
Parameters
Type | Name | Description |
---|---|---|
Transform | parent | The parent transform of the instantiated object |
Returns
Type | Description |
---|---|
T | The new object instance |
Type Parameters
Name | Description |
---|---|
T | The component type to get from the instantiated object |
Instantiate<T>(Transform, bool)
Instantiates a new object from the pool
Declaration
public T Instantiate<T>(Transform parent, bool instantiateInWorldSpace)
Parameters
Type | Name | Description |
---|---|---|
Transform | parent | The parent transform of the instantiated object |
bool | instantiateInWorldSpace | Should the object be positioned in world space? If false, it will be positioned relative to the parent |
Returns
Type | Description |
---|---|
T | The new object instance |
Type Parameters
Name | Description |
---|---|
T | The component type to get from the instantiated object |
Instantiate<T>(Vector3, Quaternion)
Instantiates a new object from the pool
Declaration
public T Instantiate<T>(Vector3 position, Quaternion rotation)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | position | The position of the instantiated object |
Quaternion | rotation | The rotation of the instantiated object |
Returns
Type | Description |
---|---|
T | The new object instance |
Type Parameters
Name | Description |
---|---|
T | The component type to get from the instantiated object |
Instantiate<T>(Vector3, Quaternion, Transform)
Instantiates a new object from the pool
Declaration
public T Instantiate<T>(Vector3 position, Quaternion rotation, Transform parent)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | position | The position of the instantiated object |
Quaternion | rotation | The rotation of the instantiated object |
Transform | parent | The parent transform of the instantiated object |
Returns
Type | Description |
---|---|
T | The new object instance |
Type Parameters
Name | Description |
---|---|
T | The component type to get from the instantiated object |
Exceptions
Type | Condition |
---|---|
Exception | Throws if the component type |
ReturnToPool(GameObject)
Returns an object to the pool
Declaration
public void ReturnToPool(GameObject gameObject)
Parameters
Type | Name | Description |
---|---|---|
GameObject | gameObject | The object to return |
ReturnToPool(GameObject, float)
Returns an object to the pool
Declaration
public void ReturnToPool(GameObject gameObject, float time)
Parameters
Type | Name | Description |
---|---|---|
GameObject | gameObject | The object to return |
float | time | The delay before the object is put back in the pool |
Exceptions
Type | Condition |
---|---|
Exception | Throws if the returning object doesn't have a PoolableObject component attached |
ReturnToPool(PoolableObject)
Returns an object to the pool
Declaration
public void ReturnToPool(PoolableObject poolableObject)
Parameters
Type | Name | Description |
---|---|---|
PoolableObject | poolableObject | The object to return |
ReturnToPool(PoolableObject, float)
Returns an object to the pool
Declaration
public void ReturnToPool(PoolableObject poolableObject, float time)
Parameters
Type | Name | Description |
---|---|---|
PoolableObject | poolableObject | The object to return |
float | time | The delay before the object is put back in the pool |
Exceptions
Type | Condition |
---|---|
Exception | Throws if the returning object doesn't have a PoolableObject component attached |
ReturnToPool<T>(T)
Returns an object to the pool
Declaration
public void ReturnToPool<T>(T component) where T : Component
Parameters
Type | Name | Description |
---|---|---|
T | component | The object to return |
Type Parameters
Name | Description |
---|---|
T | The type of the component |
ReturnToPool<T>(T, float)
Returns an object back to the pool
Declaration
public void ReturnToPool<T>(T component, float time) where T : Component
Parameters
Type | Name | Description |
---|---|---|
T | component | The component on the object being returned |
float | time | A delay before the object is returned to the pool |
Type Parameters
Name | Description |
---|---|
T | The type of the component |
Exceptions
Type | Condition |
---|---|
Exception | Throws if the returning object doesn't have a PoolableObject component attached |