using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///
/// 缓存池
///
public class CachePoolManager:MBSingletonManager
{
///
/// 对象字典
///
public Dictionary> objDic = new Dictionary>();
///
/// 对象节点
///
public Dictionary nodes = new Dictionary();
///
/// 取对象
///
/// 对象
///
public GameObject Take(GameObject obj)
{
GameObject recordingObj = null;
if (objDic.ContainsKey(obj.name) && objDic[obj.name].Count > 0)
{
recordingObj = objDic[obj.name][0];
objDic[obj.name].RemoveAt(0);
}
else
{
recordingObj = GameObject.Instantiate(obj);
}
recordingObj.transform.SetParent(null);
recordingObj.name = obj.name;
recordingObj.gameObject.SetActive(true);
return recordingObj;
}
///
/// 预加载
///
public void Prestrain(GameObject obj,int number)
{
for (int i = 0; i < number; i++)
{
GameObject recordingObj = GameObject.Instantiate(obj);
Put(recordingObj);
}
}
///
/// 存放
///
/// 对象
public void Put(GameObject obj)
{
string name = obj.name;
obj.gameObject.SetActive(false);
if (objDic.ContainsKey(name))
{
objDic[name].Add(obj);
}
else
{
objDic.Add(name, new List() { obj });
}
if (nodes.ContainsKey(obj.name))
{
obj.transform.SetParent(nodes[obj.name]);
}
else
{
GameObject node = new GameObject(obj.name);
node.transform.SetParent(this.transform);
obj.transform.SetParent(node.transform);
node.transform.localPosition = Vector3.zero;
node.transform.localScale = Vector3.one;
nodes.Add(obj.name, node.transform);
}
}
///
/// 清空
///
public void Clear()
{
StartCoroutine(IE_Destroy());
}
///
/// 清空的协程
///
private IEnumerator IE_Destroy()
{
for (int i = this.transform.childCount-1; i >=0; i--)
{
Destroy(this.transform.GetChild(i).gameObject);
yield return null;
}
objDic.Clear();
nodes.Clear();
}
}