Class PrefabPoolManager
- Namespace
- TAssetBundle.Extensions
- Assembly
- TAssetBundle.Extensions.dll
A class responsible for managing PrefabPool instances. This manager ensures that frequently used prefabs are pooled efficiently, reducing unnecessary instantiation and destruction overhead.
public static class PrefabPoolManager
- Inheritance
-
PrefabPoolManager
- Inherited Members
Properties
PrefabPoolRoot
Gets the root transform for all prefab pools. Pooled objects will be parented under this transform to maintain a clean hierarchy in the scene.
public static Transform PrefabPoolRoot { get; }
Property Value
- Transform
PrefabPools
Gets the dictionary that holds the prefab pools for each prefab. This allows access to the pools for managing the reuse of instantiated prefabs.
public static Dictionary<GameObject, PrefabPool> PrefabPools { get; }
Property Value
- Dictionary<GameObject, PrefabPool>
Methods
Clear()
Clears and destroys all prefab pools. This should be used when transitioning between scenes or when clearing unused assets.
public static void Clear()
Clear(GameObject)
Clears and destroys the prefab pool for the given prefab. This should be used if a specific prefab is no longer needed in the scene.
public static void Clear(GameObject prefab)
Parameters
prefab
GameObjectThe prefab to clear the pool for.
Clear<T>(T)
Clears and destroys the prefab pool for the given prefab type.
public static void Clear<T>(T prefab) where T : Component
Parameters
prefab
TThe prefab to clear the pool for.
Type Parameters
T
The type of the prefab.
Get(GameObject, Transform, bool)
Retrieves an object from the pool for the given prefab, or creates one if none exist. If the prefab is not loaded yet, consider using GetPrefabAsync(string, Transform, bool, bool, object) to load it first.
public static GameObject Get(GameObject prefab, Transform parent = null, bool active = true)
Parameters
prefab
GameObjectThe prefab to get the object for.
parent
TransformThe parent transform for the created object. Defaults to
null
.active
boolWhether the object should be active when created. Defaults to
true
.
Returns
- GameObject
The retrieved or created object.
GetPool(GameObject)
Retrieves the prefab pool for the given prefab, creating one if none exist. If the prefab is dynamically loaded via AssetManager, ensure it is loaded before calling this.
public static PrefabPool GetPool(GameObject prefab)
Parameters
prefab
GameObjectThe prefab to get the pool for.
Returns
- PrefabPool
The prefab pool for the given prefab.
GetPoolObjectCount(GameObject)
Gets the number of objects in the pool for the given prefab.
public static int GetPoolObjectCount(GameObject prefab)
Parameters
prefab
GameObjectThe prefab to get the object count for.
Returns
- int
The number of objects in the pool.
Get<T>(T, Transform, bool)
Retrieves an object of type T
from the pool for the given prefab, or creates one if none exist.
public static T Get<T>(T prefab, Transform parent = null, bool active = true) where T : Component
Parameters
prefab
TThe prefab to get the object for.
parent
TransformThe parent transform for the created object. Defaults to
null
.active
boolWhether the object should be active when created. Defaults to
true
.
Returns
- T
The retrieved or created object of type
T
.
Type Parameters
T
The type of the prefab.
Reserve(GameObject, int)
Reserves a specified number of objects for the given prefab in the pool. This can help reduce instantiation overhead when expecting a large number of object spawns.
public static void Reserve(GameObject prefab, int count)
Parameters
prefab
GameObjectThe prefab to reserve objects for.
count
intThe number of objects to reserve.
Reserve<T>(T, int)
Reserves a specified number of objects for the given prefab type in the pool.
public static void Reserve<T>(T prefab, int count) where T : Component
Parameters
prefab
TThe prefab to reserve objects for.
count
intThe number of objects to reserve.
Type Parameters
T
The type of the prefab.
TryGetPool(GameObject, out PrefabPool)
Attempts to get the pool for the given prefab. Returns false if no pool exists.
public static bool TryGetPool(GameObject prefab, out PrefabPool prefabPool)
Parameters
prefab
GameObjectThe prefab to get the pool for.
prefabPool
PrefabPoolThe prefab pool for the given prefab.
Returns
- bool
True if the pool was found, otherwise false.