using System; using System.Collections; using TMPro; using UnityEngine; using UnityEngine.Serialization; using UnityEngine.UI; namespace ZC { public class UILoading : MonoBehaviour { public Slider slider; public TMP_Text txt_Slider; private void Awake() { 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(ClickBtnYes); this.btn_No.onClick.AddListener(ClickBtnNo); this.btnSingle.onClick.AddListener(this.ClickBtnSingle); this.btnQuit.onClick.AddListener(this.ClickBtnQuit); } private void OnDestroy() { 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); this.btn_Yes.onClick.RemoveListener(ClickBtnYes); this.btn_No.onClick.RemoveListener(ClickBtnNo); this.btnSingle.onClick.RemoveListener(this.ClickBtnSingle); this.btnQuit.onClick.RemoveListener(this.ClickBtnQuit); } 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); } private void ClickBtnNo() { Debug.Log("取消更新就退出,或者进入单机模型"); this.downloadTips.SetActive(false); Application.Quit(); } private void ClickBtnYes() { EventManager.Instance.FireNow(UpdatePackageCallbackEventArgs.EventId, new UpdatePackageCallbackEventArgs(UpdatePackageCallbackType.开始下载网络文件)); this.downloadTips.SetActive(false); } public GameObject errorTips; public TMP_Text txtErrorTips; public Button btnSingle; public Button btnQuit; void SetTipsContent(string str) { this.txtErrorTips.text = str; this.errorTips.SetActive(true); } private void ClickBtnSingle() { this.errorTips.SetActive(false); EventManager.Instance.FireNow(EnterSingleModeEventArgs.EventId, new EnterSingleModeEventArgs()); } private void ClickBtnQuit() { Debug.Log("取消更新就退出,或者进入单机模型"); this.errorTips.SetActive(false); Application.Quit(); } #region Event private int count; 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."); this.count++; if (this.count <= 3) { string str = $"Failed to update static version, please check the network status."; StartCoroutine(ShowTips(str)); // 用户尝试再次更新静态版本,此步回调资源更新中的更新版本步骤 EventManager.Instance.FireNow(UpdatePackageCallbackEventArgs.EventId, new UpdatePackageCallbackEventArgs(UpdatePackageCallbackType.再次更新静态版本)); } else { string str = "获取最新版本失败,请检查服务器是否未开启!"; SetTipsContent(str); } } 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)); GlobalInitFinishEvent(this, null); } private void GlobalInitFinishEvent(object sender, GameEventArgs e) { Destroy(this.gameObject); } #endregion } }