/// <summary> /// 取消异步加载资源 /// </summary> /// <returns></returns> public bool CancleLoad(ResourceObj 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); }
/// <summary> /// 异步加载 /// </summary> /// <returns></returns> IEnumerator AsyncLoadCor() { List <AsyncCallBack> callBackList = null; //上一次yield的时间 long lastYieldTime = System.DateTime.Now.Ticks; while (true) { bool haveYield = false; for (int i = 0; i < (int)LoadResPriority.RES_NUM; i++) { 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; ResourceItem item = null; #if UNITY_EDITOR if (!m_LoadFromAssetBundle) { obj = LoadAssetByEditor <Object>(loadingItem.m_Path); //模拟异步加载 yield return(new WaitForSeconds(0.5f)); item = AssetBundleManager.Instance.FindResourceItem(loadingItem.m_Crc); } #endif if (obj == null) { item = AssetBundleManager.Instance.LoatResourceAssetBundle(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; } lastYieldTime = 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_DealFinish(loadingItem.m_Path, obj, callBack.m_Param1, callBack.m_Param2, callBack.m_Param3); callBack.m_DealFinish = 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 - lastYieldTime > MAXLOADRESTIME) { haveYield = true; lastYieldTime = System.DateTime.Now.Ticks; yield return(null); } } if (!haveYield || System.DateTime.Now.Ticks - lastYieldTime > MAXLOADRESTIME) { lastYieldTime = System.DateTime.Now.Ticks; yield return(null); } } }