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

66 lines
2.0 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.AddressableAssets.ResourceLocators;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.UI;
public class UI_VersionUpdate : MonoBehaviour
{
private Slider _slider;
private TMP_Text _textProgress;
private AsyncOperationHandle _asyncOperation;
private float _currProgress;
private void Awake()
{
_slider = transform.Find("Slider").GetComponent<Slider>();
_textProgress = transform.Find("Text_progress").GetComponent<TMP_Text>();
}
public void SetParams(AsyncOperationHandle op)
{
_asyncOperation = op;
}
private void Update()
{
Refresh();
}
private void Refresh()
{
//Debug.Log("Refresh version Update UI");
if (_asyncOperation.IsValid())
{
var downloadStatus = _asyncOperation.GetDownloadStatus();
_currProgress = downloadStatus.Percent > _currProgress ? downloadStatus.Percent : _currProgress;
//Debug.Log($"Percent:{downloadStatus.Percent} DownloadedBytes:{downloadStatus.DownloadedBytes} TotalBytes:{downloadStatus.TotalBytes}");
_textProgress.text =
$"{string.Format("{0:F1}", _currProgress * 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));
}
}
}