1.给放入缓存池的gameovject设置父节点
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 缓存池模块
/// 1.Dictionary List
/// 2.Gameobject和Resources 两个公共类的API
/// </summary>
public class poolMgr:BaseManger<poolMgr>
{
//缓存池容器
public Dictionary<string, List<GameObject>> poolDic = new Dictionary<string, List<GameObject>>();
private GameObject poolobj;
/// <summary>
/// 往外拿东西
/// name是预设体的名字和路径
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public GameObject GetObj(string name)
{
GameObject obj = null;
//缓存池里有gameobject
if (poolDic.ContainsKey(name) && poolDic[name].Count > 0)
{
obj = poolDic[name][0];
poolDic[name].RemoveAt(0);
}
//缓存池没有,加载预设体
else
{
obj = GameObject.Instantiate(Resources.Load<GameObject>(name));
//把对象名字改的和池子名字一样
obj.name = name;
}
//取出来的时候激活
obj.SetActive(true);
//断开了父子关系
obj.transform.parent = null;
return obj;
}
/// <summary>
/// 还暂时不用的东西
/// </summary>
public void PushObj(string name,GameObject obj)
{
//放进去的时候让gameobject失活
obj.SetActive(false);
if (poolobj == null)
{
poolobj = new GameObject("Pool");
}
//设置父对象为根节点
obj.transform.parent = poolobj.transform;
//里面有gameobject
if (poolDic.ContainsKey(name))
{
poolDic[name].Add(obj);
}
//里面没有
else
{
poolDic.Add(name, new List<GameObject>() { obj });
}
}
/// <summary>
/// 清空缓存池的方法
/// 用在场景切换时
/// </summary>
public void PoolClear()
{
poolDic.Clear();
poolobj = null;
}
}
2.将缓存对象的父节点划分的更细
缓存对象在层级窗口的结构:
池子容器类–PoolData
/// <summary>
/// 池子数据
/// </summary>
public class PoolData
{
//池子中存储对象的父节点(目的是让层级面板看起来不乱)
public GameObject fatherObj;
//池子列表容器,用于存储不同的对象
public List<GameObject> poolList;
//初始化单一池子 将池子父对象传入 新建一个对象作为它的子对象
public PoolData(GameObject obj, GameObject poolObj)
{
//新建一个池子父对象
fatherObj = new GameObject(obj.name);
fatherObj.transform.parent = poolObj.transform;
//fatherObj.transform.SetParent(poolObj.transform);
//新建容器
poolList = new List<GameObject>() { };
PushObj(obj);
}
public void PushObj(GameObject obj)
{
//失活,让其隐藏
obj.SetActive(false);
//存起来
poolList.Add(obj);
//设置父对象
obj.transform.parent = fatherObj.transform;
}
//从列表里取东西
public GameObject GetObj()
{
GameObject obj = null;
//取出第一个
obj = poolList[0];
poolList.RemoveAt(0);
//激活 让其显示
obj.SetActive(true);
//断开父子关系
obj.transform.parent = null;
return obj;
}
}
池子管理类–PoolMgr.cs
public class poolMgr:BaseManger<poolMgr>
{
//缓存池容器
public Dictionary<string, PoolData> poolDic = new Dictionary<string,PoolData>();
private GameObject poolobj;
/// <summary>
/// 往外拿东西
/// name是预设体的名字和路径
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public GameObject GetObj(string name)
{
GameObject obj = null;
//缓存池里有gameobject
if (poolDic.ContainsKey(name) && poolDic[name].poolList.Count > 0)
{
obj = poolDic[name].GetObj();
}
//缓存池没有,加载预设体
else
{
obj = GameObject.Instantiate(Resources.Load<GameObject>(name));
//把对象名字改的和池子名字一样
obj.name = name;
}
return obj;
}
/// <summary>
/// 还暂时不用的东西
/// </summary>
public void PushObj(string name,GameObject obj)
{
if (poolobj == null)
{
poolobj = new GameObject("Pool");
}
//里面有gameobject
if (poolDic.ContainsKey(name))
{
poolDic[name].PushObj(obj);
}
//里面没有
else
{
poolDic.Add(name, new PoolData(obj,poolobj));
}
}
/// <summary>
/// 清空缓存池的方法
/// 用在场景切换时
/// </summary>
public void PoolClear()
{
poolDic.Clear();
poolobj = null;
}
}