/// <summary> /// 清除某个资源在对象池中所有的对象 /// </summary> public void ClearPoolObject(uint crc) { List <ResourceObj> st = null; if (!m_ObjectPoolDic.TryGetValue(crc, out st) || st == null) { return; } for (int i = st.Count - 1; i >= 0; i--) { ResourceObj resourceObj = st[i]; if (resourceObj.m_bClear) { st.Remove(resourceObj); int tempID = resourceObj.m_CloneObj.GetInstanceID(); GameObject.Destroy(resourceObj.m_CloneObj); resourceObj.Reset(); m_ResourceObjDic.Remove(tempID); m_ResourceObjClassPool.Recycle(resourceObj); } } if (st.Count <= 0) { m_ObjectPoolDic.Remove(crc); } }
/// <summary> /// 取消异步加载 /// </summary> /// <param name="guid"></param> public void CancleLoad(long guid) { ResourceObj resOjbObj = null; if (m_AsyncResObj.TryGetValue(guid, out resOjbObj) && ResourceManager.Instance.CancleLoad(resOjbObj)) { m_AsyncResObj.Remove(guid); resOjbObj.Reset(); m_ResourceObjClassPool.Recycle(resOjbObj); } }
/// <summary> /// 清空对象池 /// </summary> public void ClearCache() { List <uint> tempList = new List <uint>(); foreach (var key in m_ObjectPoolDic.Keys) { List <ResourceObj> st = m_ObjectPoolDic[key]; for (int i = st.Count - 1; i >= 0; i--) { ResourceObj resourceObj = st[i]; if (!System.Object.ReferenceEquals(resourceObj.m_CloneObj, null) && resourceObj.m_bClear) { GameObject.Destroy(resourceObj.m_CloneObj); m_ResourceObjDic.Remove(resourceObj.m_CloneObj.GetInstanceID()); resourceObj.Reset(); m_ResourceObjClassPool.Recycle(resourceObj); } } if (st.Count <= 0) { tempList.Add(key); } } for (int i = 0; i < tempList.Count; i++) { uint temp = tempList[i]; if (m_ObjectPoolDic.ContainsKey(temp)) { m_ObjectPoolDic.Remove(temp); } } tempList.Clear(); }
/// <summary> /// 释放需要实例化的资源 /// </summary> /// <param name="obj"></param> /// <param name="maxCacheCount"></param> /// <param name="destoryCache"></param> /// <param name="recycleParent"></param> public void ReleaseObject(GameObject obj, int maxCacheCount = -1, bool destoryCache = false, bool recycleParent = true) { if (obj == null) { return; } ResourceObj resObj = null; int tempID = obj.GetInstanceID(); if (!m_ResourceObjDic.TryGetValue(tempID, out resObj)) { Debug.Log($"{obj.name}对象非ObjectManager创建"); } if (resObj == null) { Debug.LogError("缓存的ResourceObj为空!"); return; } if (resObj.m_CloneObj == null) { Debug.LogError("该对象已经放回对象池,检测自己是否清空引用!"); return; } #if UNITY_EDITOR obj.name += "(Recycle)"; #endif List <ResourceObj> st = null; if (maxCacheCount == 0) { m_ResourceObjDic.Remove(tempID); ResourceManager.Instance.ReleaseResource(resObj, destoryCache); resObj.Reset(); m_ResourceObjClassPool.Recycle(resObj); } else//回收到对象池 { if (!m_ObjectPoolDic.TryGetValue(resObj.m_Crc, out st) || st == null) { st = new List <ResourceObj>(); m_ObjectPoolDic.Add(resObj.m_Crc, st); } if (resObj.m_CloneObj) { if (recycleParent) { resObj.m_CloneObj.transform.SetParent(ResyclePoolTrs); } else { resObj.m_CloneObj.SetActive(false); } } if (maxCacheCount < 0 || st.Count < maxCacheCount) { st.Add(resObj); resObj.m_Already = true; ResourceManager.Instance.DecreaseResourceRef(resObj); } else { m_ResourceObjDic.Remove(tempID); ResourceManager.Instance.ReleaseResource(resObj, destoryCache); resObj.Reset(); m_ResourceObjClassPool.Recycle(resObj); } } }