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.FindObjectOfType<T>()Object.ToString()Object.nameObject.hideFlags
- Extension Methods
Properties
Count
Gets the current number of objects in the pool.
public int Count { 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
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
parent
TransformThe parent transform for the newly created object.
active
boolThe initial active state of the object.
Returns
- GameObject
The newly created GameObject.
Free(GameObject)
Frees the given object, deactivating it and returning it to the pool.
public void Free(GameObject go)
Parameters
go
GameObjectThe 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
parent
TransformThe parent transform for the object.
active
boolThe 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
parent
TransformThe parent transform for the object.
active
boolThe active state of the object.
Returns
- T
The component of type T attached to the object.
Type Parameters
T
The type of the component to get.
Init(GameObject)
Initializes the pool with the given prefab.
public void Init(GameObject prefab)
Parameters
prefab
GameObjectThe 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.
Reserve(int)
Reserves a specified number of objects by creating them if needed.
public void Reserve(int count)
Parameters
count
intThe 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.
public GameObject Use(Transform parent, bool active)
Parameters
parent
TransformThe parent transform for the used object.
active
boolThe active state of the object.
Returns
- GameObject
The object used from the pool.
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