[opt] 优化下载更新界面的显示

This commit is contained in:
chengshengfei
2024-05-28 15:56:05 +08:00
parent 7914660f8c
commit c84c21574d
3 changed files with 56 additions and 41 deletions

View File

@@ -175,11 +175,11 @@ MonoBehaviour:
m_SelectOnRight: {fileID: 0} m_SelectOnRight: {fileID: 0}
m_Transition: 1 m_Transition: 1
m_Colors: m_Colors:
m_NormalColor: {r: 0.9450981, g: 0.6392157, b: 0.25490198, a: 1} m_NormalColor: {r: 1, g: 1, b: 1, a: 1}
m_HighlightedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1} m_HighlightedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1} m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1}
m_SelectedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1} m_SelectedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
m_DisabledColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 0.5019608} m_DisabledColor: {r: 0.94509804, g: 0.6392157, b: 0.25490198, a: 1}
m_ColorMultiplier: 1 m_ColorMultiplier: 1
m_FadeDuration: 0.1 m_FadeDuration: 0.1
m_SpriteState: m_SpriteState:
@@ -193,12 +193,12 @@ MonoBehaviour:
m_PressedTrigger: Pressed m_PressedTrigger: Pressed
m_SelectedTrigger: Selected m_SelectedTrigger: Selected
m_DisabledTrigger: Disabled m_DisabledTrigger: Disabled
m_Interactable: 1 m_Interactable: 0
m_TargetGraphic: {fileID: 4296679187938450242} m_TargetGraphic: {fileID: 4296679187938450242}
m_HandleRect: {fileID: 3254196395727856927} m_HandleRect: {fileID: 3254196395727856927}
m_Direction: 0 m_Direction: 0
m_Value: 0 m_Value: 0
m_Size: 1 m_Size: 0
m_NumberOfSteps: 0 m_NumberOfSteps: 0
m_OnValueChanged: m_OnValueChanged:
m_PersistentCalls: m_PersistentCalls:

View File

