예제 #1
0
        internal static AssetLoaderBase CreateLoaderInternal(AssetBundleInfo bundleInfo)
        {
            // 如果加载器已经存在
            AssetLoaderBase loader = TryGetLoader(bundleInfo.BundleName);

            if (loader != null)
            {
                return(loader);
            }

            // 创建加载器
            if (SimulationOnEditor)
            {
                loader = new AssetDatabaseLoader(bundleInfo);
            }
            else
            {
                loader = new AssetBundleLoader(bundleInfo);
            }

            // 新增下载需求
            _loaders.Add(loader);
            return(loader);
        }
예제 #2
0
        /// <summary>
        /// 创建资源文件加载器
        /// </summary>
        public static AssetLoaderBase CreateLoader(string location, string variant)
        {
            AssetBundleInfo bundleInfo = GetAssetBundleInfo(location, variant);

            return(CreateLoaderInternal(bundleInfo));
        }
예제 #3
0
 public AssetDatabaseLoader(AssetBundleInfo bundleInfo)
     : base(bundleInfo)
 {
 }
예제 #4
0
 public AssetLoaderBase(AssetBundleInfo bundleInfo)
 {
     BundleInfo = bundleInfo;
     RefCount   = 0;
     States     = ELoaderStates.None;
 }
예제 #5
0
        /// <summary>
        /// 创建资源文件加载器
        /// </summary>
        public static FileLoaderBase CreateLoader(string location)
        {
            AssetBundleInfo bundleInfo = GetAssetBundleInfo(location);

            return(CreateLoaderInternal(bundleInfo));
        }
예제 #6
0
        public override void Update()
        {
            // 如果资源文件加载完毕
            if (States == ELoaderStates.Success || States == ELoaderStates.Fail)
            {
                UpdateAllProvider();
                return;
            }

            if (States == ELoaderStates.None)
            {
                // 检测加载地址是否为空
                if (string.IsNullOrEmpty(BundleInfo.LocalPath))
                {
                    States = ELoaderStates.Fail;
                    return;
                }

                if (string.IsNullOrEmpty(BundleInfo.RemoteURL))
                {
                    States = ELoaderStates.LoadDepends;
                }
                else
                {
                    States = ELoaderStates.Download;
                }
            }

            // 1. 从服务器下载
            if (States == ELoaderStates.Download)
            {
                _downloader = new WebFileRequest(BundleInfo.RemoteURL, BundleInfo.LocalPath);
                _downloader.DownLoad();
                States = ELoaderStates.CheckDownload;
            }

            // 2. 检测服务器下载结果
            if (States == ELoaderStates.CheckDownload)
            {
                if (_downloader.IsDone() == false)
                {
                    return;
                }

                if (_downloader.HasError())
                {
                    _downloader.ReportError();
                    States = ELoaderStates.Fail;
                }
                else
                {
                    // 校验文件完整性
                    if (AssetSystem.BundleServices.CheckContentIntegrity(BundleInfo.ManifestPath) == false)
                    {
                        MotionLog.Error($"Check download content integrity is failed : {BundleInfo.ManifestPath}");
                        States = ELoaderStates.Fail;
                    }
                    else
                    {
                        States = ELoaderStates.LoadDepends;
                    }
                }

                // 释放网络资源下载器
                if (_downloader != null)
                {
                    _downloader.Dispose();
                    _downloader = null;
                }
            }

            // 3. 加载所有依赖项
            if (States == ELoaderStates.LoadDepends)
            {
                string[] dependencies = AssetSystem.BundleServices.GetDirectDependencies(BundleInfo.ManifestPath);
                if (dependencies != null && dependencies.Length > 0)
                {
                    foreach (string dpManifestPath in dependencies)
                    {
                        AssetBundleInfo dpBundleInfo = AssetSystem.BundleServices.GetAssetBundleInfo(dpManifestPath);
                        AssetLoaderBase dpLoader     = AssetSystem.CreateLoaderInternal(dpBundleInfo);
                        _depends.Add(dpLoader);
                    }
                }
                States = ELoaderStates.CheckDepends;
            }

            // 4. 检测所有依赖完成状态
            if (States == ELoaderStates.CheckDepends)
            {
                foreach (var dpLoader in _depends)
                {
                    if (dpLoader.IsDone() == false)
                    {
                        return;
                    }
                }
                States = ELoaderStates.LoadFile;
            }

            // 5. 加载AssetBundle
            if (States == ELoaderStates.LoadFile)
            {
#if UNITY_EDITOR
                // 注意:Unity2017.4编辑器模式下,如果AssetBundle文件不存在会导致编辑器崩溃,这里做了预判。
                if (System.IO.File.Exists(BundleInfo.LocalPath) == false)
                {
                    MotionLog.Warning($"Not found assetBundle file : {BundleInfo.LocalPath}");
                    States = ELoaderStates.Fail;
                    return;
                }
#endif

                // Load assetBundle file
                if (BundleInfo.IsEncrypted)
                {
                    if (AssetSystem.DecryptServices == null)
                    {
                        throw new Exception($"{nameof(AssetBundleLoader)} need IDecryptServices : {BundleInfo.ManifestPath}");
                    }

                    EDecryptMethod decryptType = AssetSystem.DecryptServices.DecryptType;
                    if (decryptType == EDecryptMethod.GetDecryptOffset)
                    {
                        ulong offset = AssetSystem.DecryptServices.GetDecryptOffset(BundleInfo);
                        _cacheRequest = AssetBundle.LoadFromFileAsync(BundleInfo.LocalPath, 0, offset);
                    }
                    else if (decryptType == EDecryptMethod.GetDecryptBinary)
                    {
                        byte[] binary = AssetSystem.DecryptServices.GetDecryptBinary(BundleInfo);
                        _cacheRequest = AssetBundle.LoadFromMemoryAsync(binary);
                    }
                    else
                    {
                        throw new NotImplementedException($"{decryptType}");
                    }
                }
                else
                {
                    _cacheRequest = AssetBundle.LoadFromFileAsync(BundleInfo.LocalPath);
                }
                States = ELoaderStates.CheckFile;
            }

            // 6. 检测AssetBundle加载结果
            if (States == ELoaderStates.CheckFile)
            {
                if (_cacheRequest.isDone == false)
                {
                    return;
                }
                CacheBundle = _cacheRequest.assetBundle;

                // Check error
                if (CacheBundle == null)
                {
                    MotionLog.Warning($"Failed to load assetBundle file : {BundleInfo.ManifestPath}");
                    States = ELoaderStates.Fail;
                }
                else
                {
                    States = ELoaderStates.Success;
                }
            }
        }