using System.Collections.Generic;
|
using UnityEngine;
|
using System;
|
|
|
|
public class GameObjectPoolManager : SingletonMonobehaviour<GameObjectPoolManager>
|
{
|
#if UNITY_EDITOR
|
private Dictionary<int, DebugItem> m_DebugInstIDDict = null;
|
#endif
|
private Dictionary<int, GameObjectPool> m_PoolDict = null;
|
|
// 不需要销毁的对象列表
|
private List<int> dontDestoryGoInstIDList = null;
|
|
private Transform m_TargetContainer;
|
|
//需要动态卸载的预制体,例如切换场景时
|
public List<GameObject> needDestroyPrefabList = new List<GameObject>();
|
|
public void Initialize()
|
{
|
if (m_PoolDict == null)
|
{
|
m_PoolDict = new Dictionary<int, GameObjectPool>();
|
}
|
|
if (!m_TargetContainer)
|
{
|
m_TargetContainer = new GameObject("Container").transform;
|
m_TargetContainer.SetParent(transform);
|
}
|
|
if (dontDestoryGoInstIDList == null)
|
{
|
dontDestoryGoInstIDList = new List<int>();
|
}
|
|
#if UNITY_EDITOR
|
if (m_DebugInstIDDict == null)
|
{
|
m_DebugInstIDDict = new Dictionary<int, DebugItem>();
|
}
|
#endif
|
}
|
|
public void AddDontDestroyGoInstID(int id)
|
{
|
if (!dontDestoryGoInstIDList.Contains(id))
|
{
|
dontDestoryGoInstIDList.Add(id);
|
}
|
}
|
|
public bool ExistPool(GameObject prefab)
|
{
|
if (prefab == null)
|
{
|
return false;
|
}
|
|
if (m_PoolDict.ContainsKey(prefab.GetInstanceID()))
|
{
|
return true;
|
}
|
|
return false;
|
}
|
|
public GameObjectPool RequestPool(GameObject prefab)
|
{
|
if (prefab == null)
|
{
|
return null;
|
}
|
|
int _prefabInstanceId = prefab.GetInstanceID();
|
|
GameObjectPool _pool = null;
|
if (m_PoolDict.TryGetValue(_prefabInstanceId, out _pool))
|
{
|
return _pool;
|
}
|
|
_pool = new GameObjectPool(prefab);
|
m_PoolDict[_prefabInstanceId] = _pool;
|
|
return _pool;
|
}
|
|
|
public void CacheGameObject(GameObject prefab, int count, bool _prefabActive)
|
{
|
if (prefab == null)
|
{
|
return;
|
}
|
|
RequestPool(prefab).Cache(count, _prefabActive);
|
}
|
|
|
public GameObject RequestGameObject(GameObject prefab)
|
{
|
if (prefab == null)
|
{
|
return null;
|
}
|
|
return RequestPool(prefab).Request();
|
}
|
|
//特定情况下需要动态卸载的,如切换场景时
|
public void UnLoadAll(bool force = false)
|
{
|
List<int> _removeList = new List<int>();
|
foreach (var _pool in m_PoolDict.Values)
|
{
|
if (needDestroyPrefabList.Contains(_pool.Prefab))
|
{
|
_pool.Clear();
|
_removeList.Add(_pool.PrefabInstanceId);
|
}
|
}
|
|
for (int i = 0; i < _removeList.Count; ++i)
|
{
|
m_PoolDict.Remove(_removeList[i]);
|
}
|
}
|
|
public class GameObjectPool
|
{
|
private GameObject m_Prefab = null;
|
public GameObject Prefab
|
{
|
get
|
{
|
return m_Prefab;
|
}
|
}
|
|
//预制体实例id,用于检索池
|
public int PrefabInstanceId
|
{
|
get; private set;
|
}
|
|
public HashSet<GameObject> m_ActiveHashSet = new HashSet<GameObject>();
|
private Queue<GameObject> m_FreeQueue = null;
|
private int Pool_FreeList_Warning_Threshold = 8;
|
|
Action<GameObject> releaseCallBack;
|
#if UNITY_EDITOR && !UNITY_ANDROID && !UNITY_IOS
|
private Transform m_DebugContainer;
|
private Transform m_DebugActive;
|
private Transform m_DebugFree;
|
#endif
|
|
public string assetBundleName;
|
public string assetName;
|
|
internal protected GameObjectPool(GameObject prefab)
|
{
|
m_Prefab = prefab;
|
PrefabInstanceId = m_Prefab.GetInstanceID();
|
m_ActiveHashSet = new HashSet<GameObject>();
|
m_FreeQueue = new Queue<GameObject>();
|
|
#if UNITY_EDITOR && !UNITY_ANDROID && !UNITY_IOS
|
m_DebugContainer = new GameObject(prefab.name).transform;
|
m_DebugActive = new GameObject("Active").transform;
|
m_DebugFree = new GameObject("Free").transform;
|
m_DebugActive.SetParent(m_DebugContainer);
|
m_DebugFree.SetParent(m_DebugContainer);
|
m_DebugContainer.SetParent(Instance.transform);
|
#endif
|
}
|
|
public void AddReleaseListener(Action<GameObject> _callBack)
|
{
|
releaseCallBack = _callBack;
|
}
|
|
public void RemoveReleaseListener()
|
{
|
releaseCallBack = null;
|
}
|
|
public void Cache(int count, bool _prefabActive)
|
{
|
GameObject _go;
|
for (int i = 0; i < count; ++i)
|
{
|
_go = Instantiate(m_Prefab, Constants.Special_Hide_Position, Quaternion.identity);
|
m_FreeQueue.Enqueue(_go);
|
_go.transform.SetParent(Instance.m_TargetContainer);
|
_go.transform.position = Constants.Special_Hide_Position;
|
|
|
#if UNITY_EDITOR && !UNITY_ANDROID && !UNITY_IOS
|
// 检查空闲列表数量是否超过阈值
|
if (m_FreeList.Count > Pool_FreeList_Warning_Threshold)
|
{
|
Debug.LogWarning($"GameObjectPool {m_Prefab.name} 的空闲对象数量 ({m_FreeList.Count}) 超过阈值 ({Pool_FreeList_Warning_Threshold}),请检查是否需要清理。");
|
}
|
|
DebugItem _debugItem = new GameObject(_go.GetInstanceID().ToString()).AddMissingComponent<DebugItem>();
|
_debugItem.transform.SetParent(m_DebugFree);
|
_debugItem.target = _go;
|
Instance.m_DebugInstIDDict[_go.GetInstanceID()] = _debugItem;
|
#endif
|
var animator = _go.GetComponent<Animator>();
|
if (animator != null)
|
{
|
animator.enabled = false;
|
}
|
_go.SetActive(_prefabActive);
|
}
|
}
|
|
public GameObject Request()
|
{
|
GameObject _go = null;
|
|
if (m_FreeQueue.Count > 0)
|
{
|
_go = m_FreeQueue.Dequeue();
|
|
m_ActiveHashSet.Add(_go);
|
|
#if UNITY_EDITOR && !UNITY_ANDROID && !UNITY_IOS
|
if (Instance.m_DebugInstIDDict.ContainsKey(_go.GetInstanceID()))
|
{
|
DebugItem _debugItem = Instance.m_DebugInstIDDict[_go.GetInstanceID()];
|
_debugItem.transform.SetParent(m_DebugActive);
|
}
|
#endif
|
}
|
|
|
if (_go == null)
|
{
|
_go = Instantiate(m_Prefab, Constants.Special_Hide_Position, Quaternion.identity);
|
m_ActiveHashSet.Add(_go);
|
|
#if UNITY_EDITOR && !UNITY_ANDROID && !UNITY_IOS
|
DebugItem _debugItem = new GameObject(_go.GetInstanceID().ToString()).AddMissingComponent<DebugItem>();
|
_debugItem.transform.SetParent(m_DebugActive);
|
_debugItem.target = _go;
|
Instance.m_DebugInstIDDict[_go.GetInstanceID()] = _debugItem;
|
#endif
|
}
|
|
_go.transform.SetParent(null);
|
_go.transform.localScale = Vector3.one;
|
|
return _go;
|
}
|
|
public void Release(GameObject go)
|
{
|
if (go == null)
|
{
|
return;
|
}
|
m_ActiveHashSet.Remove(go);
|
|
m_FreeQueue.Enqueue(go);
|
go.transform.SetParent(Instance.m_TargetContainer);
|
go.transform.position = Constants.Special_Hide_Position;
|
|
|
#if UNITY_EDITOR && !UNITY_ANDROID && !UNITY_IOS
|
// 检查空闲列表数量是否超过阈值
|
if (m_FreeList.Count > Pool_FreeList_Warning_Threshold)
|
{
|
Debug.LogWarning($"GameObjectPool {m_Prefab.name} 的空闲对象数量 ({m_FreeList.Count}) 超过阈值 ({Pool_FreeList_Warning_Threshold}),请检查是否需要清理。");
|
}
|
if (Instance.m_DebugInstIDDict.ContainsKey(go.GetInstanceID()))
|
{
|
DebugItem _debugItem = Instance.m_DebugInstIDDict[go.GetInstanceID()];
|
_debugItem.transform.SetParent(m_DebugFree);
|
}
|
#endif
|
|
if (releaseCallBack != null)
|
{
|
releaseCallBack(go);
|
}
|
}
|
|
public void ReleaseAll()
|
{
|
//释放 m_ActiveHashSet中 的GameObject,排除
|
foreach (GameObject go in m_ActiveHashSet)
|
{
|
if (go != null)
|
{
|
m_FreeQueue.Enqueue(go);
|
go.transform.SetParent(Instance.m_TargetContainer);
|
go.transform.position = Constants.Special_Hide_Position;
|
|
#if UNITY_EDITOR && !UNITY_ANDROID && !UNITY_IOS
|
if (Instance.m_DebugInstIDDict.ContainsKey(go.GetInstanceID()))
|
{
|
DebugItem _debugItem = Instance.m_DebugInstIDDict[go.GetInstanceID()];
|
_debugItem.transform.SetParent(m_DebugFree);
|
}
|
#endif
|
if (releaseCallBack != null)
|
{
|
releaseCallBack(go);
|
}
|
}
|
|
}
|
m_ActiveHashSet.Clear();
|
}
|
|
public void Clear()
|
{
|
foreach (GameObject go in m_ActiveHashSet)
|
{
|
if (go != null)
|
{
|
if (!Instance.dontDestoryGoInstIDList.Contains(go.GetInstanceID()))
|
{
|
m_ActiveHashSet.Remove(go);
|
Destroy(go);
|
}
|
|
}
|
|
}
|
|
#if UNITY_EDITOR && !UNITY_ANDROID && !UNITY_IOS
|
if (m_ActiveHashSet.Count != 0)
|
{
|
Debug.LogWarningFormat("{0} 池清理后, 还有 {1} 个活跃对象. ", m_Prefab.name, m_ActiveHashSet.Count);
|
}
|
#endif
|
|
Queue<GameObject> tempQueue = new Queue<GameObject>();
|
while (m_FreeQueue.Count > 0)
|
{
|
GameObject obj = m_FreeQueue.Dequeue();
|
if (Instance.dontDestoryGoInstIDList.Contains(obj.GetInstanceID()))
|
tempQueue.Enqueue(obj);
|
else
|
Destroy(obj);
|
}
|
m_FreeQueue = tempQueue;
|
|
|
#if UNITY_EDITOR && !UNITY_ANDROID && !UNITY_IOS
|
if (m_FreeList.Count != 0)
|
{
|
Debug.LogWarningFormat("{0} 池清理后, 还有 {1} 个空闲对象. ", m_Prefab.name, m_FreeList.Count);
|
// for (int i = 0; i < m_FreeList.Count; ++i)
|
// {
|
// Debug.LogWarningFormat(" |-- {0}", m_FreeList[i].name);
|
// }
|
}
|
#endif
|
if (IsEmpty())
|
{
|
m_Prefab = null;
|
#if UNITY_EDITOR && !UNITY_ANDROID && !UNITY_IOS
|
Destroy(m_DebugContainer.gameObject);
|
#endif
|
}
|
|
if (!string.IsNullOrEmpty(assetBundleName) && !string.IsNullOrEmpty(assetName))
|
{
|
ResManager.Instance.UnloadAsset(assetBundleName, assetName);
|
// ResManager.Instance.UnloadAssetBundle(assetBundleName, true, false);
|
}
|
}
|
|
public bool IsEmpty()
|
{
|
return m_ActiveHashSet.Count == 0 && m_FreeQueue.Count == 0;
|
}
|
}
|
}
|