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 Projectile Get(int projectileid, Transform tf) { var data = table_projectile.Ins.Get_Data(projectileid); return Get(data.s_ProjectilePrefabs, tf.position, tf.rotation); } public Projectile Get(string prefabPath, Vector3 pos, Quaternion rot) { 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); p.transform.SetPositionAndRotation(pos, rot); 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; } }