/// <summary> /// 取消异步加载资源 /// </summary> /// <returns>true为完全取消了,false表示可能还有其他相同操作的异步在进行,并没有取消</returns> public bool CancleLoad(ResourceObj resObj) { AsyncLoadResParam para = null; if (m_LoadingAssetDic.TryGetValue(resObj.m_Crc, out para) && m_LoadingAssetList[(int)para.m_Priority].Contains(para)) { for (int i = para.m_CallBackList.Count - 1; i >= 0; i--) { AsyncCallBack tempCallBack = para.m_CallBackList[i]; if (tempCallBack != null && resObj == tempCallBack.m_ResObj) { tempCallBack.Reset(); m_AsyncCallBackPool.Recycle(tempCallBack); para.m_CallBackList.Remove(tempCallBack); } } if (para.m_CallBackList.Count <= 0) { m_LoadingAssetList[(int)para.m_Priority].Remove(para); para.Reset(); m_AsyncLoadResParamPool.Recycle(para); m_LoadingAssetDic.Remove(resObj.m_Crc); return(true); } } return(false); }
public bool CancelAsyncLoad(ResourceObject resObj) { AsyncLoadResParam asyncParam = null; //首先获取是否存在该异步加载资源 if (m_LoadingAssetDict.TryGetValue(resObj.m_Crc, out asyncParam) && m_LoadAsyncResList[(int)resObj.m_LoadPriority].Contains(asyncParam)) { for (int i = asyncParam.callBackList.Count - 1; i >= 0; i--) { AsyncCallBack tempCallBack = asyncParam.callBackList[i]; //这个判断是,当有多个物体A,B都需要加载同样的资源时,如果A取消了,但是B不能取消 if (tempCallBack != null && tempCallBack.resObj == resObj) { tempCallBack.Reset(); m_AsyncCallBackPool.UnSpawn(tempCallBack); asyncParam.callBackList.RemoveAt(i); } } //在上层遍历完,如果异步回调列表空了,则代表不再需要异步加载该资源了,因此直接清空回收 if (asyncParam.callBackList.Count <= 0) { asyncParam.Reset(); m_LoadAsyncResList[(int)resObj.m_LoadPriority].Remove(asyncParam); m_LoadingAssetDict.Remove(resObj.m_Crc); m_AsyncLoadResPool.UnSpawn(asyncParam); return(true); } } return(false); }
/// <summary> /// 异步加载 /// ?一直在运行 /// </summary> /// <returns></returns> IEnumerator AsyncLoadCor() { long lastYiledTime = System.DateTime.Now.Ticks; List <AsyncCallBack> callBacks = null; bool hadYiled = false; while (true) { //上一次yield的时间 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); callBacks = 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.LoadResourceAssetBundle(loadingItem.m_Crc); if (item != null && item.m_AssetBundle) { 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, callBacks.Count); for (int j = 0; j < callBacks.Count; j++) { AsyncCallBack callBack = callBacks[j]; if (callBack != null && callBack.m_DealFinish != null && callBack.m_ResObj != null) { ResourceObj 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; callBacks.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; hadYiled = true; } } if (hadYiled || System.DateTime.Now.Ticks - lastYiledTime > MAXLOADRESTIME) { lastYiledTime = System.DateTime.Now.Ticks; yield return(null); } } }
/// <summary> /// 异步加载 /// </summary> IEnumerator AsyncLoader() { 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++) { List <AsyncLoadResParam> loadingList = _loadingAssetList[i]; if (loadingList.Count <= 0) { continue; } AsyncLoadResParam loadingItem = loadingList[0]; loadingList.RemoveAt(0); callBackList = loadingItem.callBackList; Object obj = null; ResourceItem item = null; #if UNITY_EDITOR if (!_loadFromAssetBundle) { obj = LoadAssetByEditor <Object>(loadingItem.path); //模拟异步加载 yield return(new WaitForSeconds(0.5f)); item = ABMgr.Ins.FingResourceItem(loadingItem.crc); } #endif if (obj == null) { item = ABMgr.Ins.LoadResAssetBundle(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; } lastYiledTime = System.DateTime.Now.Ticks; } } CacheResource(loadingItem.path, ref item, loadingItem.crc, obj, callBackList.Count); for (int j = 0; j < callBackList.Count; j++) { AsyncCallBack callBack = callBackList[i]; if (callBack != null && callBack.dealFinish != null) { callBack.dealFinish(loadingItem.path, obj, callBack.param1, callBack.param2, callBack.param3); callBack.dealFinish = null; } callBack.Reset(); _asyncCallBackPool.Recycle(callBack); } obj = null; callBackList.Clear(); _loadingAssetDic.Remove(loadingItem.crc); loadingItem.Reset(); _asyncLoadResParamPool.Recycle(loadingItem); if (System.DateTime.Now.Ticks - lastYiledTime > MAX_LOAD_RES_TIME) { yield return(null); lastYiledTime = System.DateTime.Now.Ticks; haveYield = true; } } if (!haveYield || System.DateTime.Now.Ticks - lastYiledTime > MAX_LOAD_RES_TIME) { lastYiledTime = System.DateTime.Now.Ticks; yield return(null); } } }