Ejemplo n.º 1
0
        public static LoadAssetTask Create(string assetName, AssetCategory assetCategory, Type assetType, int priority, ResourceInfo resourceInfo, Dictionary <string, AssetCategory> dependencyAssetNames, LoadAssetCallbacks loadAssetCallbacks, object userData)
        {
            LoadAssetTask loadAssetTask = ReferencePool.Acquire <LoadAssetTask>();

            loadAssetTask.Initialize(assetName, assetCategory, assetType, priority, resourceInfo, dependencyAssetNames, userData);
            loadAssetTask.m_LoadAssetCallbacks = loadAssetCallbacks;
            return(loadAssetTask);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 开始处理加载资源任务。
        /// </summary>
        /// <param name="task">要处理的加载资源任务。</param>
        /// <returns>开始处理任务的状态。</returns>
        public StartTaskStatus Start(LoadResourceTaskBase task)
        {
            if (task == null)
            {
                throw new Exception("Task is invalid.");
            }

            m_Task           = task;
            m_Task.StartTime = DateTime.Now;
            ResourceInfo resourceInfo = m_Task.ResourceInfo;

            if (!resourceInfo.Ready)
            {
                m_Task.StartTime = default(DateTime);
                return(StartTaskStatus.HasToWait);
            }

            if (IsAssetLoading(m_Task.AssetName))
            {
                m_Task.StartTime = default(DateTime);
                return(StartTaskStatus.HasToWait);
            }

            if (!m_Task.IsScene)
            {
                AssetObject assetObject = m_ResourceLoader.AssetPool(m_Task.AssetCategory).Spawn(m_Task.AssetName);
                if (assetObject != null)
                {
                    OnAssetObjectReady(assetObject);
                    return(StartTaskStatus.Done);
                }
            }

            foreach (var dependencyAssetItem in m_Task.GetDependencyAssetNames())
            {
                if (!m_ResourceLoader.AssetPool(dependencyAssetItem.Value).CanSpawn(dependencyAssetItem.Key))
                {
                    m_Task.StartTime = default(DateTime);
                    return(StartTaskStatus.HasToWait);
                }
            }

            string resourceName = resourceInfo.ResourceName.Name;

            if (IsResourceLoading(resourceName))
            {
                m_Task.StartTime = default(DateTime);
                return(StartTaskStatus.HasToWait);
            }

            s_LoadingAssetNames.Add(m_Task.AssetName);

            ResourceObject resourceObject = m_ResourceLoader.ResourcePool.Spawn(resourceName);

            if (resourceObject != null)
            {
                OnResourceObjectReady(resourceObject);
                return(StartTaskStatus.CanResume);
            }

            s_LoadingResourceNames.Add(resourceName);


            string fullPath = null;

            if (!s_CachedResourceNames.TryGetValue(resourceName, out fullPath))
            {
                fullPath = Utility.Path.GetRegularPath(Path.Combine(resourceInfo.StorageInReadOnly ? m_ReadOnlyPath : m_ReadWritePath, resourceInfo.UseFileSystem ? resourceInfo.FileSystemName : resourceInfo.ResourceName.FullName));
                s_CachedResourceNames.Add(resourceName, fullPath);
            }

            if (resourceInfo.LoadType == LoadType.LoadFromFile)
            {
                if (resourceInfo.UseFileSystem)
                {
                    IFileSystem fileSystem = m_ResourceLoader.ResourceManager.GetFileSystem(resourceInfo.FileSystemName, resourceInfo.StorageInReadOnly);
                    m_Helper.ReadFile(fileSystem, resourceInfo.ResourceName.FullName);
                }
                else
                {
                    m_Helper.ReadFile(fullPath);
                }
            }
            else if (resourceInfo.LoadType == LoadType.LoadFromMemory || resourceInfo.LoadType == LoadType.LoadFromMemoryAndQuickDecrypt || resourceInfo.LoadType == LoadType.LoadFromMemoryAndDecrypt)
            {
                if (resourceInfo.UseFileSystem)
                {
                    IFileSystem fileSystem = m_ResourceLoader.ResourceManager.GetFileSystem(resourceInfo.FileSystemName, resourceInfo.StorageInReadOnly);
                    m_Helper.ReadBytes(fileSystem, resourceInfo.ResourceName.FullName);
                }
                else
                {
                    m_Helper.ReadBytes(fullPath);
                }
            }
            else
            {
                throw new Exception(Utility.Text.Format("Resource load type '{0}' is not supported.", resourceInfo.LoadType.ToString()));
            }

            return(StartTaskStatus.CanResume);
        }