对象池粗解

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Pool//对象池
{
public string prefabPath;//注意:把要动态加载的资源对象放在Resources文件夹下
int maxCount = 10; //对象池的最大容量是10个
List usingList = new List();//正在使用的列表
List freeList = new List();//空闲的列表
public GameObject GetObj()//从对象池中获得一个对象
{
GameObject obj = null;
if (freeList.Count > 0)//空闲列表中有对象就直接取出来使用并添加到正在使用的集合列表中
{
obj = freeList[0];//顺序取出空闲的对象
usingList.Add(obj);//添加到正在使用中
freeList.RemoveAt(0);//移除空闲
}
else
{
if (usingList.Count >= maxCount)
{
return null;
}
//没有空闲使用的对象就克隆一个对象出来,然后添加到正在使用
GameObject prefab = Resources.Load(prefabPath);//要克隆的原始对象
obj = GameObject.Instantiate(prefab);
usingList.Add(obj);//添加到正在使用中
}
return obj;
}
public void RealeaseObj(GameObject Obj)//对象没有在使用,则放回空闲列表中
{
usingList.Remove(Obj);
freeList.Add(Obj);
}
}
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class PoolManager //对象池管理类
{
//单例模式
private static PoolManager _instance;
public static PoolManager Instance
{
get
{
if(_instance == null)
{
_instance = new PoolManager();
}
return _instance;
}
}
//所有的对象池的集合,key : 物体的加载路径 value :是某一个对象池类
Dictionary<string,Pool> allPools = new Dictionary<string, Pool>();

//获取对象的接口
public GameObject GetPoolGameObject(string Path)//根据路径,从对象池中查找对象
{
    Pool pool = null;
    if (allPools.ContainsKey(Path))//从所有的池子列表中去寻找,获取到一个已经存在的对象池
    {
        pool = allPools[Path];
    }
    else//还没有加入到列表中,则是第一次Add添加到集合中
    {
        pool = new Pool();
        pool.prefabPath = Path;
        allPools.Add(Path,pool);
    }
    GameObject obj = pool.GetObj();
    if (obj != null)//因为只有10个对象,所以加上代码保护,不为空才可以显示
    {
        obj.SetActive(true);//使用时激活显示 
    }
    return obj;
}
//回收对象的接口
public void ReleasePoolGameObject(string prefabPath,GameObject obj)
{
    Pool pool = allPools[prefabPath];//取出要释放的对象所在的池子
    obj.SetActive(false);
    pool.RealeaseObj(obj);//调用封装的单独的对象池类中的方法RealeaseObj,释放某一个对象(也就是标识为:空闲状态)
}

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PoolTest : MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown(KeyCode.P))
{
string prefabPath = “Prefab/Cube”;//Resources文件夹下的文件路径
OnGetPoolObject(prefabPath);
}
if (Input.GetKeyDown(KeyCode.O))
{
string prefabPath = “Prefab/Sphere”;//Resources文件夹下的文件路径
OnGetPoolObject(prefabPath);
}
}
void OnGetPoolObject(string Path)
{
GameObject obj = PoolManager.Instance.GetPoolGameObject(Path);
if (obj == null)
{
return;
}
obj.transform.position = Vector3.zero;//位置赋值
//回收释放
//如果对象身上没有添加自动回收的叫本,则新添加一个脚本
AutoRelease ar = obj.GetComponent();
if(ar == null)
{
ar = obj.AddComponent();
}
ar.PrefabPath = Path;
ar.BeginRelease();
}
}using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AutoRelease : MonoBehaviour//自动释放对象
{
public string PrefabPath;
public void BeginRelease()
{
StartCoroutine(“IAutoRelese”);
}
IEnumerator IAutoRelese()
{
yield return new WaitForSeconds(2f);
//调用对象池管理类脚本中的回收对象的接口
PoolManager.Instance.ReleasePoolGameObject(PrefabPath,gameObject);
}
}

猜你喜欢

转载自blog.csdn.net/bellainvan/article/details/108643079