using System.Collections.Generic; using UnityEngine; namespace OM { /// /// Manages object pools for efficient instantiation and reuse of GameObjects. /// public class OMObjectPoolManager : MonoBehaviour { /// /// Represents a single object pool item with its configuration. /// [System.Serializable] public class OMObjectPoolItem { public string key; public bool rename = true; public GameObject prefab; public bool useIPool = false; public int preload = 0; public int maxCapacity = 10; public OMObjectPool Pool { get; private set; } /// /// Sets up the object pool item with the specified parent transform. /// /// public void Setup(Transform parent) { Pool = new OMObjectPool(maxCapacity, preload, useIPool, () => { var obj = Instantiate(prefab, parent); obj.gameObject.SetActive(false); if (rename) { obj.name += $" _{Pool.GetSpawnedCount}"; } return obj; }, obj => { obj.SetActive(true); }, obj => { obj.SetActive(false); obj.transform.SetParent(parent, true); }); } } /// /// Singleton instance of the OMObjectPoolManager. /// public static OMObjectPoolManager Instance { get; private set; } /// /// array of object pool items to be managed. /// [SerializeField] private OMObjectPoolItem[] items; /// /// Dictionary to hold the object pools by their keys. /// private readonly Dictionary> _dictionary = new(); /// /// Initializes the object pool manager and sets up the pools. /// private void Awake() { Instance = this; foreach (var item in items) { var parent = new GameObject($"Pool ({item.key})"); parent.transform.SetParent(transform, true); item.Setup(parent.transform); _dictionary.TryAdd(item.key, item.Pool); } } /// /// Gets the object pool associated with the specified key. /// public OMObjectPool GetPoolByKey(string key) { if (_dictionary.TryGetValue(key, out var pool)) { return pool; } Debug.LogError($"[OMObjectPoolManager] No pool found with key: {key}"); return null; } /// /// Spawns an object from the pool associated with the specified key. /// public GameObject Spawn(string key) { return GetPoolByKey(key)?.Spawn(); } /// /// Spawns an object from the pool and sets its position. /// public GameObject Spawn(string key, Vector3 pos) { var o = Spawn(key); if (o != null) o.transform.position = pos; return o; } /// /// Spawns an object from the pool and sets its position and rotation. /// public GameObject Spawn(string key, Vector3 pos, Quaternion rot) { var o = Spawn(key); if (o != null) o.transform.SetPositionAndRotation(pos, rot); return o; } /// /// Spawns an object from the pool and sets its position, rotation, and parent. /// public GameObject Spawn(string key, Vector3 pos, Quaternion rot, Transform parent) { var o = Spawn(key); if (o != null) { o.transform.SetPositionAndRotation(pos, rot); o.transform.SetParent(parent, true); } return o; } /// /// Spawns an object from the pool and returns its component of type T. /// public T Spawn(string key) { var o = Spawn(key); return o ? o.GetComponent() : default; } /// /// Spawns an object from the pool, sets its position, and returns its component of type T. /// public T Spawn(string key, Vector3 pos) { var o = Spawn(key, pos); return o ? o.GetComponent() : default; } /// /// Spawns an object from the pool, sets its position and rotation, and returns its component of type T. /// public T Spawn(string key, Vector3 pos, Quaternion rot) { var o = Spawn(key, pos, rot); return o ? o.GetComponent() : default; } /// /// Spawns an object from the pool, sets its position, rotation, and parent, and returns its component of type T. /// public T Spawn(string key, Vector3 pos, Quaternion rot, Transform parent) { var o = Spawn(key, pos, rot, parent); return o ? o.GetComponent() : default; } /// /// Despawns an object back to the pool associated with the specified key. /// public void Despawn(string key, GameObject obj) { var pool = GetPoolByKey(key); pool?.Despawn(obj); } /// /// Despawns an object back to the pool associated with the specified key. /// public void Despawn(string key, T obj) where T : MonoBehaviour { Despawn(key, obj.gameObject); } } }