/// <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>
        /// <param name="taskGroup"></param>
        public void AddTaskGroup(LoadTaskGroup taskGroup)
        {
            foreach (var t in taskGroup.TaskIdList)
            {
                willDoTaskSet.Add(t.Id);
            }

            this.allTaskList.Add(taskGroup);
        }
        /// <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();
                });
            }
        }
Пример #5
0
        /// <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)
            {
                currentTaskGroup = this.allTaskList[0];
                BDebug.Log("---------开始执行任务组----------  |" + currentTaskGroup.GetHashCode());
                foreach (var t in currentTaskGroup.TaskIdList)
                {
                    BDebug.Log("--->" + t.ResourcePath);
                }
            }

            LoadTaskData taskData = null;

            //获取一个任务
            while (true)
            {
                taskData = currentTaskGroup.GetTask();

                //任务不存在当前可执行列表中
                if (taskData != null && !willDoTaskSet.Contains(taskData.Id))
                {
                    currentTaskGroup.RemoveTask(taskData.Id);
                    continue;
                }
                //task为空,或者任务可以执行,跳出
                if (taskData == null || willDoTaskSet.Contains(taskData.Id))
                {
                    break;
                }
            }


            //当前任务组已经全部完成
            if (taskData == null)
            {
                BDebug.Log("---------加载任务组已完成---------- |" + currentTaskGroup.GetHashCode());
                currentTaskGroup = null;
                this.allTaskList.RemoveAt(0);

                DoNextTask();
            }
            else
            {
                //
                BDebug.Log("执行任务组中task:" + taskData.Id + " - " + taskData.ResourcePath);
                isDoing = true;
                //执行任务
                AsyncLoadAssetBundle(taskData.ResourcePath, b =>
                {
                    //移除任务
                    this.willDoTaskSet.Remove(taskData.Id);
                    //

                    var path = GetAssetRealPath(taskData.ResourcePath);
                    var obj  = LoadFormAssetBundle(path, taskData.LoadType);
                    //任务完成
                    currentTaskGroup.OnOneTaskComplete(taskData.Id, taskData.ResourcePath, obj);
                    isDoing = false;
                    //继续执行
                    DoNextTask();
                });
            }
        }