//取消异步加载 public bool CancelAsyncLoad(GameObjectItem item) { AsyncLoadAssetParam param = null; if (m_loadingAssetDict.TryGetValue(item.Crc, out param) && param != null && m_loadingAssetList[(int)param.m_priority].Contains(param)) { for (int i = param.m_callbackList.Count - 1; i >= 0; --i) { AsyncCallBack callback = param.m_callbackList[i]; if (callback != null && item == callback.m_gameObjectItem) { callback.Reset(); m_asyncCallBackPool.Recycle(callback); param.m_callbackList.Remove(callback); } } if (param.m_callbackList.Count <= 0) { param.Reset(); m_loadingAssetList[(int)param.m_priority].Remove(param); m_asyncLoadAssetParamPool.Recycle(param); m_loadingAssetDict.Remove(item.Crc); return(true); } } return(false); }
//异步加载资源协程 IEnumerator AsyncLoadAssetCoroutine() { List <AsyncCallBack> callbackList = null; long lastYieldTime = System.DateTime.Now.Ticks; while (true) { bool haveYield = false; for (int i = 0; i < (int)LoadAssetPriority.NUM; i++) { List <AsyncLoadAssetParam> loadingList = m_loadingAssetList[i]; if (loadingList.Count <= 0) { continue; } //取出第一个要加载的资源参数 AsyncLoadAssetParam param = loadingList[0]; loadingList.RemoveAt(0); callbackList = param.m_callbackList; Object obj = null; ResourceItem item = null; #if UNITY_EDITOR if (!m_loadFromAssetBundle) { obj = LoadAssetByEditor <Object>(param.m_path); //模拟异步加载 yield return(new WaitForSeconds(0.5f)); item = AssetBundleManager.Instance.FindResourceItem(param.m_crc); } #endif if (obj == null) { item = AssetBundleManager.Instance.LoadResourceItem(param.m_crc); if (item != null && item.Ab != null) { AssetBundleRequest request = null; if (param.m_Sprite) //图片资源需要特殊处理,因为object不能转Sprite { request = item.Ab.LoadAssetAsync <Sprite>(item.AssetName); } else { request = item.Ab.LoadAssetAsync <Object>(item.AssetName); } yield return(request); if (request.isDone) { obj = request.asset; } lastYieldTime = System.DateTime.Now.Ticks; } } CacheAsset2AssetCache(param.m_path, ref item, param.m_crc, obj, callbackList.Count); for (int j = 0; j < callbackList.Count; j++) { AsyncCallBack callback = callbackList[j]; if (callback != null && callback.m_gameObjDealFinished != null) { GameObjectItem gameObjectItem = callback.m_gameObjectItem; gameObjectItem.ResourceItem = item; callback.m_gameObjDealFinished(param.m_path, gameObjectItem, gameObjectItem.Param1, gameObjectItem.Param2, gameObjectItem.Param3); callback.m_gameObjDealFinished = null; } if (callback != null && callback.m_resourceObjDealFinished != null) { callback.m_resourceObjDealFinished(param.m_path, obj, callback.m_param1, callback.m_param2, callback.m_param3); callback.m_resourceObjDealFinished = null; } callback.Reset(); m_asyncCallBackPool.Recycle(callback); } obj = null; callbackList.Clear(); m_loadingAssetDict.Remove(param.m_crc); param.Reset(); m_asyncLoadAssetParamPool.Recycle(param); if (System.DateTime.Now.Ticks - lastYieldTime > MAXLOADASSETTIME) { yield return(null); lastYieldTime = System.DateTime.Now.Ticks; haveYield = true; } } //下边的yield return null, 作用于没有异步加载资源请求时 if (!haveYield || System.DateTime.Now.Ticks - lastYieldTime > MAXLOADASSETTIME) { lastYieldTime = System.DateTime.Now.Ticks; yield return(null); } } }