using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PoolManager
{
public static PoolManager instance;
private Dictionary<string, List<GameObject>> poolDic;
public PoolManager()
{
instance = new PoolManager();
poolDic = new Dictionary<string, List<GameObject>>();
}
public GameObject Get(string name)
{
GameObject obj = null;
if (poolDic.ContainsKey(name) && poolDic[name].Count > 0)
{
List<GameObject> list = poolDic[name];
obj = list[0];
obj.SetActive(true);
list.RemoveAt(0);
}
else
{
obj = SimpleFactory.CreateGameObject(name);
obj.name = name;
}
return obj;
}
public void ReturnPool(GameObject obj)
{
obj.SetActive(false);
if (poolDic.ContainsKey(obj.name))
{
poolDic[obj.name].Add(obj);
}
else
{
List<GameObject> list = new List<GameObject>();
list.Add(obj);
poolDic.Add(obj.name, list);
}
}
}
#Unity _ 对象池
最新推荐文章于 2024-01-11 13:57:09 发布