using System.Collections.Generic; using UnityEngine; public class ProjectileMgr : MonoBehaviourSingletonTemplate { [Header("Pool Setting")] public Transform tf_parent; public int defaultPoolSize = 10; // prefabPath -> pool Dictionary> pools = new Dictionary>(); public void InitPools() { var lst = table_projectile.Ins.Get_DataList(); for (int i = 0; i < lst.Count; i++) { string prefabPath = lst[i].s_ProjectilePrefabs; if (pools.ContainsKey(prefabPath)) continue; Queue q = new Queue(); pools.Add(prefabPath, q); for (int j = 0; j < defaultPoolSize; j++) { Projectile p = CreateExtra(prefabPath); p.gameObject.SetActive(false); q.Enqueue(p); } } } public void Shoot_Projectile(ProjectileData pd) { var proj = Get(pd.m_Data.s_ProjectilePrefabs); proj.Set(pd); } public Projectile Get(string prefabPath) { if (!pools.TryGetValue(prefabPath, out Queue pool)) { Debug.LogError($"[ProjectileMgr] Pool not found : {prefabPath}"); return null; } Projectile p = pool.Count > 0 ? pool.Dequeue() : CreateExtra(prefabPath); p.gameObject.SetActive(true); return p; } public void Return(Projectile p) { p.gameObject.SetActive(false); string key = p.Get_ProjectileTData().s_ProjectilePrefabs; if (!pools.TryGetValue(key, out Queue pool)) { Debug.LogError($"[ProjectileMgr] Return failed. Pool not found : {key}"); return; } pool.Enqueue(p); } Projectile CreateExtra(string prefabPath) { Projectile p = DSUtil.Get_Clone(prefabPath, tf_parent); p.SetOwner(this, prefabPath); return p; } }