/// <summary> /// 针对ObjectPoolMgr异步加载接口 /// </summary> public void AsyncLoadResource(string path, ResourceObj resObj, OnAsyncFinish dealFinish, LoadResPriority priority) { ResourceItem item = GetCacheResourceItem(resObj.m_Crc); if (item != null) { resObj.m_ResItem = item; if (dealFinish != null) { dealFinish(path, resObj); } return; } //判断是否在加载中 AsynvLoadResParam para = null; if (!m_LodingAssetDic.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_LodingAssetDic.Add(resObj.m_Crc, para); m_LoadingAssetList[(int)priority].Add(para); } //往回调列表里加回调 //这里加的是针对的回调 ResMgrAsyncCallBack callBack = m_AsyncCallBackPool.Spawn(true); callBack.m_ObjDealFinish = dealFinish; callBack.m_ResObj = resObj; para.m_CallBackList.Add(callBack); }
/// <summary> /// 取消异步加载资源 /// </summary> public bool CancleLoad(ResourceObj resObj) { AsynvLoadResParam para = null; if (m_LodingAssetDic.TryGetValue(resObj.m_Crc, out para) && m_LoadingAssetList[(int)para.m_Priority].Contains(para)) { //清除异步加载中间类中记录的异步回调list,将回调类先重置,后回收,然后移除 for (int i = para.m_CallBackList.Count; i >= 0; i--) { ResMgrAsyncCallBack tempCallBack = para.m_CallBackList[i]; if (tempCallBack != null && resObj == tempCallBack.m_ResObj) { tempCallBack.Reset(); m_AsyncCallBackPool.Recycle(tempCallBack); para.m_CallBackList.Remove(tempCallBack); } } //进行完上一步后,继续重置异步加载中间类,先重置,然后将分级列表中,dic中移除,回收异步加载中间类 if (para.m_CallBackList.Count <= 0) { para.Reset(); m_LoadingAssetList[(int)para.m_Priority].Remove(para); m_AsyncLoadResParamPool.Recycle(para); m_LodingAssetDic.Remove(resObj.m_Crc); return(true); } } return(false); }
/// <summary> /// 异步加载资源(仅仅是不需要实例化的资源,例如texture,音频等等) /// </summary> public void AsyncLoadResource(string path, OnAsyncObjFinish dealFinish, LoadResPriority priority, bool isSprite = false, object param1 = null, object param2 = null, object param3 = null, uint crc = 0) { if (crc == 0) { crc = CRC32.GetCRC32(path); } ResourceItem item = GetCacheResourceItem(crc); if (item != null) { if (dealFinish != null) { dealFinish(path, item.m_Obj, param1, param2, param3); } return; } //判断是否在加载中 AsynvLoadResParam para = null; if (!m_LodingAssetDic.TryGetValue(crc, out para) || para == null) { para = m_AsyncLoadResParamPool.Spawn(true); para.m_Crc = crc; para.m_Path = path; para.m_SpriteBoo = isSprite; para.m_Priority = priority; m_LodingAssetDic.Add(crc, para); m_LoadingAssetList[(int)priority].Add(para); } //往回调列表里加回调 //这里加的是不针对的回调 ResMgrAsyncCallBack callBack = m_AsyncCallBackPool.Spawn(true); callBack.m_ResDealFinish = dealFinish; callBack.m_Param1 = param1; callBack.m_Param2 = param2; callBack.m_Param3 = param3; para.m_CallBackList.Add(callBack); }
/// <summary> /// 异步加载 /// </summary> IEnumerator AsyncLoadCor() { List <ResMgrAsyncCallBack> callBackList = null; //上一次yield的时间 long lastYieldTime = System.DateTime.Now.Ticks; while (true) { bool haveYieldBoo = 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 <AsynvLoadResParam> loadingList = m_LoadingAssetList[i]; if (loadingList.Count <= 0) { continue; } AsynvLoadResParam loadingItem = loadingList[0]; loadingList.RemoveAt(0); callBackList = loadingItem.m_CallBackList; Object obj = null; ResourceItem item = null; #if UNITY_EDITOR if (AppConst.DebugMode) { if (loadingItem.m_SpriteBoo) { obj = LoadAssetByEditor <Sprite>(loadingItem.m_Path); } else { obj = LoadAssetByEditor <Object>(loadingItem.m_Path); } //模拟异步加载 yield return(new WaitForSeconds(0.5f)); item = AssetBundleMgr.Instance.FindResourceItem(loadingItem.m_Crc); if (item == null) { item = new ResourceItem(); item.m_Crc = loadingItem.m_Crc; } } #endif if (obj == null) { item = AssetBundleMgr.Instance.LoadResourceAssetBundle(loadingItem.m_Crc); if (item != null && item.m_AssetBundel != null) { AssetBundleRequest abRequest = null; if (loadingItem.m_SpriteBoo) { abRequest = item.m_AssetBundel.LoadAssetAsync <Sprite>(item.m_AssetName); } else { abRequest = item.m_AssetBundel.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++) { ResMgrAsyncCallBack callBack = callBackList[j]; if (callBack != null && callBack.m_ObjDealFinish != null && callBack.m_ResObj != null) { ResourceObj tempResObj = callBack.m_ResObj; tempResObj.m_ResItem = item; callBack.m_ObjDealFinish(loadingItem.m_Path, tempResObj, tempResObj.m_Param1, tempResObj.m_Param2, tempResObj.m_Param3); callBack.m_ObjDealFinish = null; tempResObj = null; } if (callBack != null && callBack.m_ResDealFinish != null) { callBack.m_ResDealFinish(loadingItem.m_Path, obj, callBack.m_Param1, callBack.m_Param2, callBack.m_Param3); callBack.m_ResDealFinish = null; } callBack.Reset(); m_AsyncCallBackPool.Recycle(callBack); } obj = null; callBackList.Clear(); m_LodingAssetDic.Remove(loadingItem.m_Crc); loadingItem.Reset(); m_AsyncLoadResParamPool.Recycle(loadingItem); if (System.DateTime.Now.Ticks - lastYieldTime > MAXLOADRESTIME) { yield return(null); lastYieldTime = System.DateTime.Now.Ticks; haveYieldBoo = true; } } if (!haveYieldBoo || System.DateTime.Now.Ticks - lastYieldTime > MAXLOADRESTIME) { lastYieldTime = System.DateTime.Now.Ticks; yield return(null); } } }