using System; using System.Collections; using Game; using TMPro; using UnityEngine; using UnityEngine.UI; namespace Mono { public class UILoading : MonoBehaviour { public Slider slider; public TMP_Text txt_Slider; private void Awake() { EventManager.Instance.Subscribe(GlobalInitFinishEventArgs.EventId, GlobalInitFinishEvent); EventManager.Instance.Subscribe(InitializeFailedEventArgs.EventId, InitializeFailedEvent); EventManager.Instance.Subscribe(PatchStatesChangeEventArgs.EventId, PatchStatesChangeEvent); EventManager.Instance.Subscribe(FoundUpdateFilesEventArgs.EventId, FoundUpdateFilesEvent); EventManager.Instance.Subscribe(DownloadProgressUpdateEventArgs.EventId, DownloadProgressUpdateEvent); EventManager.Instance.Subscribe(PackageVersionUpdateFailedEventArgs.EventId, PackageVersionUpdateFailedEvent); EventManager.Instance.Subscribe(PatchManifestUpdateFailedEventArgs.EventId, PatchManifestUpdateFailedEvent); EventManager.Instance.Subscribe(WebFileDownloadFailedEventArgs.EventId, WebFileDownloadFailedEvent); EventManager.Instance.Subscribe(UpdaterDoneEventArgs.EventId, UpdaterDoneEvent); this.btn_Yes.onClick.AddListener(() => { EventManager.Instance.FireNow(UpdatePackageCallbackEventArgs.EventId, new UpdatePackageCallbackEventArgs(UpdatePackageCallbackType.开始下载网络文件)); this.downloadTips.SetActive(false); }); this.btn_No.onClick.AddListener(() => { Debug.Log("取消更新就退出,或者进入单机模型"); this.downloadTips.SetActive(false); Application.Quit(); }); } private void OnDestroy() { EventManager.Instance.Unsubscribe(GlobalInitFinishEventArgs.EventId, GlobalInitFinishEvent); EventManager.Instance.Unsubscribe(InitializeFailedEventArgs.EventId, InitializeFailedEvent); EventManager.Instance.Unsubscribe(PatchStatesChangeEventArgs.EventId, PatchStatesChangeEvent); EventManager.Instance.Unsubscribe(FoundUpdateFilesEventArgs.EventId, FoundUpdateFilesEvent); EventManager.Instance.Unsubscribe(DownloadProgressUpdateEventArgs.EventId, DownloadProgressUpdateEvent); EventManager.Instance.Unsubscribe(PackageVersionUpdateFailedEventArgs.EventId, PackageVersionUpdateFailedEvent); EventManager.Instance.Unsubscribe(PatchManifestUpdateFailedEventArgs.EventId, PatchManifestUpdateFailedEvent); EventManager.Instance.Unsubscribe(WebFileDownloadFailedEventArgs.EventId, WebFileDownloadFailedEvent); EventManager.Instance.Unsubscribe(UpdaterDoneEventArgs.EventId, UpdaterDoneEvent); } public GameObject downloadTips; public TMP_Text txtDownloadTips; public Button btn_Yes; public Button btn_No; private void ShowDownloadTips(int count, string size) { this.downloadTips.SetActive(true); txtDownloadTips.text = $"Found update patch files, Total count {count} Total szie {size}MB"; } public GameObject tips; public TMP_Text txt_Tips; IEnumerator ShowTips(string str) { this.tips.SetActive(true); this.txt_Tips.text = str; yield return new WaitForSeconds(1); this.tips.SetActive(false); } #region Event private void InitializeFailedEvent(object sender, GameEventArgs e) { var args = e as InitializeFailedEventArgs; string str = "初始化失败"; StartCoroutine(ShowTips(str)); EventManager.Instance.FireNow(UpdatePackageCallbackEventArgs.EventId, new UpdatePackageCallbackEventArgs(UpdatePackageCallbackType.再次初始化资源包)); } private void PatchStatesChangeEvent(object sender, GameEventArgs e) { var args = e as PatchStatesChangeEventArgs; string str = $"补丁流程步骤改变: {args.Tips}"; StartCoroutine(ShowTips(str)); } private void FoundUpdateFilesEvent(object sender, GameEventArgs e) { var args = e as FoundUpdateFilesEventArgs; string str = $"发现更新文件: {args.TotalCount}, {args.TotalSizeBytes}"; StartCoroutine(ShowTips(str)); float sizeMB = args.TotalSizeBytes / 1048576f; sizeMB = Mathf.Clamp(sizeMB, 0.1f, float.MaxValue); string totalSizeMB = sizeMB.ToString("f1"); // 弹出对话框,让用户选择是否更新。 Debug.Log($"Found update patch files, Total count {args.TotalCount} Total szie {totalSizeMB}MB"); ShowDownloadTips(args.TotalCount, totalSizeMB); } private void DownloadProgressUpdateEvent(object sender, GameEventArgs e) { var args = e as DownloadProgressUpdateEventArgs; slider.value = (float)args.CurrentDownloadCount / args.TotalDownloadCount; string currentSizeMB = (args.CurrentDownloadSizeBytes / 1048576f).ToString("f1"); string totalSizeMB = (args.TotalDownloadSizeBytes / 1048576f).ToString("f1"); this.txt_Slider.text = $"{args.CurrentDownloadCount}/{args.TotalDownloadCount} {currentSizeMB}MB/{totalSizeMB}MB"; } private void PackageVersionUpdateFailedEvent(object sender, GameEventArgs e) { var args = e as PackageVersionUpdateFailedEventArgs; Debug.Log($"Failed to update static version, please check the network status."); string str = $"Failed to update static version, please check the network status."; StartCoroutine(ShowTips(str)); // 用户尝试再次更新静态版本,此步回调资源更新中的更新版本步骤 EventManager.Instance.FireNow(UpdatePackageCallbackEventArgs.EventId, new UpdatePackageCallbackEventArgs(UpdatePackageCallbackType.再次更新静态版本)); } private void PatchManifestUpdateFailedEvent(object sender, GameEventArgs e) { var args = e as PatchManifestUpdateFailedEventArgs; Debug.Log("Failed to update patch manifest, please check the network status."); string str = $"Failed to update patch manifest, please check the network status."; StartCoroutine(ShowTips(str)); // 用户尝试再次更新补丁清单 EventManager.Instance.FireNow(UpdatePackageCallbackEventArgs.EventId, new UpdatePackageCallbackEventArgs(UpdatePackageCallbackType.再次更新补丁清单)); } private void WebFileDownloadFailedEvent(object sender, GameEventArgs e) { var args = e as WebFileDownloadFailedEventArgs; Debug.Log($"Failed to download file : {args.FileName}"); string str = $"Failed to download file : {args.FileName}"; StartCoroutine(ShowTips(str)); // 用户尝试再次下载网络文件 EventManager.Instance.FireNow(UpdatePackageCallbackEventArgs.EventId, new UpdatePackageCallbackEventArgs(UpdatePackageCallbackType.再次下载网络文件)); } private void UpdaterDoneEvent(object sender, GameEventArgs e) { var args = e as UpdaterDoneEventArgs; Debug.Log("资源更新完成"); string str = $"资源更新完成"; slider.value = 1; StartCoroutine(ShowTips(str)); } private void GlobalInitFinishEvent(object sender, GameEventArgs e) { Destroy(this.gameObject); } #endregion } }