Table of Contents

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
Object
Component
Behaviour
MonoBehaviour
PrefabPool
Derived
Inherited Members
MonoBehaviour.IsInvoking()
MonoBehaviour.CancelInvoke()
MonoBehaviour.StopCoroutine(Coroutine)
MonoBehaviour.StopAllCoroutines()
MonoBehaviour.useGUILayout
MonoBehaviour.runInEditMode
Behaviour.enabled
Behaviour.isActiveAndEnabled
Component.GetComponent<T>()
Component.TryGetComponent<T>(out T)
Component.GetComponentInChildren<T>()
Component.GetComponentsInChildren<T>()
Component.GetComponentInParent<T>()
Component.GetComponentsInParent<T>()
Component.GetComponents<T>()
Component.transform
Component.gameObject
Component.tag
Object.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.name
Object.hideFlags
Extension Methods

Properties

Count

Gets the current number of objects in the pool.

public int Count { get; }

Property Value

int

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 Transform

The parent transform for the newly created object.

active bool

The 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 GameObject

The 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 Transform

The parent transform for the object.

active bool

The 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 Transform

The parent transform for the object.

active bool

The 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 GameObject

The 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 int

The 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 Transform

The parent transform for the used object.

active bool

The 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

PrefabPoolDelegate

OnFree

Event triggered when a prefab instance is returned to the pool for reuse.

public event PrefabPoolDelegate OnFree

Event Type

PrefabPoolDelegate

OnUsed

Event triggered when a prefab instance is retrieved from the pool for use.

public event PrefabPoolDelegate OnUsed

Event Type

PrefabPoolDelegate