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("loadSlider"); Slider_Text = GetControl("%"); image = GetControl("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(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(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); // } //} }