CultivationOfBrewing/Assets/Scripts/UI/UIPanel/UI_LoadingPanel.cs

104 lines
2.6 KiB
C#

using Cysharp.Threading.Tasks;
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class UI_LoadingPanel : BasePanel
{
public Slider loadSlider;
public CanvasGroup canvasGroup;
private float currentProgress = 0;
public float speed;
public bool isLoading = false;
public TextMeshProUGUI Slider_Text;
public Sprite[] frames; // 每一帧的Sprite图片
private Image image;
public int index = 0;
protected override void Awake()
{
base.Awake();
//canvasGroup.alpha = 0;
loadSlider = GetControl<Slider>("loadSlider");
Slider_Text = GetControl<TextMeshProUGUI>("%");
image = GetControl<Image>("middleImage"); // 循环播放
}
public override void ShowMe(int time = 0)
{
base.ShowMe(); // 循环播放
index = 0;
Slider_Text.text = "0%";
// await ToolManager.CanvasFadeIn(canvasGroup, 0.5f);
Bootstrap.Instance.eventCenter.AddEventListener<float>(Enum_EventType.UpdateProgress, UpdateProgress);
}
public override void HideMe(int time = 0)
{
base.HideMe();
loadSlider.value = 0;
currentProgress = 0;
speed = 0;
//await ToolManager.CanvasFadeOut(canvasGroup, 0.5f);
Bootstrap.Instance.eventCenter.RemoveEventListener<float>(Enum_EventType.UpdateProgress, UpdateProgress);
}
private void Update()
{
if (isLoading)
{
if (currentProgress < 0.98f)
{
currentProgress += Time.deltaTime * speed;
loadSlider.value = currentProgress;
float percent = loadSlider.value * 100f;
Slider_Text.text = Mathf.RoundToInt(percent) + "%";
}
else
{
isLoading = false;
HideMe();
}
}
}
public async UniTask Loading()
{
while (isLoading)
{
index++;
//Debug.Log(index+"啊啊啊啊啊啊啊啊啊");
await UniTask.Delay(50);
if (index >= frames.Length)
{
index = 0;
}
image.sprite = frames[index];
}
}
private async void UpdateProgress(float speed)
{
isLoading = true;
this.speed = speed;
await Loading();
}
//private IEnumerator Animate()
//{
// for (int i = 0; i < frames.Length; i++)
// {
// image.sprite = frames[i];
// yield return new WaitForSeconds(1f / frameRate);
// }
//}
}