public static async Task DownloadBundle() { if (Define.IsAsync) { try { using (BundleDownloaderComponent bundleDownloaderComponent = Game.Scene.AddComponent <BundleDownloaderComponent>()) { // 下载Version进行MD5对比 await bundleDownloaderComponent.StartAsync(); // 根据对比结果进行自动下载更新 await bundleDownloaderComponent.DownloadAsync(); } // 下载成功后读取AssetBundleManifest数据并且缓存起来 Game.Scene.GetComponent <ResourcesComponent>().LoadOneBundle("StreamingAssets"); ResourcesComponent.AssetBundleManifestObject = (AssetBundleManifest)Game.Scene.GetComponent <ResourcesComponent>().GetAsset("StreamingAssets", "AssetBundleManifest"); // 完成更新 FGUI.GetShowingUI <UI_CheckUpdate>()?.SetState(DownloadState.Done); } catch (Exception e) { Log.Error(e); } } else { // 编辑器模式直接跳到完成 FGUI.GetShowingUI <UI_CheckUpdate>()?.SetState(DownloadState.Done); } }
public async Task DownloadAsync() { if (this.bundles.Count == 0 && this.downloadingBundle == "") { // 请自行设计刷新UI进度操作 *这边只做测试代码 FGUI.GetShowingUI <UI_CheckUpdate>()?.UpdateState(1, 1, 0, ""); FGUI.GetShowingUI <UI_CheckUpdate>()?.SetState(DownloadState.Done); return; } try { while (true) { if (this.bundles.Count == 0) { // 请自行设计刷新UI进度操作 *这边只做测试代码 FGUI.GetShowingUI <UI_CheckUpdate>()?.UpdateState(1, 1, 0, ""); FGUI.GetShowingUI <UI_CheckUpdate>()?.SetState(DownloadState.Done); break; } this.downloadingBundle = this.bundles.Dequeue(); while (true) { try { using (this.webRequest = ComponentFactory.Create <UnityWebRequestAsync>()) { await this.webRequest.DownloadAsync(GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + this.downloadingBundle); byte[] data = this.webRequest.Request.downloadHandler.data; string path = Path.Combine(PathHelper.AppHotfixResPath, this.downloadingBundle); using (FileStream fs = new FileStream(path, FileMode.Create)) { fs.Write(data, 0, data.Length); } } } catch (Exception e) { Log.Error($"download bundle error: {this.downloadingBundle}\n{e}"); continue; } break; } this.downloadedBundles.Add(this.downloadingBundle); this.downloadingBundle = ""; this.webRequest = null; } } catch (Exception e) { Log.Error(e); } }