Class ObjectPool
Object pool containing several copies of a template object (usually a prefab). Using the pool with GetObject and Release provides a high speed alternative to repeatedly calling Instantiate and Destroy.
Namespace: QFSW.MOP2
Assembly: QFSW.MOP2.dll
Syntax
[CreateAssetMenu(fileName = "Untitled Pool", menuName = "Master Object Pooler 2/Object Pool", order = 0)]
public class ObjectPool : ScriptableObject
Properties
| Improve this Doc View SourceIncrementalInstanceNames
If enabled, object instances will be renamed to ObjectName#XXX where XXX is the instance number. This is useful if you want them all to be uniquely named.
Declaration
public bool IncrementalInstanceNames { get; set; }
Property Value
| Type | Description |
|---|---|
| Boolean |
Initialized
Declaration
public bool Initialized { get; }
Property Value
| Type | Description |
|---|---|
| Boolean |
ObjectParent
Parent transform for all pooled objects.
Declaration
public Transform ObjectParent { get; }
Property Value
| Type | Description |
|---|---|
| UnityEngine.Transform |
PoolName
The name of the pool. Used for identification and as the key when using a MasterObjectPooler.
Declaration
public string PoolName { get; }
Property Value
| Type | Description |
|---|---|
| String |
Methods
| Improve this Doc View SourceCreate(GameObject, Int32, Int32)
Creates an ObjectPool.
Declaration
public static ObjectPool Create(GameObject template, int defaultSize = 0, int maxSize = -1)
Parameters
| Type | Name | Description |
|---|---|---|
| UnityEngine.GameObject | template | The template object to center the pool on. All objects in the pool will be a copy of this object. |
| Int32 | defaultSize | The default number of objects to create in this pool when initializing it. |
| Int32 | maxSize | The maximum number of objects that can be kept in this pool. If it is exceeded, objects will be destroyed instead of pooled when returned. Set to -1 for no limit. |
Returns
| Type | Description |
|---|---|
| ObjectPool | The created ObjectPool. |
Create(GameObject, String, Int32, Int32)
Creates an ObjectPool.
Declaration
public static ObjectPool Create(GameObject template, string name, int defaultSize = 0, int maxSize = -1)
Parameters
| Type | Name | Description |
|---|---|---|
| UnityEngine.GameObject | template | The template object to center the pool on. All objects in the pool will be a copy of this object. |
| String | name | The name of the pool. Used for identification and as the key when using a MasterObjectPooler. |
| Int32 | defaultSize | The default number of objects to create in this pool when initializing it. |
| Int32 | maxSize | The maximum number of objects that can be kept in this pool. If it is exceeded, objects will be destroyed instead of pooled when returned. Set to -1 for no limit. |
Returns
| Type | Description |
|---|---|
| ObjectPool | The created ObjectPool. |
CreateAndInitialize(GameObject, Int32, Int32)
Creates an ObjectPool and initializes it.
Declaration
public static ObjectPool CreateAndInitialize(GameObject template, int defaultSize = 0, int maxSize = -1)
Parameters
| Type | Name | Description |
|---|---|---|
| UnityEngine.GameObject | template | The template object to center the pool on. All objects in the pool will be a copy of this object. |
| Int32 | defaultSize | The default number of objects to create in this pool when initializing it. |
| Int32 | maxSize | The maximum number of objects that can be kept in this pool. If it is exceeded, objects will be destroyed instead of pooled when returned. Set to -1 for no limit. |
Returns
| Type | Description |
|---|---|
| ObjectPool | The created ObjectPool. |
CreateAndInitialize(GameObject, String, Int32, Int32)
Creates an ObjectPool and initializes it.
Declaration
public static ObjectPool CreateAndInitialize(GameObject template, string name, int defaultSize = 0, int maxSize = -1)
Parameters
| Type | Name | Description |
|---|---|---|
| UnityEngine.GameObject | template | The template object to center the pool on. All objects in the pool will be a copy of this object. |
| String | name | The name of the pool. Used for identification and as the key when using a MasterObjectPooler. |
| Int32 | defaultSize | The default number of objects to create in this pool when initializing it. |
| Int32 | maxSize | The maximum number of objects that can be kept in this pool. If it is exceeded, objects will be destroyed instead of pooled when returned. Set to -1 for no limit. |
Returns
| Type | Description |
|---|---|
| ObjectPool | The created ObjectPool. |
Destroy(IEnumerable<GameObject>)
Forcibly destroys a collection of objects and does not return them to the pool.
Declaration
public void Destroy(IEnumerable<GameObject> objs)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<UnityEngine.GameObject> | objs | The objects to destroy. |
Destroy(GameObject)
Forcibly destroys the object and does not return it to the pool.
Declaration
public void Destroy(GameObject obj)
Parameters
| Type | Name | Description |
|---|---|---|
| UnityEngine.GameObject | obj | The object to destroy. |
GetAllActiveObjects()
Gets all active objects in the pool.
Declaration
public IEnumerable<GameObject> GetAllActiveObjects()
Returns
| Type | Description |
|---|---|
| IEnumerable<UnityEngine.GameObject> | The active objects. |
GetObject()
Gets an object from the pool.
Declaration
public GameObject GetObject()
Returns
| Type | Description |
|---|---|
| UnityEngine.GameObject | The retrieved object. |
GetObject(Vector3)
Gets an object from the pool.
Declaration
public GameObject GetObject(Vector3 position)
Parameters
| Type | Name | Description |
|---|---|---|
| UnityEngine.Vector3 | position | The position to set the object to. |
Returns
| Type | Description |
|---|---|
| UnityEngine.GameObject | The retrieved object. |
GetObject(Vector3, Quaternion)
Gets an object from the pool.
Declaration
public GameObject GetObject(Vector3 position, Quaternion rotation)
Parameters
| Type | Name | Description |
|---|---|---|
| UnityEngine.Vector3 | position | The position to set the object to. |
| UnityEngine.Quaternion | rotation | The rotation to set the object to. |
Returns
| Type | Description |
|---|---|
| UnityEngine.GameObject | The retrieved object. |
GetObjectComponent<T>()
Gets an object from the pool, and then retrieves the specified component using a cache to improve performance.
Declaration
public T GetObjectComponent<T>()
where T : class
Returns
| Type | Description |
|---|---|
| T | The retrieved component. |
Type Parameters
| Name | Description |
|---|---|
| T | The component type to get. |
GetObjectComponent<T>(GameObject)
Retrieves the specified component from an object using a cache to improve performance.
Declaration
public T GetObjectComponent<T>(GameObject obj)
where T : class
Parameters
| Type | Name | Description |
|---|---|---|
| UnityEngine.GameObject | obj | The object to get the component from. |
Returns
| Type | Description |
|---|---|
| T | The retrieved component. |
Type Parameters
| Name | Description |
|---|---|
| T | The component type to get. |
GetObjectComponent<T>(Vector3)
Gets an object from the pool, and then retrieves the specified component using a cache to improve performance.
Declaration
public T GetObjectComponent<T>(Vector3 position)
where T : class
Parameters
| Type | Name | Description |
|---|---|---|
| UnityEngine.Vector3 | position | The position to set the object to. |
Returns
| Type | Description |
|---|---|
| T | The retrieved component. |
Type Parameters
| Name | Description |
|---|---|
| T | The component type to get. |
GetObjectComponent<T>(Vector3, Quaternion)
Gets an object from the pool, and then retrieves the specified component using a cache to improve performance.
Declaration
public T GetObjectComponent<T>(Vector3 position, Quaternion rotation)
where T : class
Parameters
| Type | Name | Description |
|---|---|---|
| UnityEngine.Vector3 | position | The position to set the object to. |
| UnityEngine.Quaternion | rotation | The rotation to set the object to. |
Returns
| Type | Description |
|---|---|
| T | The retrieved component. |
Type Parameters
| Name | Description |
|---|---|
| T | The component type to get. |
Initialize(Boolean)
Initializes the ObjectPool.
Declaration
public void Initialize(bool forceReinitialization = false)
Parameters
| Type | Name | Description |
|---|---|---|
| Boolean | forceReinitialization |
Populate(Int32, PopulateMethod)
Populates the pool with the specified number of objects, so that they do not need instantiating later.
Declaration
public void Populate(int quantity, PopulateMethod method = PopulateMethod.Set)
Parameters
| Type | Name | Description |
|---|---|---|
| Int32 | quantity | The number of objects to populate it with. |
| PopulateMethod | method | The population mode. |
Purge()
Destroys every object in the pool, both alive and pooled.
Declaration
public void Purge()
Release(IEnumerable<GameObject>)
Releases a collection of objects and returns them back to the pool, effectively 'destroying' them from the scene.
Declaration
public void Release(IEnumerable<GameObject> objs)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<UnityEngine.GameObject> | objs | the objects to release. |
Release(GameObject)
Releases an object and returns it back to the pool, effectively 'destroying' it from the scene. Pool equivalent of Destroy.
Declaration
public void Release(GameObject obj)
Parameters
| Type | Name | Description |
|---|---|---|
| UnityEngine.GameObject | obj | The object to release. |
ReleaseAll()
Releases every active object in this pool.
Declaration
public void ReleaseAll()