/// <summary> /// 异步加载接口 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="path"></param> /// <param name="callback"></param> /// <returns></returns> public int AsyncLoad <T>(string path, Action <T> callback) where T : UnityEngine.Object { Queue <LoaderTaskData> taskQueue = new Queue <LoaderTaskData>(); //获取依赖 path = GetExistPath(path); var res = manifest.Manifest.GetDirectDependencies(path); foreach (var r in res) { var task = new LoaderTaskData(r, typeof(Object)); taskQueue.Enqueue(task); } //添加任务组 LoaderTaskGroup taskGroup = new LoaderTaskGroup(5, taskQueue, AsyncLoadAssetBundle, (p, obj) => { callback(obj as T); }); taskGroup.Id = this.taskIDCounter++; AddTaskGroup(taskGroup); //开始任务 DoNextTask(); return(taskGroup.Id); }
/// <summary> /// 异步加载接口 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="path"></param> /// <param name="callback"></param> /// <returns></returns> public int AsyncLoad <T>(string path, Action <T> callback) where T : UnityEngine.Object { List <LoaderTaskData> taskQueue = new List <LoaderTaskData>(); //获取依赖 var dependAssets = config.Manifest.GetDirectDependenciesByName(path); if (dependAssets == null) { return(-1); } // foreach (var r in dependAssets) { var task = new LoaderTaskData(r, typeof(Object)); taskQueue.Add(task); } var mainAsset = dependAssets[dependAssets.Length - 1]; //添加任务组 LoaderTaskGroup taskGroup = new LoaderTaskGroup(mainAsset, 10, taskQueue, AsyncLoadAssetBundle, (p, obj) => { callback(obj as T); }); taskGroup.Id = this.taskIDCounter++; AddTaskGroup(taskGroup); //开始任务 DoNextTask(); return(taskGroup.Id); }
/// <summary> /// 异步加载 多个 /// </summary> /// <param name="assetsPath">资源</param> /// <param name="onLoadComplete">加载结束</param> /// <param name="onLoadProcess">进度</param> /// <returns>taskid</returns> public List <int> AsyncLoad(IList <string> assetsPath, Action <IDictionary <string, Object> > onLoadComplete, Action <int, int> onLoadProcess) { List <int> idList = new List <int>(); IDictionary <string, Object> retMap = new Dictionary <string, Object>(); assetsPath = assetsPath.Distinct().ToList(); //去重 int total = assetsPath.Count; //source int counter = 0; foreach (var asset in assetsPath) { var _asset = asset; List <LoaderTaskData> taskQueue = new List <LoaderTaskData>(); var dependAssets = config.Manifest.GetDirectDependenciesByName(asset); //获取依赖 if (dependAssets == null) { total--; continue; } foreach (var r in dependAssets) { var task = new LoaderTaskData(r, typeof(Object)); taskQueue.Add(task); } var mainAsset = dependAssets[dependAssets.Length - 1]; //添加任务组 //加载颗粒度10个 LoaderTaskGroup taskGroup = new LoaderTaskGroup(mainAsset, 10, taskQueue, AsyncLoadAssetBundle, (p, obj) => { counter++; //注意返回加载的id,不是具体地址的id retMap[_asset] = obj; if (onLoadProcess != null) { onLoadProcess(counter, total); } //完成 if (retMap.Count == total) { onLoadComplete(retMap); } }); taskGroup.Id = this.taskIDCounter++; AddTaskGroup(taskGroup); idList.Add(taskGroup.Id); } //开始任务 DoNextTask(); // return(idList); }
/// <summary> /// 异步加载 多个 /// </summary> /// <param name="assetsPath">资源</param> /// <param name="onLoadComplete">加载结束</param> /// <param name="onLoadProcess">进度</param> /// <returns>taskid</returns> public List <int> AsyncLoad(IList <string> assetsPath, Action <IDictionary <string, Object> > onLoadComplete, Action <int, int> onLoadProcess) { List <int> idList = new List <int>(); IDictionary <string, Object> retMap = new Dictionary <string, Object>(); assetsPath = assetsPath.Distinct().ToList(); //去重 int total = assetsPath.Count; //source int counter = 0; foreach (var asset in assetsPath) { var _asset = asset; Queue <LoaderTaskData> taskQueue = new Queue <LoaderTaskData>(); //获取依赖 var path = GetExistPath(_asset); if (string.IsNullOrEmpty(path)) { Debug.LogError("不存在资源:" + _asset); total--; continue; } var res = manifest.Manifest.GetDirectDependencies(path); foreach (var r in res) { var task = new LoaderTaskData(r, typeof(Object)); taskQueue.Enqueue(task); } //添加任务组 //加载颗粒度10个 LoaderTaskGroup taskGroup = new LoaderTaskGroup(10, taskQueue, AsyncLoadAssetBundle, (p, obj) => { counter++; //注意返回加载的id,不是具体地址的id retMap[_asset] = obj; if (onLoadProcess != null) { onLoadProcess(counter, total); } //完成 if (retMap.Count == total) { onLoadComplete(retMap); } }); taskGroup.Id = this.taskIDCounter++; AddTaskGroup(taskGroup); idList.Add(taskGroup.Id); } //开始任务 DoNextTask(); // return(idList); }
/// <summary> /// 加载 /// 一般来说,主资源才需要load /// 依赖资源只要加载ab,会自动依赖 /// </summary> /// <param name="assetPath"></param> /// <param name="isMainAsset">是否需要返回加载资源</param> /// <param name="callback"></param> /// <returns></returns> IEnumerator IE_AsyncLoadAssetbundle(LoaderTaskData task, Action <LoadAssetState, Object> callback) { //正在被加载中,放入后置队列 if (lockSet.Contains(task.AssetPath)) { callback(LoadAssetState.IsLoding, null); yield break; } //没被加载 if (!loder.AssetbundleMap.ContainsKey(task.AssetPath)) { AssetBundleCreateRequest ret = null; string fullpath = ""; lockSet.Add(task.AssetPath); //加锁 { fullpath = loder.FindAsset(task.AssetPath); ret = AssetBundle.LoadFromFileAsync(fullpath); yield return(ret); } lockSet.Remove(task.AssetPath); //解锁 //添加assetbundle if (ret.assetBundle != null) { loder.AddAssetBundle(task.AssetPath, ret.assetBundle); } else { callback(LoadAssetState.Fail, null); BDebug.LogError("ab资源为空:" + fullpath); yield break; } } if (task.IsMainAsset) { var instObj = loder.LoadFormAssetBundle <Object>(this.MainAssetName, this.manifestItem); callback(LoadAssetState.Success, instObj); } else { callback(LoadAssetState.Success, null); } }
/// <summary> /// 一个任务完成的回调 /// </summary> /// <param name="assetPath"></param> /// <param name="obj"></param> private void OnTaskComplete(LoaderTaskData task, Object obj) { if (isStop) { return; } //判断任务进度 if (!task.IsMainAsset) { this.DoNextTask(); } else { //总进度通知 IsComplete = true; OnAllTaskCompleteCallback?.Invoke(MainAssetName, obj); } }
public AsyncLoadTaskGroupResult(AssetBundleMgrV2 loder, AssetBundleItem assetBundleItem) { //赋值 this.loder = loder; this.AssetBundleItem = assetBundleItem; //1.依赖资源队列 this.taskQueue = new Queue <LoaderTaskData>(); var dependAssetList = loder.AssetConfigLoder.GetDependAssets(assetBundleItem); foreach (var dependAsset in dependAssetList) { var task = new LoaderTaskData(dependAsset, typeof(Object)); this.taskQueue.Enqueue(task); } //2.主资源添加队列 var mainTask = new LoaderTaskData(assetBundleItem, typeof(Object), true); this.taskQueue.Enqueue(mainTask); }
/// <summary> /// 加载 /// 一般来说,主资源才需要load /// 依赖资源只要加载ab,会自动依赖 /// </summary> /// <param name="assetPath"></param> /// <param name="isMainAsset">是否需要返回加载资源</param> /// <param name="callback"></param> /// <returns></returns> IEnumerator IE_AsyncLoadAssetbundle(LoaderTaskData task, Action <LoadAssetState> callback) { //正在被加载中,放入后置队列 if (lockSet.Contains(task.AssetPath)) { callback(LoadAssetState.IsLoding); yield break; } //没被加载 if (!loder.AssetbundleMap.ContainsKey(task.AssetPath)) { AssetBundleCreateRequest ret = null; string fullpath = ""; lockSet.Add(task.AssetPath); //加锁 { fullpath = loder.FindAsset(task.AssetPath); ret = AssetBundle.LoadFromFileAsync(fullpath); yield return(ret); } lockSet.Remove(task.AssetPath); //解锁 //添加assetbundle if (ret.assetBundle != null) { loder.AddAssetBundle(task.AssetPath, ret.assetBundle); } else { callback(LoadAssetState.Fail); BDebug.LogError("ab资源为空:" + fullpath); yield break; } } callback(LoadAssetState.Success); }