/// <summary> /// 卸载 /// </summary> /// <param name="assetName">根据加载路径卸载</param> /// <param name="isForceUnload">强制卸载</param> public void UnloadAsset(string assetName, bool isForceUnload = false) { //非hash模式,需要debugRuntime // if (!this.AssetConfigLoder.IsHashName) // { // assetName = ZString.Format(DEBUG_RUNTIME, assetName); // } var(assetBundleItem, dependAssetList) = AssetConfigLoder.GetDependAssets(assetName); //添加主资源一起卸载 dependAssetList.Add(assetBundleItem); //卸载 for (int i = 0; i < dependAssetList.Count; i++) { var assetPath = dependAssetList[i].AssetBundlePath; AssetBundleWapper abw = null; if (AssetbundleMap.TryGetValue(assetPath, out abw)) { if (isForceUnload) { abw.UnLoad(); } else { abw.Unuse(); } } } }
/// <summary> /// load ALL TestAPI /// 这个有一定局限性,这里是返回某个Ab中的所有资源 /// 简单处理一些简单资源情况:目前只解决图集 /// 仅作为某些项目补坑用 /// </summary> /// <param name="path"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> /// <exception cref="NotImplementedException"></exception> public T[] LoadAll <T>(string path) where T : Object { //非hash模式,需要debugRuntime // if (!this.AssetConfigLoder.IsHashName) // { // path = ZString.Format(DEBUG_RUNTIME, path); // } var item = AssetConfigLoder.GetAssetBundleData <T>(path); //加载assetbundle AssetBundle ab = LoadAssetBundle(item.AssetBundlePath); if (ab != null) { var assetNames = ab.GetAllAssetNames(); string relname = ""; if (assetNames.Length == 1) { relname = assetNames[0]; } else { var f = path + "."; relname = assetNames.First((s) => s.Contains(f)); } return(ab.LoadAssetWithSubAssets <T>(relname)); } return(null); }
/// <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> /// <param name="type"></param> /// <param name="assetLoadPath">api传入的加载路径,Runtime下的相对路径</param> /// <returns></returns> public Object Load(Type type, string assetLoadPath) { var retObj = GetObjectFormCache(type, assetLoadPath); if (!retObj) { //1.依赖路径 var(mainAssetItem, dependAssetList) = AssetConfigLoder.GetDependAssets(assetLoadPath, type); //2.加载 if (mainAssetItem != null) { if (dependAssetList == null) { dependAssetList = new List <AssetBundleItem>(); } dependAssetList.Add(mainAssetItem); //加载所有ab foreach (var dependABItem in dependAssetList) { LoadAssetBundle(dependABItem.AssetBundlePath, dependABItem.Mix); } //加载实例 retObj = LoadObjectFormAssetBundle(type, assetLoadPath, mainAssetItem); } } return(retObj); }
/// <summary> /// 异步加载接口 /// 外部创建 需要自己管理yield,防止逻辑冲突 /// 开放该接口,主要用于各种批量测试控制逻辑,一般情况下无需调用 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="assetName"></param> /// <param name="callback"></param> /// <returns>异步任务id</returns> public AsyncLoadTaskGroupResult CreateAsyncLoadTask <T>(string assetName) where T : UnityEngine.Object { var assetBundleItem = AssetConfigLoder.GetAssetBundleData <T>(assetName); if (assetBundleItem != null) { //添加任务组 var taskGroup = new AsyncLoadTaskGroupResult(this, assetBundleItem); taskGroup.Id = this.taskIdxCounter++; // AddAsyncTaskGroup(taskGroup); return(taskGroup); } else { BDebug.LogError("不存在资源:" + assetName); } return(null); }
public Object Load(Type type, string path) { //1.依赖路径 var(assetBundleItem, dependAssetList) = AssetConfigLoder.GetDependAssets(path, type); if (assetBundleItem != null) { //加载依赖 foreach (var dependABItem in dependAssetList) { LoadAssetBundle(dependABItem.AssetBundlePath, dependABItem.Mix); } //加载主资源 LoadAssetBundle(assetBundleItem.AssetBundlePath, assetBundleItem.Mix); // return(LoadFormAssetBundle(type, path, assetBundleItem)); } return(null); }
/// <summary> /// 同步加载 /// </summary> /// <param name="path"></param> /// <param name="pathType"></param> /// <param name="fullPath"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public T Load <T>(string path, LoadPathType pathType = LoadPathType.RuntimePath) where T : UnityEngine.Object { //这里首次会耗时,主要是需要关联查询依赖文件 if (pathType == LoadPathType.GUID) { var abi = AssetConfigLoder.GetAssetBundleDataByGUID(path); if (abi != null) { path = abi.LoadPath; } } var obj = Load(typeof(T), path); if (obj) { return(obj as T); } return(null); }
/// <summary> /// 异步加载接口 /// 外部创建 需要自己管理yield,防止逻辑冲突 /// 开放该接口,主要用于各种批量测试控制逻辑,一般情况下无需调用 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="assetLoadPath">api传入的加载路径,Runtime下的相对路径</param> /// <param name="callback"></param> /// <returns>异步任务id</returns> private LoadTaskGroup CreateAsyncLoadTask <T>(string assetLoadPath) where T : UnityEngine.Object { var assetBundleItem = AssetConfigLoder.GetAssetBundleData <T>(assetLoadPath); if (assetBundleItem != null) { //取消卸载任务 this.CancelUnloadTask(assetBundleItem.AssetBundlePath); //创建任务组 var taskGroup = new LoadTaskGroup(this, typeof(T), assetLoadPath, assetBundleItem); taskGroup.Id = this.taskIdxCounter++; // AddAsyncTaskGroup(taskGroup); return(taskGroup); } else { BDebug.LogError("不存在资源:" + assetLoadPath); } return(null); }