/// <summary> /// 异步加载AB /// </summary> /// <typeparam name="T"></typeparam> /// <param name="abName"></param> /// <param name="assetName"></param> /// <param name="cb"></param> /// <returns></returns> IEnumerator LoadAssetAsync <T>(string abName, string assetName, System.Action <T> cb) where T : Object { //AB是否已加载 if (!abLoadedDic.ContainsKey(abName)) //如果还没加载 //先加载依赖项 { foreach (string item in manifest.GetAllDependencies(abName)) { yield return(LoadAssetAsync <T>(item, null, null)); } //加载AB AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(PathHelper.AssetBundlePath + "/" + abName); while (!request.isDone) { yield return(new WaitForSeconds(0.15f)); } AssetBundleItem abItem = new AssetBundleItem(abName, request.assetBundle, manifest.GetAllDependencies(abName)); abLoadedDic.Add(abName, abItem); } else { //AB内存已释放 if (abLoadedDic[abName].V_AssetBundle == null) { //先加载依赖项 foreach (string item in manifest.GetAllDependencies(abName)) { yield return(LoadAssetAsync <T>(item, null, null)); } //加载AB AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(PathHelper.AssetBundlePath + "/" + abName); yield return(request); if (request.assetBundle != null) { abLoadedDic[abName].V_AssetBundle = request.assetBundle; } } } if (!string.IsNullOrEmpty(assetName) && cb != null) { StartCoroutine(abLoadedDic[abName].LoadAssetAsync <T>(assetName, cb)); } }
/// <summary> /// 加载AB /// </summary> /// <param name="abName"></param> /// <returns></returns> bool LoadAssetBundle(string abName) { //AB是否已加载 if (!abLoadedDic.ContainsKey(abName)) //如果还没加载 //先加载依赖项 { foreach (string item in manifest.GetAllDependencies(abName)) { if (!LoadAssetBundle(item)) { return(false); } } //加载AB AssetBundle ab = AssetBundle.LoadFromFile(PathHelper.AssetBundlePath + "/" + abName); AssetBundleItem abItem = new AssetBundleItem(abName, ab, manifest.GetAllDependencies(abName)); abLoadedDic.Add(abName, abItem); return(true); } else { //AB内存已释放 if (abLoadedDic[abName].V_AssetBundle == null) { //先加载依赖项 foreach (string item in manifest.GetAllDependencies(abName)) { if (!LoadAssetBundle(item)) { return(false); } } //加载AB AssetBundle ab = AssetBundle.LoadFromFile(PathHelper.AssetBundlePath + "/" + abName); abLoadedDic[abName].V_AssetBundle = ab; return(true); } else { //AB内存未释放 return(true); } } }