Unity_实现最简单的缓存池模块

小鸟游星野
小鸟游星野
发布于 2025-03-11 / 7 阅读
0
1

Unity_实现最简单的缓存池模块

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class MyPoolObjectPos
{
   private GameObject objFather;
   private Stack<GameObject> poolStack = new Stack<GameObject>();
   private List<GameObject> poolList = new List<GameObject>();
 
   public int usedCount => poolList.Count;

   public void PushList(GameObject obj)
   {
      poolList.Add(obj);
   }
   public int Count
   {
      get
      {
         return poolStack.Count;
      }
   }
 
   public GameObject Pop()
   {
      GameObject obj;
      if (Count > 0)
      {
         obj = poolStack.Pop();
         PushList(obj);
      }
      else
      {
         obj = poolList[0];
         poolList.RemoveAt(0);
         poolList.Add(obj);
      }
      obj.SetActive(true);
      obj.transform.SetParent(null);
      Debug.Log("当前数量" + Count);
      return obj;
   }
 
   public void Push(GameObject obj)
   {
      poolStack.Push(obj);
      poolList.Remove(obj);
      obj.transform.SetParent(objFather.transform);
      obj.SetActive(false);
      Debug.Log("当前数量" + Count);
   }
   
   //外部在创建新的字典时肯定会有新的对象 我们直接将其压入list
   public MyPoolObjectPos(GameObject outFather,string name, GameObject used)
   {
      objFather = new GameObject();
      objFather.name = name;
      objFather.transform.SetParent(outFather.transform);
      PushList(used);
      
   }
}
public class 我的缓存池PartOne : NoMonoSingleton<我的缓存池PartOne>
{
   public GameObject poolObj = new GameObject();
   public Dictionary<string,MyPoolObjectPos> poolDict = new Dictionary<string,MyPoolObjectPos>();
   //缓存池由字典和栈或队列组成,是由一堆相同的内容组成的
   private 我的缓存池PartOne()
   {
      
   }
 
   //为了方便存储我们将字典中栈容器的key与对象预设体的名字设置一致
   
   /// <summary>
   /// 从抽屉中取出对象
   /// </summary>
   /// <param name="name">对象的名字</param>
   /// <returns></returns>
   public GameObject GetGameObject(string name,int max = 100)
   {
      GameObject obj = null;
      
      #region 加入数量上限后的逻辑判断

      if (!poolDict.ContainsKey(name))
      {
         
         obj = GameObject.Instantiate(Resources.Load<GameObject>(name));
         obj.name = name;
         poolDict.Add(name,new MyPoolObjectPos(poolObj,name,obj));
      }
      else if(poolDict[name].Count > 0 || poolDict[name].usedCount >= max)
      {
         obj = poolDict[name].Pop();
      }
      else
      {
         obj = GameObject.Instantiate(Resources.Load<GameObject>(name));
         obj.name = name;
         
         poolDict[name].PushList(obj);
      }

      #endregion
      
      #region 没有加入上限时的逻辑

      // if (poolDict.ContainsKey(name) && poolDict[name].Count > 0)
      // {
      //    obj = poolDict[name].Pop();
      // }
      // else
      // {
      //    obj = GameObject.Instantiate(Resources.Load<GameObject>(name));
      //    obj.name = name;
      // }

      #endregion
    
      
      return obj;
   }
 
 
   public void PushGameObject(GameObject obj)
   {
      //压入对象创建一个父对象
      

      poolDict[obj.name].Push(obj);
      
   }
   
   
   //清空缓存池引用数据
   public void PoolClear()
   {
      poolDict.Clear(); 
      poolObj = null;
   }
}


评论