hch
2025-12-04 7ceccc591077b348613dcd1880408b82e2a183d4
Main/System/AssetVersion/InGameDownLoadWin.cs
@@ -10,196 +10,205 @@
using UnityEngine.UI;
    public class InGameDownLoadWin : UIBase
public class InGameDownLoadWin : UIBase
{
    [SerializeField] RectTransform m_ContainerHint;
    [SerializeField] ItemCell[] items;
    [SerializeField] RectTransform[] m_RewardGotSigns;
    [SerializeField] RichText m_Content;
    [SerializeField] Button m_StartDownLoad;
    [SerializeField] Button m_PauseDownLoad;
    [SerializeField] Button m_Award;
    [SerializeField] Transform m_ContainerProgress;
    [SerializeField] SmoothSlider m_ProgressSlider;
    [SerializeField] Text m_Progress;
    [SerializeField] Text m_DownLoadSpeed;
    // [SerializeField] ToggleButton downLoadGo; //加速下载
    float timer = 1f;
    #region Built-in
    protected override void InitComponent()
    {
        [SerializeField] RectTransform m_RewardGotSign;
        // [SerializeField] RewardPreviewGroup m_RewardGroup;
        [SerializeField] RectTransform m_ContainerHint;
        [SerializeField] RichText m_Content;
        [SerializeField] Button m_StartDownLoad;
        [SerializeField] Button m_PauseDownLoad;
        [SerializeField] Button m_Award;
        [SerializeField] Button m_Close;
        m_StartDownLoad.AddListener(StartDownLoad);
        m_PauseDownLoad.AddListener(PauseDownLoad);
        m_Award.AddListener(Award);
        // downLoadGo.SetListener(() => {
        //     downLoadGo.isOn = !downLoadGo.isOn;
        //     InGameDownLoad.Instance.downLoadGo = downLoadGo.isOn;
        //     DownloadHotMgr.MaxDownLoadTask = InGameDownLoad.Instance.GetMaxTask();
        // });
    }
        [SerializeField] Transform m_ContainerProgress;
        [SerializeField] SmoothSlider m_ProgressSlider;
        [SerializeField] Text m_Progress;
        [SerializeField] Text m_DownLoadSpeed;
        [SerializeField] ToggleButton downLoadGo; //加速下载
        float timer = 1f;
        #region Built-in
        protected override void InitComponent()
    protected override void OnPreOpen()
    {
        InGameDownLoad.Instance.ParseRewardConfig();
        timer = 1f;
        for (int i = 0;  i < items.Length; i++)
        {
            m_StartDownLoad.AddListener(StartDownLoad);
            m_PauseDownLoad.AddListener(PauseDownLoad);
            m_Award.AddListener(Award);
            m_Close.AddListener(CloseWindow);
            downLoadGo.SetListener(() => {
                downLoadGo.isOn = !downLoadGo.isOn;
                InGameDownLoad.Instance.downLoadGo = downLoadGo.isOn;
                DownloadHotMgr.MaxDownLoadTask = InGameDownLoad.Instance.GetMaxTask();
            });
        }
        protected override void OnPreOpen()
        {
            timer = 1f;
            m_RewardGotSign.SetActive(InGameDownLoad.Instance.hasReward);
            // var items = new List<Item>();
            // foreach (var reward in InGameDownLoad.Instance.rewards)
            // {
            //     items.Add(new Item(reward.id, reward.count));
            // }
            // m_RewardGroup.Display(items);
            OnDownLoadStepChange(InGameDownLoad.Instance.state);
            downLoadGo.isOn = InGameDownLoad.Instance.downLoadGo;
        }
        protected override void OnOpen()
        {
            InGameDownLoad.Instance.downLoadStateChangeEvent += OnDownLoadStepChange;
        }
        protected override void OnPreClose()
        {
            InGameDownLoad.Instance.downLoadStateChangeEvent -= OnDownLoadStepChange;
        }
        protected override void OnClose()
        {
        }
        #endregion
        private void OnDownLoadStepChange(InGameDownLoad.State _step)
        {
            m_Award.SetActive(_step == InGameDownLoad.State.Award);
            m_PauseDownLoad.SetActive(_step == InGameDownLoad.State.DownLoad);
            m_StartDownLoad.SetActive(_step == InGameDownLoad.State.Prepared || _step == InGameDownLoad.State.Pause);
            m_ContainerProgress.SetActive(_step == InGameDownLoad.State.DownLoad || _step == InGameDownLoad.State.Pause);
            if (_step != InGameDownLoad.State.DownLoad)
            if (i < InGameDownLoad.Instance.rewards.Length)
            {
                m_DownLoadSpeed.text = string.Empty;
            }
            if (_step == InGameDownLoad.State.Prepared || _step == InGameDownLoad.State.Award)
            {
                m_ContainerHint.SetActive(true);
                DisplayHintContent();
                items[i].SetActive(true);
                int itemID = InGameDownLoad.Instance.rewards[i][0];
                items[i].Init(new ItemCellModel(InGameDownLoad.Instance.rewards[i][0], false, InGameDownLoad.Instance.rewards[i][1]));
                items[i].button.AddListener(() => {
                    ItemTipUtility.Show(itemID);
                });
                m_RewardGotSigns[i].SetActive(InGameDownLoad.Instance.hasReward);
            }
            else
            {
                m_ContainerHint.SetActive(false);
                items[i].SetActive(false);
            }
        }
        protected void LateUpdate()
        {
            var step = InGameDownLoad.Instance.state;
            if (step == InGameDownLoad.State.DownLoad)
            {
                timer += Time.deltaTime;
                if (timer > 1f)
                {
                    timer -= 1f;
                    m_ProgressSlider.value = InGameDownLoad.Instance.progress;
                    var totalSizeString = ((float)InGameDownLoad.Instance.showTotalSize / InGameDownLoad.BYTE_PER_MILLIONBYTE).ToString("f1");
                    var downLoadedSize = Mathf.Clamp(InGameDownLoad.Instance.showDownLoadedSize, 0, InGameDownLoad.Instance.showTotalSize - 1);
                    var downLoadedSizeString = ((float)downLoadedSize / InGameDownLoad.BYTE_PER_MILLIONBYTE).ToString("f1");
                    m_Progress.text = Language.GetFromLocal(13, StringUtility.Contact(downLoadedSizeString, "M", "/", totalSizeString, "M"));
        OnDownLoadStepChange(InGameDownLoad.Instance.state);
        // downLoadGo.isOn = InGameDownLoad.Instance.downLoadGo;
    }
                    if (InGameDownLoad.Instance.showDownLoadedSize >= InGameDownLoad.Instance.showTotalSize)
                    {
                        m_DownLoadSpeed.text = StringUtility.Contact(UnityEngine.Random.Range(5, 10), "KB/S");
                    }
                    else
                    {
                        m_DownLoadSpeed.text = DownloadHotMgr.Instance.SpeedFormat;
                    }
    protected override void OnOpen()
    {
        InGameDownLoad.Instance.downLoadStateChangeEvent += OnDownLoadStepChange;
    }
    protected override void OnPreClose()
    {
        InGameDownLoad.Instance.downLoadStateChangeEvent -= OnDownLoadStepChange;
    }
    protected override void OnClose()
    {
    }
    #endregion
    private void OnDownLoadStepChange(InGameDownLoad.State _step)
    {
        m_Award.SetActive(_step == InGameDownLoad.State.Award && !InGameDownLoad.Instance.hasReward);
        m_PauseDownLoad.SetActive(_step == InGameDownLoad.State.DownLoad);
        m_StartDownLoad.SetActive(_step == InGameDownLoad.State.Prepared || _step == InGameDownLoad.State.Pause);
        m_ContainerProgress.SetActive(_step == InGameDownLoad.State.DownLoad || _step == InGameDownLoad.State.Pause);
        if (_step != InGameDownLoad.State.DownLoad)
        {
            m_DownLoadSpeed.text = string.Empty;
        }
        if (_step == InGameDownLoad.State.Prepared || _step == InGameDownLoad.State.Award)
        {
            m_ContainerHint.SetActive(true);
            DisplayHintContent();
        }
        else
        {
            m_ContainerHint.SetActive(false);
        }
    }
    protected void LateUpdate()
    {
        var step = InGameDownLoad.Instance.state;
        if (step == InGameDownLoad.State.DownLoad)
        {
            timer += Time.deltaTime;
            if (timer > 1f)
            {
                timer -= 1f;
                m_ProgressSlider.value = InGameDownLoad.Instance.progress;
                var totalSizeString = ((float)InGameDownLoad.Instance.showTotalSize / InGameDownLoad.BYTE_PER_MILLIONBYTE).ToString("f1");
                var downLoadedSize = Mathf.Clamp(InGameDownLoad.Instance.showDownLoadedSize, 0, InGameDownLoad.Instance.showTotalSize - 1);
                var downLoadedSizeString = ((float)downLoadedSize / InGameDownLoad.BYTE_PER_MILLIONBYTE).ToString("f1");
                m_Progress.text = Language.GetFromLocal(13, StringUtility.Contact(downLoadedSizeString, "M", "/", totalSizeString, "M"));
                if (InGameDownLoad.Instance.showDownLoadedSize >= InGameDownLoad.Instance.showTotalSize)
                {
                    m_DownLoadSpeed.text = StringUtility.Contact(UnityEngine.Random.Range(5, 10), "KB/S");
                }
                else
                {
                    m_DownLoadSpeed.text = DownloadHotMgr.Instance.SpeedFormat;
                }
            }
        }
        private void DisplayHintContent()
        {
            var step = InGameDownLoad.Instance.state;
            switch (step)
            {
                case InGameDownLoad.State.Prepared:
                    var totalCount = InGameDownLoad.Instance.showTotalCount;
                    var totalSize = InGameDownLoad.Instance.showTotalSize;
                    if (totalSize > InGameDownLoad.BYTE_PER_MILLIONBYTE)
                    {
                        var sizeDescription = ((float)totalSize / InGameDownLoad.BYTE_PER_MILLIONBYTE).ToString("f1");
                        m_Content.text = Language.GetFromLocal(19, sizeDescription);
                    }
                    else
                    {
                        var sizeDescription = ((float)totalSize / InGameDownLoad.BYTE_PER_KILOBYTE).ToString("f1");
                        m_Content.text = Language.GetFromLocal(20, sizeDescription);
                    }
                    break;
                case InGameDownLoad.State.DownLoad:
                    m_Content.text = Language.GetFromLocal(3);
                    break;
                case InGameDownLoad.State.Pause:
                    m_Content.text = Language.GetFromLocal(21);
                    break;
                case InGameDownLoad.State.Award:
                    m_Content.text = Language.GetFromLocal(25);
                    break;
            }
        }
        private void StartDownLoad()
        {
            timer = 1f;
            switch (InGameDownLoad.Instance.state)
            {
                case InGameDownLoad.State.Prepared:
                case InGameDownLoad.State.Pause:
                    if (Application.internetReachability == NetworkReachability.NotReachable)
                    {
                        ServerTipDetails.DisplayNormalTip(Language.GetFromLocal(24));
                    }
                    else
                    {
                        InGameDownLoad.Instance.StartDownLoad();
                        CloseWindow();
                    }
                    break;
                default:
                    CloseWindow();
                    break;
            }
        }
        private void PauseDownLoad()
        {
            timer = 1f;
            switch (InGameDownLoad.Instance.state)
            {
                case InGameDownLoad.State.DownLoad:
                    InGameDownLoad.Instance.Pause();
                    break;
                default:
                    break;
            }
        }
        private void Award()
        {
            InGameDownLoad.Instance.RequestDownLoadReward(true);
            UIManager.Instance.CloseWindow<InGameDownLoadWin>();
        }
    }
    private void DisplayHintContent()
    {
        var step = InGameDownLoad.Instance.state;
        switch (step)
        {
            case InGameDownLoad.State.Prepared:
                var totalCount = InGameDownLoad.Instance.showTotalCount;
                var totalSize = InGameDownLoad.Instance.showTotalSize;
                if (totalSize > InGameDownLoad.BYTE_PER_MILLIONBYTE)
                {
                    var sizeDescription = ((float)totalSize / InGameDownLoad.BYTE_PER_MILLIONBYTE).ToString("f1");
                    m_Content.text = Language.GetFromLocal(19, sizeDescription);
                }
                else
                {
                    var sizeDescription = ((float)totalSize / InGameDownLoad.BYTE_PER_KILOBYTE).ToString("f1");
                    m_Content.text = Language.GetFromLocal(20, sizeDescription);
                }
                break;
            case InGameDownLoad.State.DownLoad:
                m_Content.text = Language.GetFromLocal(3);
                break;
            case InGameDownLoad.State.Pause:
                m_Content.text = Language.GetFromLocal(21);
                break;
            case InGameDownLoad.State.Award:
                m_Content.text = Language.GetFromLocal(25);
                break;
        }
    }
    private void StartDownLoad()
    {
        timer = 1f;
        switch (InGameDownLoad.Instance.state)
        {
            case InGameDownLoad.State.Prepared:
            case InGameDownLoad.State.Pause:
                if (Application.internetReachability == NetworkReachability.NotReachable)
                {
                    ServerTipDetails.DisplayNormalTip(Language.GetFromLocal(24));
                }
                else
                {
                    InGameDownLoad.Instance.StartDownLoad();
                    CloseWindow();
                }
                break;
            default:
                CloseWindow();
                break;
        }
    }
    private void PauseDownLoad()
    {
        timer = 1f;
        switch (InGameDownLoad.Instance.state)
        {
            case InGameDownLoad.State.DownLoad:
                InGameDownLoad.Instance.Pause();
                break;
            default:
                break;
        }
    }
    private void Award()
    {
        InGameDownLoad.Instance.RequestDownLoadReward();
        UIManager.Instance.CloseWindow<InGameDownLoadWin>();
    }
}