/// <summary> /// 清除某个资源在对象池中所有的对象 /// </summary> /// <param name="crc"></param> public void ClearPoolObject(uint crc) { List <ResouceObj> st = null; if (!m_ObjectPoolDic.TryGetValue(crc, out st) || st == null) { return; } for (int i = st.Count - 1; i >= 0; i--) { ResouceObj resObj = st[i]; if (resObj.m_bClear) { st.Remove(resObj); int tempID = resObj.m_CloneObj.GetInstanceID(); GameObject.Destroy(resObj.m_CloneObj); resObj.Reset(); m_ResouceObjDic.Remove(tempID); m_ResourceObjClassPool.Recycle(resObj); } } if (st.Count <= 0) { m_ObjectPoolDic.Remove(crc); } }
/// <summary> /// 取消异步加载 /// </summary> /// <param name="guid"></param> public void CancleLoad(long guid) { ResouceObj resObj = null; if (m_AsyncResObjs.TryGetValue(guid, out resObj) && ResourceManager.Instance.CancleLoad(resObj)) { m_AsyncResObjs.Remove(guid); resObj.Reset(); m_ResourceObjClassPool.Recycle(resObj); } }
/// <summary> /// 清空对象池 /// </summary> public void ClearCache() { List <uint> tempList = new List <uint>(); foreach (uint key in m_ObjectPoolDic.Keys) { List <ResouceObj> st = m_ObjectPoolDic[key]; for (int i = st.Count - 1; i >= 0; i--) { ResouceObj resObj = st[i]; if (!System.Object.ReferenceEquals(resObj.m_CloneObj, null) && resObj.m_bClear) { GameObject.Destroy(resObj.m_CloneObj); m_ResouceObjDic.Remove(resObj.m_CloneObj.GetInstanceID()); resObj.Reset(); m_ResourceObjClassPool.Recycle(resObj); st.Remove(resObj); } } 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; } ResouceObj resObj = null; int tempID = obj.GetInstanceID(); if (!m_ResouceObjDic.TryGetValue(tempID, out resObj)) { Debug.Log(obj.name + "对象不是ObjectManager创建的!"); return; } if (resObj == null) { Debug.LogError("缓存的ResouceObj为空!"); return; } if (resObj.m_Already) { Debug.LogError("该对象已经放回对象池了,检测自己是否情况引用!"); return; } #if UNITY_EDITOR obj.name += "(Recycle)"; #endif List <ResouceObj> st = null; if (maxCacheCount == 0) { m_ResouceObjDic.Remove(tempID); ResourceManager.Instance.ReleaseResouce(resObj, destoryCache); resObj.Reset(); m_ResourceObjClassPool.Recycle(resObj); } else//回收到对象池 { if (!m_ObjectPoolDic.TryGetValue(resObj.m_Crc, out st) || st == null) { st = new List <ResouceObj>(); m_ObjectPoolDic.Add(resObj.m_Crc, st); } if (resObj.m_CloneObj) { if (recycleParent) { resObj.m_CloneObj.transform.SetParent(RecyclePoolTrs); } else { resObj.m_CloneObj.SetActive(false); } } if (maxCacheCount < 0 || st.Count < maxCacheCount) { st.Add(resObj); resObj.m_Already = true; //ResourceManager做一个引用计数 ResourceManager.Instance.DecreaseResoucerRef(resObj); } else { m_ResouceObjDic.Remove(tempID); ResourceManager.Instance.ReleaseResouce(resObj, destoryCache); resObj.Reset(); m_ResourceObjClassPool.Recycle(resObj); } } }