NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/HotUpdate/UI_VersionUpdate.cs

61 lines
1.6 KiB
C#
Raw Normal View History

2023-10-19 15:00:19 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.AddressableAssets.ResourceLocators;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.UI;
2023-11-08 19:04:23 +08:00
public class UI_VersionUpdate : MonoBehaviour
2023-10-19 15:00:19 +08:00
{
private Slider _slider;
private TMP_Text _textProgress;
private AsyncOperationHandle _asyncOperation;
2023-11-08 19:04:23 +08:00
private void Awake()
2023-10-19 15:00:19 +08:00
{
2023-11-08 19:04:23 +08:00
_slider = transform.Find("Slider").GetComponent<Slider>();
_textProgress = transform.Find("Text_progress").GetComponent<TMP_Text>();
2023-10-19 15:00:19 +08:00
}
2023-11-08 19:04:23 +08:00
public void SetParams(AsyncOperationHandle op)
2023-10-19 15:00:19 +08:00
{
2023-11-08 19:04:23 +08:00
_asyncOperation = op;
2023-10-19 15:00:19 +08:00
}
private void Update()
{
Refresh();
}
private void Refresh()
{
if (_asyncOperation.IsValid())
{
var downloadStatus = _asyncOperation.GetDownloadStatus();
_textProgress.text = $"{string.Format("{0:F1}", downloadStatus.Percent * 100)}% {ConvertBytes(downloadStatus.DownloadedBytes)}/{ConvertBytes(downloadStatus.TotalBytes)}";
_slider.value = downloadStatus.Percent;
}
}
private string ConvertBytes(long bytes)
{
if (bytes < 1024)
{
return $"{bytes}B";
}
else if (bytes < 1024 * 1024)
{
return string.Format("{0:f1}KB", bytes / 1024f);
}
else if (bytes < 1024 * 1024 * 1024)
{
return string.Format("{0:f1}MB", bytes / (1024f * 1024f));
}
else
{
return string.Format("{0:f1}GB", bytes / (1024f * 1024f * 1024f));
}
}
}