Exemplo n.º 1
0
        /// <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);
        }
Exemplo n.º 2
0
        /// <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);
        }
Exemplo n.º 3
0
        /// <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);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 核心功能,所有任务靠这个推进度
        /// 执行下个任务
        /// </summary>
        void DoNextTask()
        {
            if (isDoing)
            {
                return;
            }

            //没有东西的时候 跳出
            if (currentTaskGroup == null && this.allTaskList.Count == 0)
            {
                Debugger.Log("---------无任务组,退出----------");
                return;
            }
            else if (currentTaskGroup == null && this.allTaskList.Count > 0)
            {
                Debugger.Log("---------开始执行任务组----------");
                currentTaskGroup = this.allTaskList[0];
            }

            LoadTask task = null;

            //获取一个任务
            for (;;)
            {
                task = currentTaskGroup.GetTask();
                //task为空,或者任务可以执行,跳出
                if (task == null || willDoTaskSet.Contains(task.Id))
                {
                    break;
                }
            }


            //当前任务组已经全部完成
            if (task == null)
            {
                Debugger.Log("---------一组加载任务组已完成----------");
                currentTaskGroup = null;
                this.allTaskList.RemoveAt(0);

                DoNextTask();
            }
            else
            {
                //
                Debugger.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>(IPath.Combine(this.artRootPath, path), path);
                    //任务完成
                    currentTaskGroup.OnOneTaskComplete(task.Id, task.ResourcePath, obj);
                    isDoing = false;
                    //继续执行
                    DoNextTask();
                });
            }
        }