/// <summary> /// 卸载/AssetBundle /// </summary> /// <param name="assetLoadPath">api传入的加载路径,Runtime下的相对路径</param> /// <param name="isForceUnload">强制卸载</param> /// <param name="type">为空则卸载所有同名资源,为空则卸载指定类型资源</param> public void UnloadAsset(string assetLoadPath, bool isForceUnload = false, Type type = null) { //1.AB卸载 var(assetBundleItem, dependAssetList) = AssetConfigLoder.GetDependAssets(assetLoadPath, type); //添加主资源一起卸载 dependAssetList.Add(assetBundleItem); //卸载 for (int i = 0; i < dependAssetList.Count; i++) { var assetbundleFileName = dependAssetList[i].AssetBundlePath; if (AssetbundleCacheMap.TryGetValue(assetbundleFileName, out var abw)) { // abw.Unuse(); //判断是否需要卸载 if (isForceUnload || abw.UseCounter == 0) { //卸载回调 Action onUnloadEnd = () => { //移除assetbundle AssetbundleCacheMap.Remove(assetbundleFileName); //移除缓存 this.UnloadObjectCache(type, assetLoadPath); }; //创建unload任务 var unloadTask = new UnLoadTask(abw, onUnloadEnd); //添加卸载任务 this.AddUnloadTask(assetbundleFileName, unloadTask); } } } }
/// <summary> /// 执行卸载任务 /// </summary> public void AddUnloadTask(string assetbundleFileName, UnLoadTask unloadTask) { UnloadTaskQueue.Enqueue(new KeyValuePair <string, UnLoadTask>(assetbundleFileName, unloadTask)); }