Exemple #1
0
        private void LoadBundleFromFile(AssetLoadTask task)
        {
            string      path = Util.AppContentPath() + task.path;
            AssetBundle ab   = AssetBundle.LoadFromFile(path);

            OnBundleLoaded(task, ab);
        }
Exemple #2
0
 private void DoTask(AssetLoadTask task)
 {
     if (task.dependencies == null)
     {
         DoImmediateTask(task);
     }
     else
     {
         if (task.loadedDependenciesCount >= task.dependencies.Count)
         {
             DoImmediateTask(task);
         }
         else
         {
             int i = task.loadedDependenciesCount;
             for (; i < task.dependencies.Count; ++i)
             {
                 if (dependenciesObj.ContainsKey(task.dependencies[i]) || persistantObjects.ContainsKey(task.dependencies[i]))
                 {
                     task.loadedDependenciesCount += 1;
                     if (task.loadedDependenciesCount >= task.dependencies.Count)
                     {
                         DoImmediateTask(task);
                         return;
                     }
                 }
                 else
                 {
                     AddTask(task.dependencies[i], null, task.loadType, task.id);
                 }
             }
         }
     }
 }
Exemple #3
0
        private IEnumerator LoadBundleFromWWW(AssetLoadTask task)
        {
            string path = Util.AppContentPath() + task.path;
            var    www  = new WWW(path);

            yield return(www);

            if (null != www.error)
            {
                Debug.LogErrorFormat("LoadBundleAsync: {0} failed! www error:{1}", task.path, www.error);
            }
            OnBundleLoaded(task, www.assetBundle);
            www.Dispose();
        }
Exemple #4
0
 public void RemoveTask(uint taskId, Action <Object> action)
 {
     if (IsLoading(taskId))
     {
         AssetLoadTask oldTask = null;
         if (loadingTasks.TryGetValue(taskId, out oldTask))
         {
             if (null != action)
             {
                 oldTask.actions -= action;
             }
         }
     }
 }
Exemple #5
0
 private void DoImmediateTask(AssetLoadTask task)
 {
     currentTaskCount += 1;
     if ((task.loadType & (int)ResourceLoadType.LoadBundleFromWWW) != 0)
     {
         StartCoroutine(LoadBundleFromWWW(task));
     }
     else if ((task.loadType & (int)ResourceLoadType.LoadBundleFromFile) != 0)
     {
         LoadBundleFromFile(task);
     }
     else
     {
         currentTaskCount -= 1;
         Debug.LogErrorFormat("Unknown task loadtype:{0} path:{1}", task.loadType, task.path);
     }
 }
Exemple #6
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);
            }
        }
Exemple #7
0
        private void DoDelayTasks()
        {
            if (delayAssetLoadTask.Count > 0)
            {
                while (delayAssetLoadTask.Count > 0 && currentTaskCount < MaxTaskCount)
                {
                    AssetLoadTask task = delayAssetLoadTask.Dequeue();
                    DoTask(task);
                }
            }
            if (delayLoadTask.Count > 0)
            {
                var maxLoadTime = 0.02f;
                var startTime   = Time.realtimeSinceStartup;
                while (delayLoadTask.Count > 0 && Time.realtimeSinceStartup - startTime < maxLoadTime)
                {
                    LoadTask loadTask = delayLoadTask.Dequeue();
                    var      task     = loadTask.task;
                    var      bundle   = loadTask.bundle;

                    Object obj = null;
                    if (bundle != null)
                    {
                        if (!bundle.isStreamedSceneAssetBundle)
                        {
                            var objs = bundle.LoadAllAssets();
                            if (objs.Length > 0)
                            {
                                obj = objs[0];
                            }
                            if (obj == null)
                            {
                                Debug.LogErrorFormat("LoadBundle: {0} ! No Assets in Bundle!", task.path);
                            }
                        }
                    }
                    OnAseetsLoaded(task, bundle, obj);
                    loadTaskPool.Put(loadTask);
                }
            }
        }
Exemple #8
0
        //Asset加载完毕,可能是依赖资源,也可能是主资源
        private void OnAseetsLoaded(AssetLoadTask task, AssetBundle ab, Object obj)
        {
            string[] dependencies = manifest.GetAllDependencies(task.path);
            if (dependencies == null || dependencies.Length == 0)
            {
                RemoveRefCount(task.path);
            }

            loadingFiles.Remove(task.path);
            loadingTasks.Remove(task.id);

            //主资源加载完毕
            if (task.actions != null && task.parentTaskIds == null)
            {
                Delegate[] delegates = task.actions.GetInvocationList();
                foreach (var dele in delegates)
                {
                    var action = (Action <Object>)dele;
                    try
                    {
                        if ((task.loadType & (int)ResourceLoadType.ReturnAssetBundle) > 0)
                        {
                            action(ab);
                        }
                        else
                        {
                            action(obj);
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.LogErrorFormat("Load Bundle {0} DoAction Exception: {1}", task.path, e);
                    }
                }
            }

            //缓存加载的资源对象[非AB]
            if (ab != null && task.parentTaskIds == null)
            {
                if ((task.loadType & (int)ResourceLoadType.Persistent) > 0)
                {
                    persistantObjects[task.path] = obj;
                    if ((task.loadType & (int)ResourceLoadType.UnLoad) > 0)
                    {
                        ab.Unload(false);
                    }
                }
                else
                {
                    if ((task.loadType & (int)ResourceLoadType.Cache) > 0)
                    {
                        var cacheObject = new CacheObject
                        {
                            lastUseTime = Time.realtimeSinceStartup,
                            obj         = obj
                        };

                        cacheObjects[task.path] = cacheObject;
                    }
                    if ((task.loadType & (int)ResourceLoadType.ReturnAssetBundle) == 0)
                    {
                        ab.Unload(false);
                    }
                }
            }

            //依赖资源加载完毕
            if (task.parentTaskIds != null)
            {
                dependenciesObj[task.path] = ab;
                for (int i = 0; i < task.parentTaskIds.Count; ++i)
                {
                    uint          taskid = task.parentTaskIds[i];
                    AssetLoadTask pt     = null;
                    if (loadingTasks.TryGetValue(taskid, out pt))
                    {
                        pt.loadedDependenciesCount += 1;
                        if (pt.loadedDependenciesCount >= pt.dependencies.Count)
                        {
                            DoTask(pt);
                        }
                    }
                }
            }

            task.Reset();
            assetLoadTaskPool.Put(task);
        }