using System.Collections; using System.Collections.Generic; using UnityEngine; public class ObjectPool : MonoSingleton { /// /// 资源目录 /// public string ResourceDir = ""; Dictionary m_pools = new Dictionary(); //取出物体 //public GameObject Spawn(string name,Transform trans) { // SubPool pool = null; // if (!m_pools.ContainsKey(name)) { // RegieterNew(name,trans); // } // pool = m_pools[name]; // return pool.Spawn(trans); //} public GameObject Spawn(GameObject go, Transform trans) { SubPool pool = null; if (!m_pools.ContainsKey(go.name)) { RegieterNew(go, trans); } pool = m_pools[go.name]; return pool.Spawn(trans); } //回收物体 public void Unspawn(GameObject go) { SubPool pool = null; foreach (var p in m_pools.Values) { if (p.Contain(go)) { pool = p; break; } else { pool = new SubPool(go); } } pool.UnSpawn(go); } //回收所有 public void UnspawnAll() { foreach (var p in m_pools.Values) { p.UnspawnAll(); } } //清除所有 public void Clear() { m_pools.Clear(); } //新建一个池子 void RegieterNew(GameObject go,Transform trans) { //资源目录 // string path = ResourceDir + "/" + names; //生成预制体 // GameObject go = Resources.Load(path); //新建一池子 SubPool pool = new SubPool(go); m_pools.Add(pool.Name,pool); } }