/// <summary> /// 加载依赖项 /// </summary> /// <param name="assetBundleName">Asset bundle name.</param> private void LoadDependencies(string scope, string assetBundleName, string variants) { AssetBundleManifest manifest = this.GetPersistentManifest(); string variantsName = this.GetVariants(assetBundleName, variants); string[] deps = manifest.GetAllDependencies(variantsName); if (deps.Length == 0) { return; } if (variants != "") { for (int i = 0; i < deps.Length; i++) { string itemVariantsName = this.GetVariants(deps[i], variants); deps[i] = itemVariantsName; } } // Record and load all dependencies. dependencies.Add(variantsName, deps); for (int i = 0; i < deps.Length; i++) { // 这个abname已经是变体了 string itemVariantsName = deps[i]; // 加载依赖时,如果队列不足,要先等待 if (dicLoadings.Count >= this._maxSize) { // 需要等待 LoadTask task = new LoadTask(scope, itemVariantsName); //LoadTask task = new LoadTask(); //task.scope = scope; //task.name = itemVariantsName; downList.Add(task); } else { // 这个之后要获取 this.LoadAssetBundleInternal(itemVariantsName, ""); } } }
/// <summary> /// 添加加载任务 /// TODO 只能在外部调用时,自己初始化AssetBundleLoadAssetOperationBatch来控制,这里可以改掉 /// </summary> /// <param name="abName">Ab name.</param> /// <param name="callBack">Call back.</param> public void MultiLoadAssetBundle(string scopeName, string abName, Action <string> callBack, string variants = "") { if (SimulateAssetBundleInEditor == false) { LoadStreamManifest(); } #if UNITY_EDITOR if (SimulateAssetBundleInEditor) { string assetBundleName = this.GetVariants(abName, variants); string[] assetPaths = AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName); if (assetPaths.Length == 0) { Debug.LogError("There is no asset with assetBundleName \"" + assetBundleName); return; } Dictionary <string, UnityEngine.Object> dic = new Dictionary <string, UnityEngine.Object>(); for (int j = 0; j < assetPaths.Length; j++) { string name = assetPaths[j]; UnityEngine.Object target = AssetDatabase.LoadMainAssetAtPath(name); string tmp = Path.GetFileNameWithoutExtension(name); if (dic.ContainsKey(tmp)) { Debug.LogWarning("注意:存在重名资源 tmp = " + tmp + " name = " + name); continue; } dic.Add(tmp, target); } this.simulateAssetDic.Add(abName, dic); // @TODO: Now we only get the main object from the first asset. Should consider type also. // UnityEngine.Object target = AssetDatabase.LoadMainAssetAtPath(assetPaths[assetPaths.Length - 1]); // this.simulateAssetDic.Add(abName, target); // 存储到字典中 if (dictScope.ContainsKey(scopeName) == false) { dictScope.Add(scopeName, new List <string>()); } dictScope[scopeName].Add(assetBundleName); if (callBack != null) { callBack(assetBundleName); } } 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); } }; // 队列没有达上限,可以直接下载 if (this.dicLoadings.Count < this._maxSize) { this.LoadAssetAsync(scopeName, abName, variants, callBack, baseCallBack); } else { // 需要等待 LoadTask task = new LoadTask(scopeName, abName, variants, callBack, baseCallBack); //LoadTask task = new LoadTask(); //task.scope = scopeName; //task.name = abName; //task.variants = variants; //task.callBack = callBack; //task.baseCallBack = baseCallBack; downList.Add(task); } } }
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); } }