/// <summary> /// 加载场景 /// TODO 这里没有再进行加载队列个数的判定,所以可能会超过最大加载数的限制 /// </summary> /// <returns>The level async.</returns> /// <param name="assetBundleName">Asset bundle name.</param> /// <param name="levelName">Level name.</param> /// <param name="isAdditive">If set to <c>true</c> is additive.</param> public AssetBundleOperation LoadSceneAsync(string scopeName, string assetBundleName, string levelName, bool isAdditive, Action <string> callBack, string variants = "") { AssetBundleOperation operation = null; #if UNITY_EDITOR if (SimulateAssetBundleInEditor) { assetBundleName = this.GetVariants(assetBundleName, variants); // 存储到字典中 if (dictScope.ContainsKey(scopeName) == false) { dictScope.Add(scopeName, new List <string>()); } dictScope[scopeName].Add(assetBundleName); operation = new AssetBundleLoadSceneSimulationOperation(assetBundleName, levelName, isAdditive, callBack); if (callBack != null) { StartCoroutine(operation); } } else #endif { // 回调函数的基础 Action <string, string, Action <string> > baseCallBack = (string ScopeName, string AssetBundleName, Action <string> CallBack) => { // 存储到字典中 if (dictScope.ContainsKey(ScopeName) == false) { dictScope.Add(ScopeName, new List <string>()); } dictScope[ScopeName].Add(AssetBundleName); if (CallBack != null) { CallBack(AssetBundleName); } }; this.LoadAssetBundle(scopeName, assetBundleName, variants); string variantsName = this.GetVariants(assetBundleName, variants); operation = new AssetBundleLoadSceneOperation(scopeName, variantsName, levelName, isAdditive, callBack, baseCallBack); this.inProgressOperations.Add(operation); if (callBack != null) { StartCoroutine(operation); } } return(operation); }
void Update() { #if UNITY_EDITOR if (SimulateAssetBundleInEditor) { return; } #endif if (dicLoadings == null || dicLoadings.Count == 0) { return; } // Collect all the finished WWWs. List <string> keysToRemove = new List <string>(); foreach (var keyValue in this.dicLoadings) { AssetBundleCreateRequest request = keyValue.Value; // isDone也有可能在系统错误的时候返回 if (request.isDone) { AssetBundle bundle = request.assetBundle; if (bundle == null) { Debug.LogError(string.Format("Failed load bundle {0} : bundle is null", keyValue.Key)); keysToRemove.Add(keyValue.Key); if (this.abRefCntLoadingDic.ContainsKey(keyValue.Key)) { this.abRefCntLoadingDic[keyValue.Key]--; if (this.abRefCntLoadingDic[keyValue.Key] == 0) { this.abRefCntLoadingDic.Remove(keyValue.Key); } } continue; } int rCount = 1; if (this.abRefCntLoadingDic.ContainsKey(keyValue.Key)) { rCount = this.abRefCntLoadingDic[keyValue.Key]; this.abRefCntLoadingDic.Remove(keyValue.Key); } AssetBundleRef abr = new AssetBundleRef(keyValue.Key, bundle); abr.m_ReferencedCount = rCount; dictAssetBundleRefs.Add(keyValue.Key, abr); keysToRemove.Add(keyValue.Key); } } // Remove the finished WWWs. foreach (var key in keysToRemove) { AssetBundleCreateRequest request = this.dicLoadings[key]; this.dicLoadings.Remove(key); request = null; } // Update all in progress operations for (int i = 0; i < inProgressOperations.Count;) { AssetBundleOperation operation = inProgressOperations[i]; if (!operation.Update()) { inProgressOperations.RemoveAt(i); } else { i++; } } // 可以继续加载 while (this.downList.Count > 0 && (this.dicLoadings.Count <= this._maxSize)) { LoadTask task = this.downList[0]; this.LoadAssetAsync(task.scope, task.name, task.variants, task.callBack, task.baseCallBack); this.downList.RemoveAt(0); } }