// Felix-Bang:FBSubPool //   へ     /| //  /\7    ∠_/ //  / │   / / // │ Z _,< /   /`ヽ // │     ヽ   /  〉 //  Y     `  /  / // イ● 、 ●  ⊂⊃〈  / // ()  へ    | \〈 //  >ー 、_  ィ  │ // //  / へ   / ノ<| \\ //  ヽ_ノ  (_/  │// //  7       |/ //  >―r ̄ ̄`ー―_ using System.Collections; using System.Collections.Generic; using UnityEngine; namespace FBFramework { public class FBSubPool { Transform f_parent; GameObject f_prefab; List f_objects = new List(); /// 名称标识 public string Name { get { return f_prefab.name; } } /// /// 构造 /// /// 预制件 public FBSubPool(Transform parent, GameObject prefab) { f_parent = parent; f_prefab = prefab; } /// 创建对象 public GameObject Spawn() { GameObject go = null; foreach (var obj in f_objects) { if (!obj.activeSelf) { go = obj; break; } } if (go == null) { go = GameObject.Instantiate(f_prefab); go.transform.parent = f_parent; f_objects.Add(go); } go.SetActive(true); go.SendMessage("OnSpawn", SendMessageOptions.DontRequireReceiver); return go; } /// 回收对象 public void Unspawn(GameObject go) { if (IsContains(go)) { go.SendMessage("OnUnspawn", SendMessageOptions.DontRequireReceiver); go.SetActive(false); } } /// 回收本对象池全部对象 public void UnspawnAll() { foreach (var item in f_objects) { if (item.activeSelf) Unspawn(item); } } /// /// 对象池是否包含对象 /// /// 目标对象 /// public bool IsContains(GameObject go) { return f_objects.Contains(go); } } }