/// <summary> /// 轮询更新 /// </summary> public override void Update() { if (_steps == ESteps.Done) { return; } if (_steps == ESteps.None) { if (MainBundleInfo.IsInvalid) { _steps = ESteps.Done; Status = EStatus.Failed; LastError = $"The bundle info is invalid : {MainBundleInfo.BundleName}"; YooLogger.Error(LastError); return; } if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming) { _steps = ESteps.LoadFile; _webURL = MainBundleInfo.GetStreamingLoadPath(); } else { throw new System.NotImplementedException(MainBundleInfo.LoadMode.ToString()); } } // 1. 从服务器或缓存中获取AssetBundle文件 if (_steps == ESteps.LoadFile) { string hash = StringUtility.RemoveExtension(MainBundleInfo.Hash); _webRequest = UnityWebRequestAssetBundle.GetAssetBundle(_webURL, Hash128.Parse(hash)); _webRequest.SendWebRequest(); _steps = ESteps.CheckFile; } // 2. 检测获取的AssetBundle文件 if (_steps == ESteps.CheckFile) { if (_webRequest.isDone == false) { return; } #if UNITY_2020_1_OR_NEWER if (_webRequest.result != UnityWebRequest.Result.Success) #else if (_webRequest.isNetworkError || _webRequest.isHttpError) #endif { YooLogger.Warning($"Failed to get asset bundle form web : {_webURL} Error : {_webRequest.error}"); _steps = ESteps.TryLoad; _tryTimer = 0; } else { CacheBundle = DownloadHandlerAssetBundle.GetContent(_webRequest); if (CacheBundle == null) { _steps = ESteps.Done; Status = EStatus.Failed; LastError = $"AssetBundle file is invalid : {MainBundleInfo.BundleName}"; YooLogger.Error(LastError); } else { _steps = ESteps.Done; Status = EStatus.Succeed; } } } // 3. 如果获取失败,重新尝试 if (_steps == ESteps.TryLoad) { _tryTimer += Time.unscaledDeltaTime; if (_tryTimer > 1f) { _webRequest.Dispose(); _webRequest = null; _steps = ESteps.LoadFile; } } }
/// <summary> /// 轮询更新 /// </summary> public override void Update() { if (_steps == ESteps.Done) { return; } if (_steps == ESteps.None) { if (MainBundleInfo.IsInvalid) { _steps = ESteps.Done; Status = EStatus.Failed; LastError = $"The bundle info is invalid : {MainBundleInfo.BundleName}"; YooLogger.Error(LastError); return; } if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote) { _steps = ESteps.Download; _fileLoadPath = MainBundleInfo.GetCacheLoadPath(); } else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming) { _steps = ESteps.LoadFile; _fileLoadPath = MainBundleInfo.GetStreamingLoadPath(); } else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache) { _steps = ESteps.LoadFile; _fileLoadPath = MainBundleInfo.GetCacheLoadPath(); } else { throw new System.NotImplementedException(MainBundleInfo.LoadMode.ToString()); } } // 1. 从服务器下载 if (_steps == ESteps.Download) { int failedTryAgain = int.MaxValue; _downloader = DownloadSystem.BeginDownload(MainBundleInfo, failedTryAgain); _steps = ESteps.CheckDownload; } // 2. 检测服务器下载结果 if (_steps == ESteps.CheckDownload) { if (_downloader.IsDone() == false) { return; } if (_downloader.HasError()) { _steps = ESteps.Done; Status = EStatus.Failed; LastError = _downloader.GetLastError(); } else { _steps = ESteps.LoadFile; } } // 3. 加载AssetBundle if (_steps == ESteps.LoadFile) { #if UNITY_EDITOR // 注意:Unity2017.4编辑器模式下,如果AssetBundle文件不存在会导致编辑器崩溃,这里做了预判。 if (System.IO.File.Exists(_fileLoadPath) == false) { _steps = ESteps.Done; Status = EStatus.Failed; LastError = $"Not found assetBundle file : {_fileLoadPath}"; YooLogger.Error(LastError); return; } #endif // Load assetBundle file if (MainBundleInfo.IsEncrypted) { if (AssetSystem.DecryptionServices == null) { throw new Exception($"{nameof(AssetBundleFileLoader)} need {nameof(IDecryptionServices)} : {MainBundleInfo.BundleName}"); } ulong offset = AssetSystem.DecryptionServices.GetFileOffset(); if (_isWaitForAsyncComplete) { CacheBundle = AssetBundle.LoadFromFile(_fileLoadPath, 0, offset); } else { _cacheRequest = AssetBundle.LoadFromFileAsync(_fileLoadPath, 0, offset); } } else { if (_isWaitForAsyncComplete) { CacheBundle = AssetBundle.LoadFromFile(_fileLoadPath); } else { _cacheRequest = AssetBundle.LoadFromFileAsync(_fileLoadPath); } } _steps = ESteps.CheckFile; } // 4. 检测AssetBundle加载结果 if (_steps == ESteps.CheckFile) { if (_cacheRequest != null) { if (_isWaitForAsyncComplete) { // 强制挂起主线程(注意:该操作会很耗时) YooLogger.Warning("Suspend the main thread to load unity bundle."); CacheBundle = _cacheRequest.assetBundle; } else { if (_cacheRequest.isDone == false) { return; } CacheBundle = _cacheRequest.assetBundle; } } // Check error if (CacheBundle == null) { _steps = ESteps.Done; Status = EStatus.Failed; LastError = $"Failed to load assetBundle : {MainBundleInfo.BundleName}"; YooLogger.Error(LastError); } else { _steps = ESteps.Done; Status = EStatus.Succeed; } } }