//-------------------------------------------------------- // [Author]: 第二世界 // [ Date ]: Wednesday, March 28, 2018 //-------------------------------------------------------- using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.IO; namespace Snxxz.UI { public class VersionUpdateWin : Window { [SerializeField] RectTransform m_ContainerHint; [SerializeField] RichText m_HintDescription; [SerializeField] RectTransform m_UpdateContentContainer; [SerializeField] RichText m_UpdateCotent; [SerializeField] Button m_Confirm; [SerializeField] Transform m_ContainerProgress; [SerializeField] SmoothSlider m_ProgressSlider; [SerializeField] Text m_Progress; [SerializeField] Text m_DownLoadSpeed; float timer = 1f; #region Built-in protected override void BindController() { m_Confirm.AddListener(Confirm); } protected override void AddListeners() { } protected override void OnPreOpen() { OnDownLoadStepChange(VersionUtility.Instance.step); } protected override void OnAfterOpen() { VersionUtility.Instance.downLoadStepChangeEvent += OnDownLoadStepChange; } protected override void OnPreClose() { VersionUtility.Instance.downLoadStepChangeEvent -= OnDownLoadStepChange; } protected override void OnAfterClose() { } #endregion private void OnDownLoadStepChange(VersionUtility.Step _step) { switch (_step) { case VersionUtility.Step.None: break; case VersionUtility.Step.ApkExist: m_ContainerHint.gameObject.SetActive(true); m_ContainerProgress.gameObject.SetActive(false); DisplayHintContent(); break; case VersionUtility.Step.DownLoadPrepared: case VersionUtility.Step.DownLoadFailed: m_ContainerHint.gameObject.SetActive(true); m_ContainerProgress.gameObject.SetActive(false); DisplayHintContent(); break; case VersionUtility.Step.DownLoad: m_ContainerHint.gameObject.SetActive(false); m_ContainerProgress.gameObject.SetActive(true); DisplayHintContent(); break; } } protected override void LateUpdate() { base.LateUpdate(); var step = VersionUtility.Instance.step; if (step == VersionUtility.Step.DownLoad) { timer += Time.deltaTime; if (timer > 1f) { timer -= 1f; m_ProgressSlider.value = VersionUtility.Instance.progress; var totalSizeString = ((float)VersionUtility.Instance.GetApkSize() * 1024 / DownLoadAndDiscompressTask.BYTE_PER_MILLIONBYTE).ToString("f1"); var downLoadedSize = Mathf.Clamp(RemoteFile.TotalDownloadedSize, 0, VersionUtility.Instance.GetApkSize() * 1024); var downLoadedSizeString = ((float)downLoadedSize / DownLoadAndDiscompressTask.BYTE_PER_MILLIONBYTE).ToString("f1"); m_Progress.text = Language.GetFromLocal(13, StringUtility.Contact(downLoadedSizeString, "M", "/", totalSizeString, "M")); } if (Time.frameCount % 2 == 0) { if (RemoteFile.TotalDownloadedSize >= VersionUtility.Instance.GetApkSize() * 1024) { m_DownLoadSpeed.text = StringUtility.Contact(UnityEngine.Random.Range(5, 10), "KB/S"); } else { m_DownLoadSpeed.text = RemoteFile.DownloadSpeed; } } } } private void DisplayHintContent() { var step = VersionUtility.Instance.step; switch (step) { case VersionUtility.Step.None: m_HintDescription.text = Language.GetFromLocal(4); break; case VersionUtility.Step.ApkExist: m_HintDescription.text = Language.GetFromLocal(5); break; case VersionUtility.Step.DownLoadPrepared: if (Application.platform == RuntimePlatform.Android) { var totalSize = VersionUtility.Instance.GetApkSize(); var sizeDescription = ((float)totalSize / DownLoadAndDiscompressTask.BYTE_PER_KILOBYTE).ToString("f1"); switch (Application.internetReachability) { case NetworkReachability.NotReachable: m_HintDescription.text = Language.GetFromLocal(6, sizeDescription); break; case NetworkReachability.ReachableViaCarrierDataNetwork: m_HintDescription.text = Language.GetFromLocal(7, sizeDescription); break; case NetworkReachability.ReachableViaLocalAreaNetwork: m_HintDescription.text = Language.GetFromLocal(8, sizeDescription); break; } } else if (Application.platform == RuntimePlatform.IPhonePlayer) { m_HintDescription.text = Language.GetFromLocal(9); } break; case VersionUtility.Step.DownLoad: m_HintDescription.text = Language.GetFromLocal(3); break; case VersionUtility.Step.DownLoadFailed: m_HintDescription.text = Language.GetFromLocal(10); break; } switch (step) { case VersionUtility.Step.ApkExist: case VersionUtility.Step.DownLoadPrepared: var updateContent = VersionUtility.Instance.GetUpdateContent(); if (string.IsNullOrEmpty(updateContent)) { m_UpdateContentContainer.gameObject.SetActive(false); } else { m_UpdateContentContainer.gameObject.SetActive(true); m_UpdateCotent.text = updateContent; } break; default: m_UpdateContentContainer.gameObject.SetActive(false); break; } } private void Confirm() { timer = 1f; var step = VersionUtility.Instance.step; switch (step) { case VersionUtility.Step.None: VersionUtility.Instance.RequestVersionCheck(); break; case VersionUtility.Step.ApkExist: SDKUtility.Instance.InstallAPK(VersionUtility.Instance.GetApkLocalUrl()); break; case VersionUtility.Step.DownLoadPrepared: switch (Application.platform) { case RuntimePlatform.Android: VersionUtility.Instance.StartDownLoad(); break; case RuntimePlatform.IPhonePlayer: Application.OpenURL(VersionUtility.Instance.GetApkRemoteUrl()); //打开应用商店链接 break; case RuntimePlatform.WindowsPlayer: //打开下载链接 Application.OpenURL(VersionUtility.Instance.GetApkRemoteUrl()); break; } break; case VersionUtility.Step.DownLoadFailed: VersionUtility.Instance.StartDownLoad(); break; } } } }