77 lines
2.8 KiB
C#
77 lines
2.8 KiB
C#
|
|
using System;
|
|||
|
|
using AlicizaX.Resource.Runtime;
|
|||
|
|
using AlicizaX.Event.Runtime;
|
|||
|
|
using AlicizaX.Runtime;
|
|||
|
|
using UnityEngine;
|
|||
|
|
|
|||
|
|
|
|||
|
|
namespace Unity.Startup.Procedure
|
|||
|
|
{
|
|||
|
|
public static class LauncherUIHandler
|
|||
|
|
{
|
|||
|
|
public static void Start()
|
|||
|
|
{
|
|||
|
|
GameApp.Event.Subscribe(AssetDownloadProgressUpdateEventArgs.EventId, SetProgressUpdate);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static void Dispose()
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static void ShowLogText(string text)
|
|||
|
|
{
|
|||
|
|
Log.Info("TipText:{0}", text);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static float _lastUpdateDownloadedSize;
|
|||
|
|
private static float _totalSpeed;
|
|||
|
|
private static int _speedSampleCount;
|
|||
|
|
private static long _currentDownloadBytes;
|
|||
|
|
|
|||
|
|
private static float CurrentSpeed
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
float interval = Math.Max(Time.deltaTime, 0.01f); // 防止deltaTime过小
|
|||
|
|
var sizeDiff = _currentDownloadBytes - _lastUpdateDownloadedSize;
|
|||
|
|
_lastUpdateDownloadedSize = _currentDownloadBytes;
|
|||
|
|
var speed = sizeDiff / interval;
|
|||
|
|
|
|||
|
|
// 使用滑动窗口计算平均速度
|
|||
|
|
_totalSpeed += speed;
|
|||
|
|
_speedSampleCount++;
|
|||
|
|
return _totalSpeed / _speedSampleCount;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static void SetProgressUpdate(object sender, GameEventArgs gameEventArgs)
|
|||
|
|
{
|
|||
|
|
var message = (AssetDownloadProgressUpdateEventArgs)gameEventArgs;
|
|||
|
|
_currentDownloadBytes = message.CurrentDownloadSizeBytes;
|
|||
|
|
float progress = message.CurrentDownloadSizeBytes / (message.TotalDownloadSizeBytes * 1f);
|
|||
|
|
string currentSizeMb = Utility.File.GetBytesSize(message.CurrentDownloadSizeBytes);
|
|||
|
|
string totalSizeMb = Utility.File.GetBytesSize(message.TotalDownloadSizeBytes);
|
|||
|
|
string speed = Utility.File.GetLengthString((int)CurrentSpeed);
|
|||
|
|
|
|||
|
|
|
|||
|
|
string line1 = Utility.Text.Format("正在更新,已更新 {0}/{1} ({2:F2}%)", message.CurrentDownloadCount, message.TotalDownloadCount, progress);
|
|||
|
|
string line2 = Utility.Text.Format("已更新大小 {0}MB/{1}MB", currentSizeMb, totalSizeMb);
|
|||
|
|
string line3 = Utility.Text.Format("当前网速 {0}/s,剩余时间 {1}", speed, GetRemainingTime(message.TotalDownloadSizeBytes, message.CurrentDownloadSizeBytes, CurrentSpeed));
|
|||
|
|
|
|||
|
|
|
|||
|
|
Log.Info($"{line1} \n {line2}\n {line3}");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static string GetRemainingTime(long totalBytes, long currentBytes, float speed)
|
|||
|
|
{
|
|||
|
|
int needTime = 0;
|
|||
|
|
if (speed > 0)
|
|||
|
|
{
|
|||
|
|
needTime = (int)((totalBytes - currentBytes) / speed);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
TimeSpan ts = new TimeSpan(0, 0, needTime);
|
|||
|
|
return ts.ToString(@"mm\:ss");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|