/// <summary> /// 异步加载接口 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="path"></param> /// <returns></returns> public int AsyncLoad <T>(string path, Action <bool, T> callback) where T : UnityEngine.Object { var task = new LoadTask() { Id = this.taskIDCounter++, ResourcePath = path, }; //创建任务组 LoadTaskGroup taskGroup = new LoadTaskGroup(new List <LoadTask>() { task }, null, (map) => { if (map.Keys.Count > 0) { //只取第一个返回 foreach (var v in map.Values) { callback(true, v as T); return; } } else { callback(false, null); } }); AddTaskGroup(taskGroup); //开始任务 DoNextTask(); return(task.Id); }
/// <summary> /// 异步加载 多个 /// </summary> /// <param name="sources">资源</param> /// <param name="onLoadComplete">加载结束</param> /// <param name="onLoadProcess">进度</param> /// <returns>taskid</returns> public List <int> AsyncLoad(IList <string> sources, Action <IDictionary <string, UnityEngine.Object> > onLoadComplete, Action <int, int> onLoadProcess) { List <int> idList = new List <int>(); List <LoadTask> tasks = new List <LoadTask>(); foreach (var s in sources) { var task = new LoadTask() { Id = this.taskIDCounter++, ResourcePath = s, }; tasks.Add(task); idList.Add(task.Id); } LoadTaskGroup taskGroup = new LoadTaskGroup(tasks, onLoadProcess, onLoadComplete); //添加任务组 AddTaskGroup(taskGroup); //开始任务 DoNextTask(); // return(idList); }
/// <summary> /// 核心功能,所有任务靠这个推进度 /// 执行下个任务 /// </summary> void DoNextTask() { if (isDoing) { return; } //没有东西的时候 跳出 if (currentTaskGroup == null && this.allTaskList.Count == 0) { BDebug.Log("---------无任务组,退出----------"); return; } else if (currentTaskGroup == null && this.allTaskList.Count > 0) { BDebug.Log("---------开始执行任务组----------"); currentTaskGroup = this.allTaskList[0]; } LoadTask task = null; //获取一个任务 for (;;) { task = currentTaskGroup.GetTask(); //task为空,或者任务可以执行,跳出 if (task == null || willDoTaskSet.Contains(task.Id)) { break; } } //当前任务组已经全部完成 if (task == null) { BDebug.Log("---------一组加载任务组已完成----------"); currentTaskGroup = null; this.allTaskList.RemoveAt(0); DoNextTask(); } else { BDebug.Log("执行任务组中task:" + task.Id + " - " + task.ResourcePath); isDoing = true; //执行任务 AsyncLoadAssetBundle(task.ResourcePath, b => { //移除任务 this.willDoTaskSet.Remove(task.Id); // var path = "assets/resource/runtime/" + task.ResourcePath.ToLower(); path = GetExistPath(path); var obj = LoadFormAssetBundle <Object>(path, path); //任务完成 currentTaskGroup.OnOneTaskComplete(task.Id, task.ResourcePath, obj); isDoing = false; //继续执行 DoNextTask(); }); } }
/// <summary> /// 加载assetbundle 文件 /// </summary> private bool AsyncLoadAssetbundleFile() { //1.loadABFile,循环添加任务 while (!isCancel && AssetBundleMgrV2.IsCanAddGlobalTask && curLoadIdx < waitingLoadAssetBundleList.Count - 1) { curLoadIdx++; var abi = waitingLoadAssetBundleList[curLoadIdx]; //没有被加载过 var abw = loder.GetAssetBundleFromCache(abi.AssetBundlePath); if (abw == null) { //判断是否在加载中 var loadTask = AssetBundleMgrV2.GetExsitLoadTask(abi.AssetBundlePath); if (loadTask != null) { loadingTaskList.Add(new KeyValuePair <string, LoadTask>(abi.AssetBundlePath, loadTask)); } else { //创建任务 var abPath = loder.FindMultiAddressAsset(abi.AssetBundlePath); loadTask = new LoadTask(abPath, 0, (ulong)abi.Mix); //加入Global任务 AssetBundleMgrV2.AddGlobalLoadTask(loadTask); //添加到loading表 loadingTaskList.Add(new KeyValuePair <string, LoadTask>(abi.AssetBundlePath, loadTask)); //开始加载 loadTask.AysncLoad(); BDebug.Log($"【AsyncLoadTaskGroup】 加 载: {abi.AssetBundlePath}"); } } else { BDebug.Log($"【AsyncLoadTaskGroup】 无需加载: {abi.AssetBundlePath}"); } } //2.loadABFile,检测加载状态 if (loadingTaskList.Count > 0) { for (int i = loadingTaskList.Count - 1; i >= 0; i--) { var loadingTask = loadingTaskList[i]; var assetbundleFileName = loadingTask.Key; var loadTask = loadingTask.Value; //判断是否结束 if (loadTask.IsDone) { //添加到返回列表 if (loadTask.AssetBundle != null) { loder.AddAssetBundleToCache(assetbundleFileName, loadTask.AssetBundle); } else { BDebug.LogError("【AsyncLoadTaskGroup】ab资源为空:" + assetbundleFileName); } //移除成功任务 loadingTaskList.RemoveAt(i); //解锁 AssetBundleMgrV2.RemoveGlobalLoadTask(loadTask); BDebug.Log($"【AsyncLoadTaskGroup】--> 加载完成:{assetbundleFileName} 剩余:{loadingTaskList.Count + waitingLoadAssetBundleList.Count - (curLoadIdx + 1)}/{waitingLoadAssetBundleList.Count}"); } } } //3.任务执行完毕检测 if (!isCancel && loadingTaskList.Count == 0 && curLoadIdx == waitingLoadAssetBundleList.Count - 1) { //加载完成,主资源只要保证在 实例化之前加载完毕即可 //加载完则使用 BDebug.Log($"<color=green>【AsyncLoadTaskGroup】所有加载完成:{MainAssetBundleItem.AssetBundlePath}</color>"); return(true); } return(false); }