/// <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="crc"></param> /// <returns></returns> protected ResouceObj GetObjectFromPool(uint crc) { List <ResouceObj> st = null; if (m_ObjectPoolDic.TryGetValue(crc, out st) && st != null && st.Count > 0) { ResourceManager.Instance.IncreaseResouceRef(crc); ResouceObj resObj = st[0]; st.RemoveAt(0); GameObject obj = resObj.m_CloneObj; if (!System.Object.ReferenceEquals(obj, null)) { resObj.m_Already = false; #if UNITY_EDITOR if (obj.name.EndsWith("(Recycle)")) { obj.name = obj.name.Replace("(Recycle)", ""); } #endif } return(resObj); } return(null); }
/// <summary> /// 同步加载 /// </summary> /// <param name="path"></param> /// <param name="bClear"></param> /// <returns></returns> public GameObject InstantiateObject(string path, bool setSceneObj = false, bool bClear = true) { uint crc = CRC32.GetCrc32(path); ResouceObj resouceObj = GetObjectFromPool(crc); if (resouceObj == null) { resouceObj = m_ResourceObjClassPool.Spawn(true); resouceObj.m_Crc = crc; resouceObj.m_bClear = bClear; //ResouceManager提供加载方法 resouceObj = ResourceManager.Instance.LoadResource(path, resouceObj); if (resouceObj.m_ResItem.m_Obj != null) { resouceObj.m_CloneObj = GameObject.Instantiate(resouceObj.m_ResItem.m_Obj) as GameObject; } } if (setSceneObj) { resouceObj.m_CloneObj.transform.SetParent(SceneTrs, false); } int tempID = resouceObj.m_CloneObj.GetInstanceID(); if (!m_ResouceObjDic.ContainsKey(tempID)) { m_ResouceObjDic.Add(tempID, resouceObj); } return(resouceObj.m_CloneObj); }
/// <summary> /// 针对ObjectManager的异步加载接口 /// </summary> /// <param name="path"></param> /// <param name="resObj"></param> /// <param name="dealfinish"></param> /// <param name="priority"></param> public void AsyncLoadResource(string path, ResouceObj resObj, OnAsyncFinsih dealfinish, LoadResPriority priority) { ResouceItem item = GetCacheResouceItem(resObj.m_Crc); if (item != null) { resObj.m_ResItem = item; if (dealfinish != null) { dealfinish(path, resObj); } return; } //判断是否在加载中 AsyncLoadResParam para = null; if (!m_LoadingAssetDic.TryGetValue(resObj.m_Crc, out para) || para == null) { para = m_AsyncLoadResParamPool.Spawn(true); para.m_Crc = resObj.m_Crc; para.m_Path = path; para.m_Priority = priority; m_LoadingAssetDic.Add(resObj.m_Crc, para); m_LoadingAssetList[(int)priority].Add(para); } //往回调列表里面加回调 AsyncCallBack callBack = m_AsyncCallBackPool.Spawn(true); callBack.m_DealFinish = dealfinish; callBack.m_ResObj = resObj; para.m_CallBackList.Add(callBack); }
/// <summary> /// 取消异步加载资源 /// </summary> /// <returns></returns> public bool CancleLoad(ResouceObj res) { AsyncLoadResParam para = null; if (m_LoadingAssetDic.TryGetValue(res.m_Crc, out para) && m_LoadingAssetList[(int)para.m_Priority].Contains(para)) { for (int i = para.m_CallBackList.Count; i >= 0; i--) { AsyncCallBack tempCallBack = para.m_CallBackList[i]; if (tempCallBack != null && res == tempCallBack.m_ResObj) { tempCallBack.Reset(); m_AsyncCallBackPool.Recycle(tempCallBack); para.m_CallBackList.Remove(tempCallBack); } } if (para.m_CallBackList.Count <= 0) { para.Reset(); m_LoadingAssetList[(int)para.m_Priority].Remove(para); m_AsyncLoadResParamPool.Recycle(para); m_LoadingAssetDic.Remove(res.m_Crc); return(true); } } return(false); }
public void Reset() { m_DealObjFinish = null; m_DealFinish = null; m_Param1 = null; m_Param2 = null; m_Param3 = null; m_ResObj = null; }
/// <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> /// 根据ResouceObj卸载资源 /// </summary> /// <param name="resObj"></param> /// <param name="destoryObj"></param> /// <returns></returns> public bool ReleaseResource(ResouceObj resObj, bool destoryObj = false) { if (resObj == null) { return(false); } ResouceItem item = null; if (!AssetDic.TryGetValue(resObj.m_Crc, out item) || null == item) { DebugPlus.LogError("AssetDic里不存在改资源:" + resObj.m_CloneObj.name + " 可能释放了多次"); } GameObject.Destroy(resObj.m_CloneObj); item.RefCount--; DestoryResouceItme(item, destoryObj); return(true); }
/// <summary> /// 资源加载完成回调 /// </summary> /// <param name="path">路径</param> /// <param name="resObj">中间类</param> /// <param name="param1">参数1</param> /// <param name="param2">参数2</param> /// <param name="param3">参数3</param> void OnLoadResouceObjFinish(string path, ResouceObj resObj, object param1 = null, object param2 = null, object param3 = null) { if (resObj == null) { return; } if (resObj.m_ResItem.m_Obj == null) { #if UNITY_EDITOR DebugPlus.LogError("异步资源加载的资源为空:" + path); #endif } else { resObj.m_CloneObj = GameObject.Instantiate(resObj.m_ResItem.m_Obj) as GameObject; } //加载完成就从正在加载的异步中移除 if (m_AsyncResObjs.ContainsKey(resObj.m_Guid)) { m_AsyncResObjs.Remove(resObj.m_Guid); } if (resObj.m_CloneObj != null && resObj.m_SetSceneParent) { resObj.m_CloneObj.transform.SetParent(SceneTrs, false); } if (resObj.m_DealFinish != null) { int tempID = resObj.m_CloneObj.GetInstanceID(); if (!m_ResouceObjDic.ContainsKey(tempID)) { m_ResouceObjDic.Add(tempID, resObj); } resObj.m_DealFinish(path, resObj.m_CloneObj, resObj.m_Param1, resObj.m_Param2, resObj.m_Param3); } }
/// <summary> /// 异步对象加载 /// </summary> /// <param name="path"></param> /// <param name="fealFinish"></param> /// <param name="priority"></param> /// <param name="setSceneObject"></param> /// <param name="param1"></param> /// <param name="param2"></param> /// <param name="param3"></param> /// <param name="bClear"></param> public long InstantiateObjectAsync(string path, OnAsyncObjFinish dealFinish, LoadResPriority priority, bool setSceneObject = false, object param1 = null, object param2 = null, object param3 = null, bool bClear = true) { if (string.IsNullOrEmpty(path)) { return(0); } uint crc = CRC32.GetCrc32(path); ResouceObj resObj = GetObjectFromPool(crc); if (resObj != null) { if (setSceneObject) { resObj.m_CloneObj.transform.SetParent(SceneTrs, false); } if (dealFinish != null) { dealFinish(path, resObj.m_CloneObj, param1, param2, param3); } return(resObj.m_Guid); } long guid = ResourceManager.Instance.CreatGuid(); resObj = m_ResourceObjClassPool.Spawn(true); resObj.m_Crc = crc; resObj.m_SetSceneParent = setSceneObject; resObj.m_bClear = bClear; resObj.m_DealFinish = dealFinish; resObj.m_Param1 = param1; resObj.m_Param2 = param2; resObj.m_Param3 = param3; //调用ResouceManager的异步加载接口 ResourceManager.Instance.AsyncLoadResource(path, resObj, OnLoadResouceObjFinish, priority); return(guid); }
/// <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)) { DebugPlus.Log(obj.name + "对象不是ObjectManager创建的!"); return; } if (resObj == null) { DebugPlus.LogError("缓存的ResouceObj为空!"); return; } if (resObj.m_Already) { DebugPlus.LogError("该对象已经放回对象池了,检测自己是否情况引用!"); return; } #if UNITY_EDITOR obj.name += "(Recycle)"; #endif List <ResouceObj> st = null; if (maxCacheCount == 0) { m_ResouceObjDic.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 <ResouceObj>(); m_ObjectPoolDic.Add(resObj.m_Crc, st); } if (resObj.m_CloneObj) { if (recycleParent) { resObj.m_CloneObj.transform.SetParent(RecyclePoolTrs, false); } 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.ReleaseResource(resObj, destoryCache); resObj.Reset(); m_ResourceObjClassPool.Recycle(resObj); } } }
/// <summary> /// 该对象是否是对象池创建的 /// </summary> /// <returns></returns> public bool IsObjectManagerCreat(GameObject obj) { ResouceObj resObj = m_ResouceObjDic[obj.GetInstanceID()]; return(resObj == null ? false : true); }
/// <summary> /// 异步加载 /// </summary> /// <returns></returns> IEnumerator AsyncLoadCor() { List <AsyncCallBack> callBackList = null; //上一次yield的视角 long lastYiledTime = System.DateTime.Now.Ticks; while (true) { bool haveYield = false; for (int i = 0; i < (int)LoadResPriority.RES_NUM; i++) { if (m_LoadingAssetList[(int)LoadResPriority.RES_HIGHT].Count > 0) { i = (int)LoadResPriority.RES_HIGHT; } else if (m_LoadingAssetList[(int)LoadResPriority.RES_MIDDLE].Count > 0) { i = (int)LoadResPriority.RES_MIDDLE; } List <AsyncLoadResParam> loadingList = m_LoadingAssetList[i]; if (loadingList.Count <= 0) { continue; } AsyncLoadResParam loadingItem = loadingList[0]; loadingList.RemoveAt(0); callBackList = loadingItem.m_CallBackList; Object obj = null; ResouceItem item = null; #if UNITY_EDITOR if (!m_LoadFormAssetBundle) { if (loadingItem.m_Sprite) { obj = LoadAssetByEditor <Sprite>(loadingItem.m_Path); } else { obj = LoadAssetByEditor <Object>(loadingItem.m_Path); } //模拟异步加载 yield return(new WaitForEndOfFrame()); item = AssetBundleManager.Instance.FindResourceItme(loadingItem.m_Crc); if (item == null) { item = new ResouceItem(); item.m_Crc = loadingItem.m_Crc; } } #endif if (obj == null) { item = AssetBundleManager.Instance.LoadResouceAssetBundle(loadingItem.m_Crc); if (item != null && item.m_AssetBundle != null) { AssetBundleRequest abRequest = null; if (loadingItem.m_Sprite) { abRequest = item.m_AssetBundle.LoadAssetAsync <Sprite>(item.m_AssetName); } else { abRequest = item.m_AssetBundle.LoadAssetAsync(item.m_AssetName); } yield return(abRequest); if (abRequest.isDone) { obj = abRequest.asset; } lastYiledTime = System.DateTime.Now.Ticks; } } CacheResource(loadingItem.m_Path, ref item, loadingItem.m_Crc, obj, callBackList.Count); for (int j = 0; j < callBackList.Count; j++) { AsyncCallBack callBack = callBackList[j]; if (callBack != null && callBack.m_DealFinish != null && callBack.m_ResObj != null) { ResouceObj tempResObj = callBack.m_ResObj; tempResObj.m_ResItem = item; callBack.m_DealFinish(loadingItem.m_Path, tempResObj, tempResObj.m_Param1, tempResObj.m_Param2, tempResObj.m_Param3); callBack.m_DealFinish = null; tempResObj = null; } if (callBack != null && callBack.m_DealObjFinish != null) { callBack.m_DealObjFinish(loadingItem.m_Path, obj, callBack.m_Param1, callBack.m_Param2, callBack.m_Param3); callBack.m_DealObjFinish = null; } callBack.Reset(); m_AsyncCallBackPool.Recycle(callBack); } obj = null; callBackList.Clear(); m_LoadingAssetDic.Remove(loadingItem.m_Crc); loadingItem.Reset(); m_AsyncLoadResParamPool.Recycle(loadingItem); if (System.DateTime.Now.Ticks - lastYiledTime > MAXLOADRESTIME) { yield return(null); lastYiledTime = System.DateTime.Now.Ticks; haveYield = true; } } if (!haveYield || System.DateTime.Now.Ticks - lastYiledTime > MAXLOADRESTIME) { lastYiledTime = System.DateTime.Now.Ticks; yield return(null); } } }
/// <summary> /// 同步加载资源,针对给ObjectManager的接口 /// </summary> /// <param name="path"></param> /// <param name="resObj"></param> /// <returns></returns> public ResouceObj LoadResource(string path, ResouceObj resObj) { if (resObj == null) { return(null); } uint crc = resObj.m_Crc == 0 ? CRC32.GetCrc32(path) : resObj.m_Crc; ResouceItem item = GetCacheResouceItem(crc); if (item != null) { resObj.m_ResItem = item; return(resObj); } Object obj = null; #if UNITY_EDITOR if (!m_LoadFormAssetBundle) { item = AssetBundleManager.Instance.FindResourceItme(crc); if (item != null && item.m_Obj != null) { obj = item.m_Obj as Object; } else { if (item == null) { item = new ResouceItem(); item.m_Crc = crc; } obj = LoadAssetByEditor <Object>(path); } } #endif if (obj == null) { item = AssetBundleManager.Instance.LoadResouceAssetBundle(crc); if (item != null && item.m_AssetBundle != null) { if (item.m_Obj != null) { obj = item.m_Obj as Object; } else { obj = item.m_AssetBundle.LoadAsset <Object>(item.m_AssetName); } } } CacheResource(path, ref item, crc, obj); resObj.m_ResItem = item; item.m_Clear = resObj.m_bClear; return(resObj); }
/// <summary> /// 根据ResouceObj减少引用计数 /// </summary> /// <param name="resObj"></param> /// <param name="count"></param> /// <returns></returns> public int DecreaseResoucerRef(ResouceObj resObj, int count = 1) { return(resObj != null?DecreaseResoucerRef(resObj.m_Crc, count) : 0); }