自己写的简单对象池
将预制体放在Resources文件加下 EasyObjectPath 为枚举类型,也就是保存预制体的名称 也是通过名称保存预制体
PoolManager.GetObj(EasyObjectPath.Cube);
PoolManager.Recycle(gameobject);
poolManager.ClearObjPool(EasyObjectPath objname);
继承BasePoolObject的对象使用
OnCreate();
Recycle();
using System.Collections.Generic;
using UnityEngine;
namespace F_EasyWork
{
public abstract class BasePoolObject : MonoBehaviour
{
public abstract void OnCreate(Object data = null);
public virtual void Recycle()
{
PoolManager.Recycle(gameObject);
}
[System.Obsolete(" ----继承 BasePoolObject 的对象销毁,请使用 Recycle()方法!----")]
public virtual void OnDestroy()
{
}
}
public class PoolManagerObject : MonoBehaviour
{
Dictionary<string, GameObject> prefabPools = new Dictionary<string, GameObject>();
internal GameObject InstantiateNewObject(EasyObjectPath enumobjectName, Object data = null)
{
string objectName = enumobjectName.ToString();
if (!prefabPools.ContainsKey(objectName))
{
GameObject prefab = Resources.Load<GameObject>(objectName);
if (prefab != null)
{
prefabPools.Add(objectName, prefab);
}
else
{
Debug.LogError("确保预制体路径 : " + objectName + " 存在");
return null;
}
}
GameObject obj = Instantiate(prefabPools[objectName]);
BasePoolObject poolObject = obj.GetComponent<BasePoolObject>();
if (poolObject)
{
poolObject.OnCreate(data);
}
obj.name = objectName;
return obj;
}
internal void ClearObj(EasyObjectPath path)
{
string objName=path.ToString();
for (int i = 0; i < transform .childCount; i++)
{
if (transform .GetChild(i).name ==objName )
{
if (!transform .GetChild (i).gameObject .activeSelf )
{
Destroy(transform.GetChild(i).gameObject);
}
}
}
}
}
public class PoolManager : Singleton<PoolManager>
{
public PoolManager()
{
GameObject obj = new GameObject();
obj.name = "PoolManagerObject";
m_poolManagerObject = obj.AddComponent<PoolManagerObject>();
}
Dictionary<string, Queue<GameObject>> objectPools = new Dictionary<string, Queue<GameObject>>();
PoolManagerObject m_poolManagerObject = null;
public static GameObject GetObj(EasyObjectPath objName, Object data = null)
{
string objectNameorPath = objName.ToString();
if (Instance.objectPools.ContainsKey(objectNameorPath))
{
if (Instance.objectPools[objectNameorPath].Count > 0)
{
foreach (var item in Instance.objectPools[objectNameorPath])
{
if (item != null)
{
GameObject obj = Instance.objectPools[objectNameorPath].Dequeue();
BasePoolObject basePoolObject = obj.GetComponent<BasePoolObject>();
if (basePoolObject != null)
{
basePoolObject.OnCreate(data);
}
obj.SetActive(true);
return obj;
}
}
}
}
GameObject tmp = Instance.m_poolManagerObject.InstantiateNewObject(objName, data);
return tmp;
}
internal static void Recycle(GameObject obj)
{
if (!Instance.objectPools.ContainsKey(obj.name))
{
Instance.objectPools.Add(obj.name ,new Queue<GameObject>());
}
Instance.objectPools[obj.name].Enqueue(obj);
obj.gameObject.SetActive(false);
obj.transform.parent = Instance.m_poolManagerObject.transform;
}
public static void ClearObjPool(EasyObjectPath name)
{
Instance .m_poolManagerObject .ClearObj (name);
Instance.objectPools.Remove(name.ToString ());
}
}
}
|