81 lines
2.3 KiB
C#
81 lines
2.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class PoolData
|
|
{
|
|
public GameObject fatherObj;
|
|
public List<GameObject> poolList;
|
|
public PoolData(GameObject obj,GameObject poolParent)
|
|
{
|
|
fatherObj = new GameObject(obj.name);
|
|
fatherObj.transform.parent = poolParent.transform;
|
|
poolList = new List<GameObject>();
|
|
PushObj(obj);
|
|
}
|
|
|
|
public void PushObj(GameObject obj)
|
|
{
|
|
poolList.Add(obj);
|
|
obj.transform.parent = fatherObj.transform;
|
|
obj.SetActive(false);
|
|
}
|
|
|
|
public GameObject GetObj()
|
|
{
|
|
GameObject obj;
|
|
obj = poolList[0];
|
|
poolList.RemoveAt(0);
|
|
obj.SetActive(true);
|
|
obj.transform.parent = null;
|
|
return obj;
|
|
}
|
|
}
|
|
|
|
public class Pool : Singleton<Pool>
|
|
{
|
|
public Dictionary<string, PoolData> dic = new Dictionary<string, PoolData>();//生成一个字典
|
|
private GameObject poolObj;//Scene窗口中的“pool”总收纳盒
|
|
public GameObject Get(string name, Vector3 position, Quaternion rotation)//返回一个预制体,
|
|
{
|
|
GameObject obj;
|
|
if (dic.ContainsKey(name) && dic[name].poolList.Count > 0)//如果字典里有这个东西,并且它的数量大于0就取出来
|
|
{
|
|
obj = dic[name].GetObj();
|
|
obj.transform.position = position;//实例化时候的参数传过来
|
|
obj.transform.rotation = rotation;//同上
|
|
}
|
|
else
|
|
{
|
|
obj = Object.Instantiate(Resources.Load(name), position, rotation) as GameObject;//没有就读取一个,不用存入字典
|
|
obj.name = name;
|
|
}
|
|
return obj;
|
|
}
|
|
public GameObject Return(GameObject obj)//隐藏掉
|
|
{
|
|
if(obj.activeSelf)
|
|
{
|
|
if(poolObj==null)
|
|
{
|
|
poolObj = new GameObject("pool");
|
|
}
|
|
string name = obj.name;
|
|
if (dic.ContainsKey(name))//如果找到了这个名字
|
|
{
|
|
dic[name].PushObj(obj);
|
|
}
|
|
else
|
|
{
|
|
dic.Add(name, new PoolData(obj, poolObj) { });
|
|
}
|
|
}
|
|
return obj;
|
|
}
|
|
public void Clear()//切换场景时将所有缓存清除
|
|
{
|
|
dic.Clear();
|
|
poolObj = null;
|
|
}
|
|
}
|