/// <summary> /// 卸载ab包,顺带会把其所依赖的包也卸载掉 /// </summary> /// <param name="abName"></param> public void DisposeAssetBundle(string abName, BundleClassify classify = BundleClassify.Normal) { #if UNITY_EDITOR // 是否设置了使用assetbundle资源 if (AssetBundleFramework.DeveloperSetting.GetUseAssetBundleAsset()) { if (_DicAllClassify.ContainsKey(classify)) { _DicAllClassify[classify].DisposeAssetBundle(abName); } else { Debug.LogError(GetType() + "/DisposeAllAssets()/找不到分类包名称,无法释放资源,请检查! classify=" + classify); } } #else if (_DicAllClassify.ContainsKey(classify)) { _DicAllClassify[classify].DisposeAssetBundle(abName); } else { Debug.LogError(GetType() + "/DisposeAllAssets()/找不到分类包名称,无法释放资源,请检查! classify=" + classify); } #endif }
/// <summary> /// 释放资源。 /// </summary> /// <param name="scenesName">场景名称</param> public void DisposeAllAssets(BundleClassify classify) { if (_DicAllScenes.ContainsKey(classify)) { MultiABMgr multObj = _DicAllScenes[classify]; multObj.DisposeAllAsset(); } else { Debug.LogError(GetType() + "/DisposeAllAssets()/找不到场景名称,无法释放资源,请检查! scenesName=" + classify); } }
/// <summary> /// 释放分类包的所有资源。(慎用) /// </summary> /// <param name="classify">分类包名称</param> public void DisposeAllAssets(BundleClassify classify) { if (_DicAllClassify.ContainsKey(classify)) { MultiABMgr multObj = _DicAllClassify[classify]; multObj.DisposeAllAsset(); _DicAllClassify.Remove(classify); } else { Debug.LogError(GetType() + "/DisposeAllAssets()/找不到分类包名称,无法释放资源,请检查! classify=" + classify); } }
}//Method_end /// <summary> /// 加载(AB 包中)资源,AB包加载完成之后才能调用 /// </summary> /// <param name="scenesName">场景名称</param> /// <param name="abName">AssetBundle 包名称</param> /// <param name="assetName">资源名称</param> /// <param name="isCache">是否使用缓存</param> /// <returns></returns> private UnityEngine.Object LoadAsset(string abName, string assetName, bool isCache = true, BundleClassify classify = BundleClassify.Normal) { if (_DicAllClassify.ContainsKey(classify)) { MultiABMgr multObj = _DicAllClassify[classify]; return(multObj.LoadAsset(abName, assetName, isCache)); } Debug.LogError(GetType() + "/LoadAsset()/找不到分类包,无法加载(AB包中)资源,请检查! scenesName=" + classify); return(null); }
/// <summary> /// 下载AssetBundle 指定包 /// </summary> /// <param name="abName">AssetBundle 包名称</param> /// <param name="loadAllCompleteHandle">委托: 下载完成回调</param> /// <param name="classify">分类包(类包之间资源应当互不依赖)</param> /// <returns></returns> private IEnumerator LoadAssetBundlePack(string abName, Action <string> loadAllCompleteHandle, BundleClassify classify = BundleClassify.Normal) { //参数检查 if (string.IsNullOrEmpty(abName)) { Debug.LogError(GetType() + "/LoadAssetBundlePack()/abName is null ,请检查!"); yield return(null); } //等待Manifest清单文件加载完成 while (!ABManifestLoader.Instance.IsLoadFinish) { yield return(null); } _ManifestObj = ABManifestLoader.Instance.GetManifest(); if (_ManifestObj == null) { Debug.LogError(GetType() + "/LoadAssetBundlePack()/_ManifestObj is null ,请先确保加载Manifest清单文件!"); yield return(null); } //把当前AB分类包加入集合中。 if (!_DicAllClassify.ContainsKey(classify)) { MultiABMgr multiMgrObj = new MultiABMgr(abName); _DicAllClassify.Add(classify, multiMgrObj); } //调用下一层(“多包管理类”) MultiABMgr tmpMultiMgrObj = _DicAllClassify[classify]; if (tmpMultiMgrObj == null) { Debug.LogError(GetType() + "/LoadAssetBundlePack()/tmpMultiMgrObj is null ,请检查!"); } //调用“多包管理类”的加载指定AB包。 yield return(tmpMultiMgrObj.LoadAssetBundle(abName, loadAllCompleteHandle)); }//Method_end
/// <summary> /// 外部调用,以IEnumerator方式加载资源,加载完的资源用GetYieldBundleAsset()方法获取 /// </summary> /// <param name="abName">AB包名称</param> /// <param name="assetName">包内资源名称</param> /// <param name="isCache">是否缓存</param> /// <param name="classify">分类包</param> public IEnumerator LoadBundleAsset(string abName, string assetName, bool isCache = true, BundleClassify classify = BundleClassify.Normal) { bool loadDone = false; // 是否加载完成 float loadTime = 20f; // 最长加载时间,超过该加载时间还未完成则直接返回 Action <UnityEngine.Object> assetLoadComplete = delegate(UnityEngine.Object prefab) { _Ht.Add(abName + ":" + assetName, prefab); loadDone = true; }; LoadBundleAsset(abName, assetName, assetLoadComplete, isCache, classify); while (!loadDone && loadTime > 0) { yield return(null); loadTime -= Time.deltaTime; } }
/// <summary> /// 外部调用,加载资源用(重载函数) /// </summary> /// <param name="asset">资源数据</param> /// <param name="assetLoadComplete">回调函数</param> /// <param name="isCache">是否缓存</param> /// <param name="classify">分类包</param> public void LoadBundleAsset(ABAsset asset, Action <UnityEngine.Object> assetLoadComplete, bool isCache = true, BundleClassify classify = BundleClassify.Normal) { LoadBundleAsset(asset.ABPath, asset.AssetName, assetLoadComplete, isCache, classify); }
/// <summary> /// 外部调用,加载资源用 /// </summary> /// <param name="abName">AB包名称</param> /// <param name="assetName">包内资源名称</param> /// <param name="assetLoadComplete">回调函数</param> /// <param name="isCache">是否缓存</param> /// <param name="classify">分类包</param> public void LoadBundleAsset(string abName, string assetName, Action <UnityEngine.Object> assetLoadComplete, bool isCache = true, BundleClassify classify = BundleClassify.Normal) { Action <string> loadCompleteCallback = delegate(string assetBundleName) //AB包加载完成的回调 { LoadBundleAsset(abName, assetName, assetLoadComplete, isCache, classify); }; if (!_DicAllClassify.ContainsKey(classify)) //判断当前分类包是否已经创建 { StartCoroutine(LoadAssetBundlePack(abName, loadCompleteCallback, classify)); //创建分类包加载器并加载给定AB包 return; } MultiABMgr tmpMultiABMgr = _DicAllClassify[classify]; if (!tmpMultiABMgr.AssetBundleIsLoaded(abName)) //判断AB包是否已经加载 { if (tmpMultiABMgr.AssetBundleIsLoading(abName)) //判断AB包是否正在加载 { tmpMultiABMgr.AddLoadCallBack(abName, loadCompleteCallback); return; } StartCoroutine(tmpMultiABMgr.LoadAssetBundle(abName, loadCompleteCallback)); //加载AB包 return; } // 如果ab路径的扩展名是.u3dscene说明加载的是场景,则只加载ab包,不加载ab资源(场景(scene)不需要加载资源,只需要加载包) if (Path.GetExtension(abName) == ".u3dscene") { assetLoadComplete(null); //资源加载完成调用回调函数 } else { assetLoadComplete(LoadAsset(abName, assetName, isCache, classify)); //资源加载完成调用回调函数,参数为加载进来的资源(若AB包已经加载了,则这是一个同步加载) } }
/// <summary> /// 外部调用,加载资源用 /// </summary> /// <param name="abName">AB包名称</param> /// <param name="assetName">包内资源名称</param> /// <param name="assetLoadComplete">回调函数</param> /// <param name="isCache">是否缓存</param> /// <param name="classify">分类包</param> public void LoadBundleAsset(string abName, string assetName, Action <UnityEngine.Object> assetLoadComplete, bool isCache = true, BundleClassify classify = BundleClassify.Normal) { Action <string> loadCompleteCallback = delegate(string assetBundleName) //AB包加载完成的回调 { LoadBundleAsset(abName, assetName, assetLoadComplete, isCache, classify); }; if (!_DicAllScenes.ContainsKey(classify)) //判断当前分类包是否已经创建 { StartCoroutine(LoadAssetBundlePack(abName, loadCompleteCallback, classify)); //创建分类包加载器并加载给定AB包 return; } MultiABMgr tmpMultiABMgr = _DicAllScenes[classify]; if (!tmpMultiABMgr.AssetBundleIsLoaded(abName)) //判断AB包是否已经加载 { if (tmpMultiABMgr.AssetBundleIsLoading(abName)) //判断AB包是否正在加载 { tmpMultiABMgr.AddLoadCallBack(abName, loadCompleteCallback); return; } StartCoroutine(tmpMultiABMgr.LoadAssetBundle(abName, loadCompleteCallback)); //加载AB包 return; } assetLoadComplete(LoadAsset(abName, assetName, isCache, classify)); //资源加载完成调用回调函数,参数为加载进来的资源 }