using System.Collections.Generic; using Unity.Mathematics; using UnityEngine;
public class GamePoolManager : SingleMono<GamePoolManager> { [System.Serializable] private class PoolItem { public string itemName; public GameObject item; public int itemCount; } [SerializeField]private List<PoolItem> poolList = new List<PoolItem>(); private Dictionary<string,Queue<GameObject>> poolDic = new Dictionary<string,Queue<GameObject>>(); private GameObject _poolParrent;
private void Awake() { _poolParrent = new GameObject("对象池父对象"); _poolParrent.transform.SetParent(transform); InitPool(); } //把东西塞池子里 private void InitPool() { //遍历池子 for (int i = 0; i < poolList.Count; i++) { for(int j = 0; j<poolList[i].itemCount; j++) { var item1 = Instantiate(poolList[i].item); item1.transform.SetParent(_poolParrent.transform); item1.SetActive(false); //如果没有对应的池子,我们new一个 if (!poolDic.ContainsKey(poolList[i].itemName)) { poolDic.Add(poolList[i].itemName, new Queue<GameObject>()); } poolDic[poolList[i].itemName].Enqueue(item1); } } }