/// <summary> /// 取消异步加载资源 /// </summary> /// <returns></returns> public bool CancleLoad(TResouceObj res) { TAsyncLoadResParam para = null; if (mLoadingAssetDic.TryGetValue(res.Crc, out para) && mLoadingAssetList[(int)para.Priority].Contains(para)) { for (int i = para.CallbackList.Count; i > 0; i--) { TAsyncCallBack tempCallBack = para.CallbackList[i]; if (tempCallBack != null && res == tempCallBack.ResObj) { tempCallBack.Reset(); mAsyncCallBackPool.Recycle(tempCallBack); para.CallbackList.Remove(tempCallBack); } } if (para.CallbackList.Count <= 0) { para.Reset(); mLoadingAssetList[(int)para.Priority].Remove(para); mAsyncLoadResParamPool.Recycle(para); mLoadingAssetDic.Remove(res.Crc); return(true); } } return(false); }
/// <summary> /// 异步加载 /// </summary> /// <returns></returns> IEnumerator AsyncLoadCor() { List <TAsyncCallBack> callBackList = null; //上一次yild的时间 long lastYildTime = System.DateTime.Now.Ticks; while (true) { bool haveYield = false; for (int i = 0; i < (int)LoadResPriority.RES_NUM; i++) { if (mLoadingAssetList[(int)LoadResPriority.RES_HIGHT].Count > 0) { i = (int)LoadResPriority.RES_HIGHT; } else if (mLoadingAssetList[(int)LoadResPriority.RES_MIDDLE].Count > 0) { i = (int)LoadResPriority.RES_MIDDLE; } List <TAsyncLoadResParam> loadingList = mLoadingAssetList[i]; if (loadingList.Count <= 0) { continue; } TAsyncLoadResParam loadingItem = loadingList[0]; loadingList.RemoveAt(0); callBackList = loadingItem.CallbackList; Object obj = null; TResouceItem item = null; #if UNITY_EDITOR if (!AppConfig.UseAssetBundle) { if (loadingItem.IsSprite) { obj = LoadAssetByEditor <Sprite>(loadingItem.Path); } else { obj = LoadAssetByEditor <Object>(loadingItem.Path); } //模拟异步加载 yield return(new WaitForSeconds(0.5f)); item = AssetBundleManager.Instance.FindResouceItem(loadingItem.Crc); if (item == null) { item = new TResouceItem(); item.Crc = loadingItem.Crc; } } #endif if (obj == null) { item = AssetBundleManager.Instance.LoadResouceAssetBundle(loadingItem.Crc); if (item != null && item.AssetBundle != null) { AssetBundleRequest abRequest = null; if (loadingItem.IsSprite) { abRequest = item.AssetBundle.LoadAssetAsync <Sprite>(item.AssetName); } else { abRequest = item.AssetBundle.LoadAssetAsync(item.AssetName); } yield return(abRequest); if (abRequest.isDone) { obj = abRequest.asset; } lastYildTime = System.DateTime.Now.Ticks; } } CacheResouce(loadingItem.Path, ref item, loadingItem.Crc, obj, callBackList.Count); for (int j = 0; j < callBackList.Count; j++) { TAsyncCallBack callBack = callBackList[j]; if (callBack != null && callBack.DealFinish != null && callBack.ResObj != null) { TResouceObj tempResObj = callBack.ResObj; tempResObj.ResItem = item; callBack.DealFinish(loadingItem.Path, tempResObj, tempResObj.Param1, tempResObj.Param2, tempResObj.Param3); callBack.DealFinish = null; tempResObj = null; } if (callBack != null && callBack.DealObjFinish != null) { callBack.DealObjFinish(loadingItem.Path, obj, callBack.Param1, callBack.Param2, callBack.Param3); callBack.DealObjFinish = null; } callBack.Reset(); mAsyncCallBackPool.Recycle(callBack); } obj = null; callBackList.Clear(); mLoadingAssetDic.Remove(loadingItem.Crc); loadingItem.Reset(); mAsyncLoadResParamPool.Recycle(loadingItem); if (System.DateTime.Now.Ticks - lastYildTime > MAXLOADRESTIME) { yield return(null); lastYildTime = System.DateTime.Now.Ticks; haveYield = true; } } if (!haveYield || System.DateTime.Now.Ticks - lastYildTime > MAXLOADRESTIME) { lastYildTime = System.DateTime.Now.Ticks; yield return(null); } yield return(null); } }