IEnumerator Cor_LoadBundleAsync(string bundleName, OnBundleLoadedAction action) { bundleName = bundleName.ToLower(); yield return(Cor_LoadDependenciesAsync(bundleName)); AssetBundle bundle; if (!bundleDict.TryGetValue(bundleName, out bundle)) // 是否已加载 { AssetBundleCreateRequest abcr; if (!abcrDict.TryGetValue(bundleName, out abcr)) // 是否正在加载 { LogFormat("Load bundle from file async: {0}", bundleName); abcr = AssetBundle.LoadFromFileAsync(fileList[bundleName].filePath); abcrDict[bundleName] = abcr; } while (!abcr.isDone) { yield return(null); } abcrDict.Remove(bundleName); // 不管加载是否成功都需要移除 if (abcr.isDone && abcr.assetBundle != null) { bundle = abcr.assetBundle; bundleDict[bundleName] = bundle; GetAssetPathFromBundle(bundle); } } if (action != null) { action(bundle); } }
IEnumerator Cor_LoadBundleAsync(string bundleName, OnBundleLoadedAction action) { bundleName = bundleName.ToLower(); if (!bundleName.EndsWith(bundleExtension)) { bundleName += bundleExtension; } yield return(null); yield return(Cor_LoadDependenciesAsync(bundleName)); if (!bundleDict.ContainsKey(bundleName)) { Log("Load bundle from file async: " + bundleName); string bundlePath = bundleDirPath + bundleName; AssetBundleCreateRequest abCR = AssetBundle.LoadFromFileAsync(bundlePath); yield return(abCR); if (abCR.isDone && abCR.assetBundle != null) { bundleDict.Add(bundleName, abCR.assetBundle); GetAssetPathFromBundle(abCR.assetBundle); } } if (action != null) { action(bundleDict[bundleName]); } }
// 异步加载AssetBundle public void LoadBundleAsync(string bundleName, OnBundleLoadedAction action = null) { StartCoroutine(Cor_LoadBundleAsync(bundleName, action)); }