public AssetBundleLoader(AssetBundleInfo bundleInfo) : base(bundleInfo) { // 准备依赖列表 string[] dependencies = AssetSystem.BundleServices.GetDirectDependencies(bundleInfo.BundleName); if (dependencies != null && dependencies.Length > 0) { foreach (string dependBundleName in dependencies) { AssetBundleInfo dependBundleInfo = AssetSystem.BundleServices.GetAssetBundleInfo(dependBundleName); AssetLoaderBase dependLoader = AssetSystem.CreateLoaderInternal(dependBundleInfo); _depends.Add(dependLoader); } } }
public override void Update() { // 如果资源文件加载完毕 if (States == ELoaderStates.Success || States == ELoaderStates.Fail) { UpdateAllProvider(); return; } if (States == ELoaderStates.None) { States = ELoaderStates.LoadDepends; } // 1. 加载所有依赖项 if (States == ELoaderStates.LoadDepends) { string[] dependencies = AssetSystem.BundleServices.GetDirectDependencies(_manifestPath); if (dependencies.Length > 0) { foreach (string dpManifestPath in dependencies) { string dpLoadPath = AssetSystem.BundleServices.GetAssetBundleLoadPath(dpManifestPath); AssetLoaderBase dpLoader = AssetSystem.CreateLoaderInternal(dpLoadPath, dpManifestPath); _depends.Add(dpLoader); } } States = ELoaderStates.CheckDepends; } // 2. 检测所有依赖完成状态 if (States == ELoaderStates.CheckDepends) { foreach (var dpLoader in _depends) { if (dpLoader.IsDone() == false) { return; } } States = ELoaderStates.LoadFile; } // 3. 加载AssetBundle if (States == ELoaderStates.LoadFile) { #if UNITY_EDITOR // 注意:Unity2017.4编辑器模式下,如果AssetBundle文件不存在会导致编辑器崩溃,这里做了预判。 if (System.IO.File.Exists(LoadPath) == false) { MotionLog.Log(ELogLevel.Warning, $"Not found assetBundle file : {LoadPath}"); States = ELoaderStates.Fail; return; } #endif // Load assetBundle file if (AssetSystem.DecryptServices != null) { if (AssetSystem.DecryptServices.DecryptType == EDecryptMethod.GetDecryptOffset) { ulong offset = AssetSystem.DecryptServices.GetDecryptOffset(LoadPath); _cacheRequest = AssetBundle.LoadFromFileAsync(LoadPath, 0, offset); } else if (AssetSystem.DecryptServices.DecryptType == EDecryptMethod.GetDecryptBinary) { byte[] binary = AssetSystem.DecryptServices.GetDecryptBinary(LoadPath); _cacheRequest = AssetBundle.LoadFromMemoryAsync(binary); } else { throw new NotImplementedException($"{AssetSystem.DecryptServices.DecryptType}"); } } else { _cacheRequest = AssetBundle.LoadFromFileAsync(LoadPath); } States = ELoaderStates.CheckFile; } // 4. 检测AssetBundle加载结果 if (States == ELoaderStates.CheckFile) { if (_cacheRequest.isDone == false) { return; } CacheBundle = _cacheRequest.assetBundle; // Check error if (CacheBundle == null) { MotionLog.Log(ELogLevel.Warning, $"Failed to load assetBundle file : {LoadPath}"); States = ELoaderStates.Fail; } else { States = ELoaderStates.Success; } } }
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; } } }