using System.Collections; using System.Collections.Generic; using UnityEngine; using ZXKFramework; public class GameObject_Pool : MonoSingleton { Dictionary> poolDic = new Dictionary>(); // Start is called before the first frame update /// /// 取出游戏物体 /// /// 取出的类型 /// 取出来以后放到哪个父物体下 /// public GameObject TakeOutGame(string typeName, Transform parent) { if (poolDic.ContainsKey(typeName) && poolDic[typeName].Count > 0 && poolDic[typeName] != null) { GameObject go = poolDic[typeName].Dequeue(); go.transform.SetParent(parent); //this.Log($"取出对象池,现在长度为:{poolDic[typeName].Count}"); return go; } else { if (!poolDic.ContainsKey(typeName)) { poolDic.Add(typeName, new Queue()); } if (poolDic[typeName] == null) { poolDic[typeName] = new Queue(); } if (poolDic[typeName].Count == 0) { GameObject go = Resources.Load(typeName); GameObject obj = GameObject.Instantiate(go, parent); poolDic[typeName].Enqueue(obj); //this.Log($"取出对象池,现在长度为:{poolDic[typeName].Count}"); return poolDic[typeName].Dequeue(); } return null; } } /// /// 存入对象池 /// /// 存入类型 /// 存入的物体 public void PutInGame(string typeName, GameObject obj) { if (poolDic[typeName] != null) { poolDic[typeName].Enqueue(obj); //this.Log($"放入对象池,现在长度为:{poolDic[typeName].Count}"); } else { poolDic[typeName] = new Queue(); poolDic[typeName].Enqueue(obj); } } }