using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Miss
{
public class ObjectPoolManager : SingletonMono<ObjectPoolManager>
{
private Dictionary<string, List<ObjectPoolEntityBase>> pool;
private GameObject m_RootPool;
private GameObject RootPool
{
get
{
if (null == m_RootPool)
{
m_RootPool = new GameObject("RoolPool");
}
return m_RootPool;
}
}
private void Awake()
{
pool = new Dictionary<string, List<ObjectPoolEntityBase>>();
}
public T LoadEntityByResources<T>(string resPath) where T : ObjectPoolEntityBase
{
List<ObjectPoolEntityBase> entityList = null;
ObjectPoolEntityBase entity = null;
if (pool.TryGetValue(resPath, out entityList))
{
if (entityList.Count > 0)
{
entity = entityList[0];
entityList.RemoveAt(0);
entity.gameObject.SetActive(true);
Debug.Log($"{resPath} 出池");
return entity as T;
}
}
GameObject go = Resources.Load(resPath) as GameObject;
if (go == null)
{
Debug.LogError($"go 为空 路径:{resPath}");
return null;
}
go = GameObject.Instantiate(go, RootPool.transform);
go.SetActive(true);
if (!go.TryGetComponent(out entity))
{
entity = go.AddComponent<T>();
entity.Init(resPath);
Debug.Log($"{resPath} 生成");
}
return entity as T;
}
#region 异步出池
public void LoadEntityAsyncByResources<T>(string resPath, Action<ObjectPoolEntityBase> finishCallBack) where T : ObjectPoolEntityBase
{
ObjectPoolEntityBase entity = null;
if (pool.ContainsKey(resPath) && pool[resPath].Count > 0)
{
entity = pool[resPath][0];
entity.gameObject.SetActive(true);
pool[resPath].RemoveAt(0);
Debug.Log("异步出池");
finishCallBack?.Invoke(entity);
return;
}
StartCoroutine(IE_AsyncLoadPrefab<T>(resPath, finishCallBack));
}
private IEnumerator IE_AsyncLoadPrefab<T>(string resPath, Action<ObjectPoolEntityBase> finishCallBack) where T : ObjectPoolEntityBase
{
ResourceRequest request = Resources.LoadAsync(resPath, typeof(GameObject));
yield return request;
if (request.isDone)
{
GameObject go = request.asset as GameObject;
if (go == null)
{
Debug.LogError("go 为空");
}
go = GameObject.Instantiate(go, RootPool.transform);
go.SetActive(true);
ObjectPoolEntityBase entity = null;
if (!go.TryGetComponent(out entity))
{
entity = go.AddComponent<T>();
entity.Init(resPath);
}
Debug.Log("异步生成");
finishCallBack?.Invoke(entity);
}
}
#endregion
#region 回池
public void PushObjectToPool(ObjectPoolEntityBase entity)
{
string resPath = entity.ResPath;
entity.transform.SetParent(RootPool.transform);
if (pool.ContainsKey(resPath))
{
pool[resPath].Add(entity);
}
else
{
pool[resPath] = new List<ObjectPoolEntityBase>() { entity };
}
Debug.Log($"{resPath} 回收完成");
}
#endregion
}
}
using UnityEngine;
namespace Miss
{
public class ObjectPoolEntityBase : MonoBehaviour
{
private string m_ResPath;
public string ResPath => m_ResPath;
public void Init(string resPath)
{
OnInit(resPath);
}
protected virtual void OnInit(string resPath)
{
m_ResPath = resPath;
}
public void Recycle()
{
OnRecycle();
}
protected virtual void OnRecycle()
{
ObjectPoolManager.GetInstance().PushObjectToPool(this);
ResetPrefab();
gameObject.SetActive(false);
}
private void ResetPrefab()
{
transform.localPosition = Vector3.zero;
transform.localEulerAngles = Vector3.zero;
}
}
}
namespace Miss
{
public class SimpleEntity : ObjectPoolEntityBase
{
}
}