/// <summary> /// 异步加载资源统一回调 /// </summary> /// <param name="LoadResult"></param> /// <param name="resInfo"></param> public void AsyncResCallBack(bool LoadResult, ResInfo resInfo) { AsyncCount -= 1; if (!LoadResult) { ResManager.Instance.RemoveLoadFailRes(resInfo.mCRC); resInfo.Recycle2Cache(); mAsyncLoadingResDic[resInfo.ResPath].ForEach((resLoadInfo) => { resLoadInfo.mRealListener.InvokeGracefully(LoadResult, null); resLoadInfo.Recycle2Cache(); }); mAsyncLoadingResDic.Remove(resInfo.ResPath); return; } bool DestroyCache = true; //异步加载回调 mAsyncLoadingResDic[resInfo.ResPath].ForEach((resLoadInfo) => { //但凡有一个调用此资源的说不销毁,就不销毁 if (!resLoadInfo.DestroyCache) { DestroyCache = false; } resInfo.Retain(); resLoadInfo.mRealListener.InvokeGracefully(LoadResult, resInfo); resLoadInfo.Recycle2Cache(); }); mAsyncLoadingResDic.Remove(resInfo.ResPath); resInfo.DestroyCache = DestroyCache; resInfo.mGUID = resInfo.ResObject.GetInstanceID(); mResList.AddValue(resInfo.ResPath); }
/// <summary> /// 回收一个资源 /// </summary> /// <param name="item"></param> /// <param name="destroyCache"></param> protected void DestoryResouceItme(ResInfo item, bool destroyCache = false) { if (item == null) { AFLogger.e("DestoryResouceItme:要释放的资源为空"); return; } if (item.IsRecycled) { AFLogger.e("资源已经被回收,请检查代码是否回收了多次:" + item.ResPath); mResDictionary.Remove(item.mCRC); return; } if (item.RefCount <= 0) { AFLogger.e("资源引用计数<=0:" + item.ResPath); return; } //释放减少引用计数 item.Release(); //如果缓存下来,移到表头 if (!destroyCache) { m_NoRefrenceAssetMapList.InsertToHead(item); return; } //不缓存,要根据引用计数判断是否释放内存 if (item.ReleaseRes()) { AFLogger.i("释放资源成功:" + item.ResPath); //资源缓存 if (!mResDictionary.Remove(item.mCRC)) { return; } m_NoRefrenceAssetMapList.Remove(item); //释放assetbundle引用 ReleaseAssetBundle(item); item.Recycle2Cache(); //在编辑器中加载,需要如此才能清除内存,因此在程序退出时要调用此方法清除内存 #if UNITY_EDITOR Resources.UnloadUnusedAssets(); #endif } //else //{ // AF_Logger.Info("释放资源失败(引用计数不为0或者并没有加载完成):" + item.ResPath); //} }
public void AsyncObjCallBack(bool LoadResult, ResInfo resInfo) { if (!LoadResult) { mAsyncLoadingObjDic.Remove(resInfo.ResPath); ResManager.Instance.RemoveLoadFailRes(resInfo.mCRC); resInfo.Recycle2Cache(); mAsyncLoadingObjDic[resInfo.ResPath].ForEach((objLoadInfo) => { objLoadInfo.loadObjCall.InvokeGracefully(LoadResult, null); objLoadInfo.Recycle2Cache(); }); mAsyncLoadingObjDic.Remove(resInfo.ResPath); return; } bool DestroyCache = false; mAsyncLoadingObjDic[resInfo.ResPath].ForEach((objLoadInfo) => { ResObject resObject = ResObject.Allocate(objLoadInfo); resObject.mObj = resInfo.ResObject; resObject.ABName = resInfo.ABName; resObject.mCloneObj = GameObject.Instantiate(resObject.mObj, objLoadInfo.ObjParentTrans) as GameObject; mObjList.AddValue(resObject.mCloneObj.GetInstanceID()); objLoadInfo.loadObjCall.InvokeGracefully(LoadResult, resObject); ResManager.Instance.CacheResObj(resObject); if (objLoadInfo.mClear) { DestroyCache = true; } resObject.Retain(); resInfo.Retain(); objLoadInfo.Recycle2Cache(); }); resInfo.DestroyCache = DestroyCache; mAsyncLoadingObjDic.Remove(resInfo.ResPath); }
/// <summary> /// 同步加载资源 /// </summary> /// <param name="resLoadInfo"></param> /// <returns></returns> public Object LoadSync(ResLoadInfo resLoadInfo) { if (resLoadInfo.mResPath == "") { AFLogger.e("资源路径为空,无法进行加载"); return(null); } ResInfo resInfo = ResManager.Instance.GetRes(resLoadInfo); if (resInfo != null && resInfo.State == ResState.Ready && (resInfo.ResObject.IsNotNull() || resInfo.ResStr.IsNotNullAndEmpty())) { resInfo.Retain(); return(resInfo.ResObject); } else if (resInfo == null) { resInfo = ResFactory.Create(resLoadInfo); if (resInfo.LoadSync()) { resInfo.Retain(); Debug.Log("加载成功:" + resInfo.RefCount); ResManager.Instance.CacheResource(resLoadInfo.mResPath, resInfo); return(resInfo.ResObject); } else { //加载失败,释放 resInfo.Recycle2Cache(); } } else { AFLogger.e("同步遇到异步加载的资源 : " + resInfo.ResPath + ",请检查调用代码!"); } return(null); }