Class PrefabPool
- Namespace
- TAssetBundle.Extensions
- Assembly
- TAssetBundle.Extensions.dll
This class manages a pool of objects, allowing for the creation and reuse of GameObjects based on a given prefab.
[AddComponentMenu("")]
[DisallowMultipleComponent]
public class PrefabPool : MonoBehaviour
- Inheritance
-
ObjectComponentBehaviourMonoBehaviourPrefabPool
- Derived
- Inherited Members
-
MonoBehaviour.IsInvoking()MonoBehaviour.CancelInvoke()MonoBehaviour.StopCoroutine(Coroutine)MonoBehaviour.StopAllCoroutines()MonoBehaviour.useGUILayoutMonoBehaviour.runInEditModeBehaviour.enabledBehaviour.isActiveAndEnabledComponent.GetComponent<T>()Component.TryGetComponent<T>(out T)Component.GetComponentInChildren<T>()Component.GetComponentsInChildren<T>()Component.GetComponentInParent<T>()Component.GetComponentsInParent<T>()Component.GetComponents<T>()Component.transformComponent.gameObjectComponent.tagObject.GetInstanceID()Object.GetHashCode()Object.Instantiate(Object, Vector3, Quaternion)Object.Instantiate(Object, Vector3, Quaternion, Transform)Object.Instantiate(Object)Object.Instantiate(Object, Transform)Object.Instantiate<T>(T)Object.Instantiate<T>(T, Vector3, Quaternion)Object.Instantiate<T>(T, Vector3, Quaternion, Transform)Object.Instantiate<T>(T, Transform)Object.Destroy(Object)Object.DestroyImmediate(Object)Object.DontDestroyOnLoad(Object)Object.DestroyObject(Object)Object.FindObjectsOfType<T>()Object.FindObjectsByType<T>(FindObjectsSortMode)Object.FindObjectsByType<T>(FindObjectsInactive, FindObjectsSortMode)Object.FindObjectOfType<T>()Object.FindFirstObjectByType<T>()Object.FindAnyObjectByType<T>()Object.FindFirstObjectByType<T>(FindObjectsInactive)Object.FindAnyObjectByType<T>(FindObjectsInactive)Object.ToString()Object.nameObject.hideFlags
- Extension Methods
Properties
Count
Gets the current number of objects in the pool.
public int Count { get; }
Property Value
InFlightLoads
Number of load coroutines currently using this pool. When greater than zero, destruction is deferred.
public int InFlightLoads { get; }
Property Value
IsPendingDestroy
True when destruction was requested but deferred due to in-flight loads.
public bool IsPendingDestroy { get; }
Property Value
IsPinned
True while at least one load coroutine is in-flight.
public bool IsPinned { get; }
Property Value
Pool
Gets the stack that manages the pool of objects.
public Stack<GameObject> Pool { get; }
Property Value
- Stack<GameObject>
Prefab
Gets the prefab used to create objects in the pool.
public GameObject Prefab { get; }
Property Value
- GameObject
Methods
BeginLoad()
Marks that a load coroutine is using this pool. Prevents destruction until EndLoad is called.
public virtual void BeginLoad()
Clear()
Clears the pool by destroying all objects and clearing the stack.
public void Clear()
Create(Transform, bool)
Creates a new object from the prefab and returns it. The new object is parented to the specified parent transform.
public GameObject Create(Transform parent, bool active)
Parameters
parentTransformThe parent transform for the newly created object.
activeboolThe initial active state of the object.
Returns
- GameObject
The newly created GameObject, or null if the prefab is no longer valid.
EndLoad()
Marks that a load coroutine finished using this pool. Destroys the pool if destruction was requested.
public virtual void EndLoad()
Free(GameObject)
Frees the given object, deactivating it and returning it to the pool.
public void Free(GameObject go)
Parameters
goGameObjectThe GameObject to free.
GetOrCreate(Transform, bool)
Gets an object from the pool or creates a new one if the pool is empty.
public GameObject GetOrCreate(Transform parent, bool active = true)
Parameters
parentTransformThe parent transform for the object.
activeboolThe active state of the object.
Returns
- GameObject
The object from the pool or a newly created one.
GetOrCreate<T>(Transform, bool)
Gets or creates an object of a specified component type from the pool.
public T GetOrCreate<T>(Transform parent, bool active = true) where T : Component
Parameters
parentTransformThe parent transform for the object.
activeboolThe active state of the object.
Returns
- T
The component of type T attached to the object.
Type Parameters
TThe type of the component to get.
Init(GameObject)
Initializes the pool with the given prefab.
public void Init(GameObject prefab)
Parameters
prefabGameObjectThe GameObject prefab to be used in the pool.
IsEmpty()
Checks if the pool is empty.
public bool IsEmpty()
Returns
- bool
True if the pool is empty, otherwise false.
OnDestroy()
protected virtual void OnDestroy()
RequestDestroy()
Requests destruction of this pool. If in-flight loads exist, defers until all loads finish.
public void RequestDestroy()
Reserve(int)
Reserves a specified number of objects by creating them if needed.
public void Reserve(int count)
Parameters
countintThe number of objects to reserve.
Use(Transform, bool)
Uses an object from the pool by popping it and setting it as a child of the specified parent transform. Skips destroyed references and falls back to Create if all pooled items are invalid.
public GameObject Use(Transform parent, bool active)
Parameters
parentTransformThe parent transform for the used object.
activeboolThe active state of the object.
Returns
- GameObject
The object used from the pool, or a newly created one.
Events
OnCreated
Event triggered when a new prefab instance is created in the pool.
public event PrefabPoolDelegate OnCreated
Event Type
OnFree
Event triggered when a prefab instance is returned to the pool for reuse.
public event PrefabPoolDelegate OnFree
Event Type
OnUsed
Event triggered when a prefab instance is retrieved from the pool for use.
public event PrefabPoolDelegate OnUsed