AlicizaX/Client/Assets/Scripts/Startup/Framework/LauncherUIHandler.cs

116 lines
4.5 KiB
C#
Raw Normal View History

2025-01-23 19:06:48 +08:00
using System;
2025-04-28 19:45:45 +08:00
using System.Collections;
using System.Collections.Generic;
2025-01-23 19:06:48 +08:00
using AlicizaX.Resource.Runtime;
using AlicizaX.Event.Runtime;
2025-04-28 19:45:45 +08:00
using AlicizaX.Localization.Runtime;
using AlicizaX;
using Cysharp.Threading.Tasks;
// using Unity.Startup.UI;
2025-01-23 19:06:48 +08:00
using UnityEngine;
namespace Unity.Startup.Procedure
{
public static class LauncherUIHandler
{
2025-04-28 19:45:45 +08:00
// private static UILoadUpdateWindow _uiLoadUpdateWindow;
public static async UniTaskVoid Start()
2025-01-23 19:06:48 +08:00
{
2025-04-28 19:45:45 +08:00
// _uiLoadUpdateWindow = await GameApp.UI.ShowUIAsync<UILoadUpdateWindow>();
2025-01-23 19:06:48 +08:00
GameApp.Event.Subscribe(AssetDownloadProgressUpdateEventArgs.EventId, SetProgressUpdate);
2025-07-11 21:00:00 +08:00
// GameApp.Event.Subscribe(AssetPatchStatesChangeEventArgs.EventId, SetPatchStates);
2025-04-28 19:45:45 +08:00
await UniTask.CompletedTask;
// TextAsset config = Resources.Load<TextAsset>($"Localization/{AppBuilderSetting.Instance.Language.ToString()}");
// Dictionary<string, string> dic = Utility.Json.ToObject<Dictionary<string, string>>(config.text);
// GameApp.Localization.AddLocalizationConfig(dic);
// Resources.UnloadAsset(config);
2025-01-23 19:06:48 +08:00
}
public static void Dispose()
{
2025-04-28 19:45:45 +08:00
// GameApp.UI.CloseUI<UILoadUpdateWindow>();
2025-07-11 21:00:00 +08:00
// GameApp.Event.Unsubscribe(AssetPatchStatesChangeEventArgs.EventId, SetPatchStates);
2025-04-28 19:45:45 +08:00
GameApp.Event.Unsubscribe(AssetDownloadProgressUpdateEventArgs.EventId, SetProgressUpdate);
2025-01-23 19:06:48 +08:00
}
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;
}
}
2025-04-28 19:45:45 +08:00
public static IEnumerator StartProgressCoroutine(float duration)
{
float elapsed = 0f;
while (elapsed < duration)
{
float progress = elapsed / duration;
Progress(progress);
elapsed += Time.deltaTime;
yield return null;
}
Progress(1f);
}
private static void Progress(float v)
{
// _uiLoadUpdateWindow.SetProgressText(v );
}
private static void SetPatchStates(object sender, GameEventArgs gameEventArgs)
{
2025-07-11 21:00:00 +08:00
// var message = (AssetPatchStatesChangeEventArgs)gameEventArgs;
2025-04-28 19:45:45 +08:00
// _uiLoadUpdateWindow.SetDescText(GameApp.Localization.GetString(message.CurrentStates.ToString()));
2025-07-11 21:00:00 +08:00
// Debug.Log(message.CurrentStates);
2025-04-28 19:45:45 +08:00
}
2025-01-26 20:55:39 +08:00
private static void SetProgressUpdate(object sender, GameEventArgs gameEventArgs)
2025-01-23 19:06:48 +08:00
{
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");
}
}
2025-01-26 20:55:39 +08:00
}