// Felix-Bang:FBObjectPool //   へ     /| //  /\7    ∠_/ //  / │   / / // │ Z _,< /   /`ヽ // │     ヽ   /  〉 //  Y     `  /  / // イ● 、 ●  ⊂⊃〈  / // ()  へ    | \〈 //  >ー 、_  ィ  │ // //  / へ   / ノ<| \\ //  ヽ_ノ  (_/  │// //  7       |/ //  >―r ̄ ̄`ー―_ // Describe:对象池 // Createtime:2018/9/18 using System.Collections; using System.Collections.Generic; using UnityEngine; namespace FBFramework { public class FBObjectPool : FBSingleton { public string ResourceDir = ""; Dictionary f_pools = new Dictionary(); /// /// 创建对象 /// /// 对象名 /// public GameObject Spawn(string name) { if (!f_pools.ContainsKey(name)) RegisterNewPool(name); FBSubPool pool = f_pools[name]; return pool.Spawn(); } /// 回收对象 public void Unspawn(GameObject go) { FBSubPool pool = null; foreach (FBSubPool p in f_pools.Values) { if (p.IsContains(go)) { pool = p; break; } } pool.Unspawn(go); } /// 回收所有对象 public void UnspawnAll() { foreach (FBSubPool p in f_pools.Values) p.UnspawnAll(); } /// 创建一个新的SubPool void RegisterNewPool(string name) { string path = ""; if (string.IsNullOrEmpty(ResourceDir)) path = name; else path = ResourceDir + "/" + name; GameObject prefab = Resources.Load(path); FBSubPool pool = new FBSubPool(transform, prefab); f_pools.Add(pool.Name, pool); } } }