예제 #1
0
        private void LoadBundleFromFile(AssetLoadTask task)
        {
            string      path = GetResPath(task.path);
            AssetBundle ab   = AssetBundle.LoadFromFile(path);

            OnBundleLoaded(task, ab);
        }
예제 #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 (_assetBundlesObj.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);
                 }
             }
         }
     }
 }
예제 #3
0
        //资源加载操作
        private IEnumerator LoadBundleFromWWW(AssetLoadTask task)
        {
            string path = GetResPath(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();
        }
예제 #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;
             }
         }
     }
 }
예제 #5
0
 private void DoImmediateTask(AssetLoadTask task)
 {
     _currentTaskCount += 1;
     if ((task.loadType & (int)ResourceLoadType.LoadBundleFromWWW) != 0)
     {
         Client.Ins.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);
     }
 }
예제 #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);
            }
        }
예제 #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)
                {
                    CompleteTask 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.Release(loadTask);
                }
            }
        }
예제 #8
0
        //Asset加载完毕,可能是依赖资源,也可能是主资源
        private void OnAseetsLoaded(AssetLoadTask task, AssetBundle ab, Object obj)
        {
            if (_assetDependencies.ContainsKey(task.path))
            {
                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)
            {
                _assetBundlesObj[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.Release(task);
        }