/// <summary> /// 加载 AB 包 /// </summary> /// <param name="abName">AB包名称</param> /// <returns></returns> public IEnumerator LoadAssetBundle(string abName) { // AB包关系的建立 if (!_DicABRelation.ContainsKey(abName)) { ABRelation tmpABrelation = new ABRelation(abName); _DicABRelation.Add(abName, tmpABrelation); } ABRelation abRelation = _DicABRelation[abName]; // 得到指定AB包的依赖引用关系 string[] strDenendence = ABManifestLoader.GetInstance().RetrivalDependce(abName); // 相同的包名在添加 依赖和引用项时 内部会做一个判断:查看是否添加过,添加过就不再添加了 foreach (string item in strDenendence) { // 添加 “依赖项” abRelation.AddDenpendce(item); // 添加 “引用项” yield return(LoadReference(item, abName)); } //加载AB包 if (!_DicSingleABLoaderCache.ContainsKey(abName)) { _CurrentSingleABLoader = new SingleABLoader(abName, CompleteLoadAB); _DicSingleABLoaderCache.Add(abName, _CurrentSingleABLoader); yield return(_DicSingleABLoaderCache[abName].LoadAssetBundle()); } }
/// <summary> /// 释放本场景中所有的资源 /// 注意点:用于场景之间的切换 /// </summary> public void DisposeAllAsset() { try { if (_DicSingleABLoaderCache != null && _DicSingleABLoaderCache.Count > 0) { foreach (SingleABLoader singleAbLoader in _DicSingleABLoaderCache.Values) { singleAbLoader.DisposeAll(); } } } catch { Debug.Log(GetType() + "DisposeAllAsset/ 出错"); } finally { _DicSingleABLoaderCache.Clear(); _DicSingleABLoaderCache = null; // 释放其他的资源 _DicABRelation.Clear(); _DicABRelation = null; _CurrentSceneName = null; _CurrentABName = null; _loadAllABPackageCompleteHandle = null; _CurrentSingleABLoader = null; // 卸载没有用到的资源 Resources.UnloadUnusedAssets(); // 强制垃圾收集 System.GC.Collect(); } }