NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/UI/UI_LoadingController.cs

146 lines
3.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using TMPro;
using Image = UnityEngine.UI.Image;
using System;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using Gameplay.SubSystems;
using UnityEngine;
using Framework;
using Gameplay;
public class UI_LoadingController : UIWindow
{
//UI GameObj
///Auto Gen Start///
public Image Image_BG;
public TMP_Text Text_InfoTitle;
public TMP_Text Text_Info;
public TMP_Text Text_Process;
public TMP_Text Text_Capacity;
public Image Image_ProcessBar;
///Auto Gen End///
// Use this for initialization
//------------------------- 进度控制
private float lastUpdate = 0f;
private float displayProgress = 0f;
private bool loadResEnd;
private Action onLoadingEnd;
int index = 0;
public override async void OnAwake()
{
//bind UI GameObj
UI_LoadingBinder.GetComponents(this);
await LoadingUILocalize.Instance.Init();
}
//invoke when open window
protected override void OnOpenWindow(object data = null)
{
}
//invoke when reload window
protected override void OnReloadWindow(object data = null)
{
}
private int GetIndexPlusOne(int i)
{
i++;
if(i< LoadingUILocalize.Instance.Config.DataList.Count)
{
return i;
}
return 0;
}
float refreshTime = 3f;
float nowTime = 0;
void Update()
{
if (nowTime >= refreshTime)
{
nowTime = 0;
RefreshJieShao();
index= GetIndexPlusOne(index);
}
else
{
nowTime += Time.deltaTime;
}
var currentExcutor = LoadingExecutorManager.Instance.CurrentExcutor;
if (currentExcutor != null)
{
LoadingText();
if (displayProgress < currentExcutor.DestProgress)
{
float diffValue = currentExcutor.DestProgress - displayProgress;
float deltaValue = 0.02f;
if (loadResEnd) //资源文件下载完后进度条最小走1
deltaValue = Mathf.Max(1.0f, diffValue * 0.1f);
else if (diffValue < 0.5f) //资源下载过程中进度条精确到小数点后2位
deltaValue = Mathf.Max(0.02f, diffValue * 0.1f);
else
deltaValue = Mathf.Max(0.1f, diffValue * 0.1f);
displayProgress += deltaValue;
displayProgress = Mathf.Min(100f, displayProgress);
SetLoadingPrecentage(displayProgress);
}
}
}
private void RefreshJieShao()
{
string[] alltext = LoadingUILocalize.Instance.GetTextByIndex(index).Split("/");
Text_InfoTitle.text =alltext[0] ;
Text_Info.text = alltext[1];
}
private void LoadingText()
{
const float EPSINON = 0.000001f;
if (Math.Abs(lastUpdate) < EPSINON)
{
SetLoadingPrecentage(0f);
}
if (Math.Abs(lastUpdate) < EPSINON || Time.unscaledTime > (lastUpdate + 5f))
{
lastUpdate = Time.unscaledTime;
}
}
private async Task SetLoadingPrecentage(float num)
{
float x = num ;
Text_Process.text = x.ToString("f0")+"%";
Image_ProcessBar.fillAmount = x*0.01f;
if (num >= 100f)
{
await UniTask.NextFrame();
onLoadingEnd?.Invoke();
HideWindow();
}
}
public void StartLoading(Action onEnd)
{
enabled = true;
displayProgress = 0;
Image_ProcessBar.fillAmount = 0f;
Text_Process.text = 0+"%";
onLoadingEnd = onEnd;
}
}