@@ -5,11 +5,11 @@ using Version = TEngine.Version;
namespace GameMain namespace GameMain
{ {
[Window(UILayer.UI, fromResources: true, location: "AssetLoad/UILoadUpdate",fullScreen:true)] [Window(UILayer.UI, fromResources: true, location: "AssetLoad/UILoadUpdate", fullScreen: true)]
public class UILoadUpdate : UIWindow public class UILoadUpdate : UIWindow
{ {
private Scrollbar m_scrollbarProgress; private Scrollbar m_scrollbarProgress;
#region #region
private Image m_imgBackGround; private Image m_imgBackGround;
private Text m_textDesc; private Text m_textDesc;
@@ -41,12 +41,16 @@ namespace GameMain
protected override void RegisterEvent() protected override void RegisterEvent()
{ {
base.RegisterEvent(); base.RegisterEvent();
AddUIEvent(RuntimeId.ToRuntimeId("RefreshVersion"),RefreshVersion); AddUIEvent(RuntimeId.ToRuntimeId("RefreshVersion"), RefreshVersion);
} }
protected override void OnRefresh() protected override void OnRefresh()
{ {
base.OnRefresh(); base.OnRefresh();
if (base.userDatas.Length > 0 && base.userDatas[0] != null)
{
m_textDesc.text = base.userDatas[0].ToString(); //获取更新内容由ProcedureDownloadFile -> OnDownloadProgressCallback 传递
}
} }
#region #region

View File

@@ -7,36 +7,43 @@ using Utility = TEngine.Utility;
namespace GameMain namespace GameMain
{ {
public class ProcedureDownloadFile:ProcedureBase public class ProcedureDownloadFile : ProcedureBase
{ {
public override bool UseNativeDialog { get; } public override bool UseNativeDialog { get; }
private ProcedureOwner _procedureOwner; private ProcedureOwner _procedureOwner;
private float _lastUpdateDownloadedSize; private float _lastUpdateDownloadedSize;
private float _totalSpeed;
private int _speedSampleCount;
private float CurrentSpeed private float CurrentSpeed
{ {
get get
{ {
float interval = GameTime.deltaTime; float interval = Math.Max(GameTime.deltaTime, 0.01f); // 防止deltaTime过小
var sizeDiff = GameModule.Resource.Downloader.CurrentDownloadBytes - _lastUpdateDownloadedSize; var sizeDiff = GameModule.Resource.Downloader.CurrentDownloadBytes - _lastUpdateDownloadedSize;
_lastUpdateDownloadedSize = GameModule.Resource.Downloader.CurrentDownloadBytes; _lastUpdateDownloadedSize = GameModule.Resource.Downloader.CurrentDownloadBytes;
var speed = (float)Math.Floor(sizeDiff / interval); var speed = sizeDiff / interval;
return speed;
// 使用滑动窗口计算平均速度
_totalSpeed += speed;
_speedSampleCount++;
return _totalSpeed / _speedSampleCount;
} }
} }
protected override void OnEnter(ProcedureOwner procedureOwner) protected override void OnEnter(ProcedureOwner procedureOwner)
{ {
_procedureOwner = procedureOwner; _procedureOwner = procedureOwner;
Log.Info("开始下载更新文件!"); Log.Info("开始下载更新文件!");
UILoadMgr.Show(UIDefine.UILoadUpdate,$"开始下载更新文件..."); UILoadMgr.Show(UIDefine.UILoadUpdate, "开始下载更新文件...");
BeginDownload().Forget(); BeginDownload().Forget();
} }
private async UniTaskVoid BeginDownload() private async UniTaskVoid BeginDownload()
{ {
var downloader = GameModule.Resource.Downloader; var downloader = GameModule.Resource.Downloader;
@@ -57,35 +64,39 @@ namespace GameMain
private void OnDownloadErrorCallback(string fileName, string error) private void OnDownloadErrorCallback(string fileName, string error)
{ {
UILoadTip.ShowMessageBox($"Failed to download file : {fileName}", MessageShowType.TwoButton, UILoadTip.ShowMessageBox($"Failed to download file : {fileName}", MessageShowType.TwoButton,
LoadStyle.StyleEnum.Style_Default LoadStyle.StyleEnum.Style_Default,
, () => { ChangeState<ProcedureCreateDownloader>(_procedureOwner); }, UnityEngine.Application.Quit); () => { ChangeState<ProcedureCreateDownloader>(_procedureOwner); }, UnityEngine.Application.Quit);
} }
private void OnDownloadProgressCallback(int totalDownloadCount, int currentDownloadCount, long totalDownloadBytes, long currentDownloadBytes) private void OnDownloadProgressCallback(int totalDownloadCount, int currentDownloadCount, long totalDownloadBytes, long currentDownloadBytes)
{ {
string currentSizeMb = (currentDownloadBytes / 1048576f).ToString("f1"); string currentSizeMb = (currentDownloadBytes / 1048576f).ToString("f1");
string totalSizeMb = (totalDownloadBytes / 1048576f).ToString("f1"); string totalSizeMb = (totalDownloadBytes / 1048576f).ToString("f1");
// UILoadMgr.Show(UIDefine.UILoadUpdate,$"{currentDownloadCount}/{totalDownloadCount} {currentSizeMb}MB/{totalSizeMb}MB"); float progressPercentage = GameModule.Resource.Downloader.Progress * 100;
string descriptionText = Utility.Text.Format("正在更新,已更新{0},总更新{1},已更新大小{2},总更新大小{3},更新进度{4},当前网速{5}/s", string speed = Utility.File.GetLengthString((int)CurrentSpeed);
currentDownloadCount.ToString(),
totalDownloadCount.ToString(),
Utility.File.GetByteLengthString(currentDownloadBytes),
Utility.File.GetByteLengthString(totalDownloadBytes),
GameModule.Resource.Downloader.Progress,
Utility.File.GetLengthString((int)CurrentSpeed));
LoadUpdateLogic.Instance.DownProgressAction?.Invoke(GameModule.Resource.Downloader.Progress);
UILoadMgr.Show(UIDefine.UILoadUpdate,descriptionText);
int needTime = 0; string line1 = Utility.Text.Format("正在更新,已更新 {0}/{1} ({2:F2}%)", currentDownloadCount, totalDownloadCount, progressPercentage);
if (CurrentSpeed > 0) string line2 = Utility.Text.Format("已更新大小 {0}MB/{1}MB", currentSizeMb, totalSizeMb);
{ string line3 = Utility.Text.Format("当前网速 {0}/s剩余时间 {1}", speed, GetRemainingTime(totalDownloadBytes, currentDownloadBytes, CurrentSpeed));
needTime = (int)((totalDownloadBytes - currentDownloadBytes) / CurrentSpeed);
} LoadUpdateLogic.Instance.DownProgressAction?.Invoke(GameModule.Resource.Downloader.Progress);
UILoadMgr.Show(UIDefine.UILoadUpdate, $"{line1}\n{line2}\n{line3}");
TimeSpan ts = new TimeSpan(0, 0, needTime);
string timeStr = ts.ToString(@"mm\:ss"); Log.Info($"{line1} {line2} {line3}");
string updateProgress = Utility.Text.Format("剩余时间 {0}({1}/s)", timeStr, Utility.File.GetLengthString((int)CurrentSpeed));
Log.Info(updateProgress);
} }
private 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");
}
} }
} }