Exemplo n.º 1
0
        //AssetBundle加载完毕
        private void OnBundleLoaded(AssetLoadTask task, AssetBundle ab)
        {
            _currentTaskCount -= 1;
            Object obj = null;

            if (ab == null)
            {
                Debug.LogErrorFormat("LoadBundle: {0} failed! assetBundle == NULL!", task.path);
                OnAseetsLoaded(task, ab, obj);
            }
            else
            {
                var loadTask = _loadTaskPool.Get();
                loadTask.task   = task;
                loadTask.bundle = ab;
                _delayLoadTask.Enqueue(loadTask);
            }
        }
Exemplo n.º 2
0
        private uint AddTask(string file, Action <Object> action, int loadType, uint parentTaskId)
        {
            if (string.IsNullOrEmpty(file))
            {
                return(0);
            }
            string fileReplace = file.Replace(@"\", @"/");

            string lowerFile = fileReplace.ToLower();
            Object obj;

            if (_persistantObjects.TryGetValue(lowerFile, out obj))
            {
                action(obj);
                return(0);
            }

            CacheObject cacheObject;

            if (_cacheObjects.TryGetValue(lowerFile, out cacheObject))
            {
                cacheObject.lastUseTime = Time.realtimeSinceStartup;

                action(cacheObject.obj);
                return(0);
            }

            AssetLoadTask oldTask;

            if (_loadingFiles.TryGetValue(lowerFile, out oldTask))
            {//资源加载任务-正在进行中
                if (action != null)
                {
                    oldTask.actions += action;
                }

                if ((loadType & (int)ResourceLoadType.Persistent) > 0)
                {
                    oldTask.loadType |= (int)ResourceLoadType.Persistent;
                }

                if (parentTaskId != 0)
                {
                    if (oldTask.parentTaskIds == null)
                    {
                        oldTask.parentTaskIds = new List <uint>();
                        Debug.LogErrorFormat("resource path {0} type is mixed, dependency resource or not ", oldTask.path);
                    }
                    oldTask.parentTaskIds.Add(parentTaskId);
                }

                return(0);
            }

            //加载资源到内存
            uint        id     = ++_nextTaskId;
            List <uint> ptList = null;

            if (parentTaskId != 0)
            {
                ptList = new List <uint>();
                ptList.Add(parentTaskId);
            }

            var task = _assetLoadTaskPool.Get();

            {
                task.id                      = id;
                task.parentTaskIds           = ptList;
                task.path                    = lowerFile;
                task.loadType                = loadType;
                task.actions                 = action;
                task.dependencies            = _assetDependencies.ContainsKey(lowerFile) ? _assetDependencies[lowerFile] : null;
                task.loadedDependenciesCount = 0;
            };
            _loadingFiles[lowerFile] = task;
            _loadingTasks[id]        = task;
            if (_assetBundlesObj.ContainsKey(task.path))
            {//依赖资源已被加载
                AddRefCount(task.path);
            }
            //任务数量达最大时,延迟加载资源[需要排队]
            if (_currentTaskCount < MaxTaskCount)
            {
                DoTask(task);
            }
            else
            {
                _delayAssetLoadTask.Enqueue(task);
            }

            return(id);
        }