/// <summary> /// 异步加载一个bundle /// </summary> /// <param name="bundleName">bundle名</param> public static void LoadBundleAsync(string bundleName, BundleLoadCallBack callBack) { ResourcesConfig configTmp = ResourcesConfigManager.GetBundleConfig(bundleName); if (configTmp == null) { Debug.LogError("LoadBundleAsync: " + bundleName + " dont exist!"); return; } string path = GetBundlePath(configTmp); LoadState state = new LoadState(); Dictionary <string, LoadState> loadStateDict = new Dictionary <string, LoadState>(); //先加载依赖包 for (int i = 0; i < configTmp.relyPackages.Length; i++) { LoadRelyBundleAsync(configTmp.relyPackages[i], (LoadState relyLoadState, RelyBundle RelyBundle) => { if (RelyBundle != null && relyLoadState.isDone) { Debug.Log(RelyBundle.bundle.name); loadStateDict.Add(RelyBundle.bundle.name, relyLoadState); state.progress += 1 / ((float)configTmp.relyPackages.Length + 1); } //所有依赖包加载完毕加载资源包 if (loadStateDict.Keys.Count == configTmp.relyPackages.Length) { ResourceIOTool.AssetsBundleLoadAsync(path, (LoadState bundleLoadState, AssetBundle bundle) => { if (bundleLoadState.isDone) { callBack(LoadState.CompleteState, AddBundle(bundleName, bundle)); } else { state.progress += bundleLoadState.progress / ((float)configTmp.relyPackages.Length + 1); callBack(state, null); } }); } else { callBack(state, null); } }); } }
/// <summary> /// 异步加载一个bundle /// </summary> /// <param name="bundleName">bundle名</param> public static void LoadBundleAsync(string bundleName, BundleLoadCallBack callBack) { string[] AllDependencies = AssetsManifestManager.GetAllDependencies(bundleName); string path = GetBundlePath(bundleName); LoadState state = new LoadState(); int LoadCount = 0; if (AllDependencies.Length > 0) { //先加载依赖包 for (int i = 0; i < AllDependencies.Length; i++) { if (AllDependencies[i] != "") { LoadRelyBundleAsync(AllDependencies[i], (LoadState relyLoadState, Bundle RelyBundle) => { if (RelyBundle != null && relyLoadState.isDone) { LoadCount++; state.progress += 1 / ((float)AllDependencies.Length + 1); } //所有依赖包加载完毕加载资源包 if (LoadCount == AllDependencies.Length) { ResourceIOTool.AssetsBundleLoadAsync(path, (LoadState bundleLoadState, AssetBundle bundle) => { if (bundleLoadState.isDone) { callBack(LoadState.CompleteState, AddBundle(bundleName, bundle)); } else { state.progress += bundleLoadState.progress / ((float)AllDependencies.Length + 1); callBack(state, null); } }); } else { callBack(state, null); } }); } } } else { ResourceIOTool.AssetsBundleLoadAsync(path, (LoadState bundleLoadState, AssetBundle bundle) => { if (bundleLoadState.isDone) { callBack(LoadState.CompleteState, AddBundle(bundleName, bundle)); } else { state.progress += bundleLoadState.progress / ((float)AllDependencies.Length + 1); callBack(state, null); } }); } }