using Cysharp.Threading.Tasks; using System; using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI; using static TMPro.SpriteAssetUtilities.TexturePacker_JsonArray; 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(); loadSlider = GetControl("loadSlider"); Slider_Text = GetControl("%"); image = GetControl("middleImage"); // Ñ­»·²¥·Å } public override void ShowMe() { base.ShowMe(); index = 0; Slider_Text.text = "0%"; Bootstrap.Instance.eventCenter.AddEventListener(Enum_EventType.UpdateProgress, UpdateProgress); } public override void HideMe() { base.HideMe(); loadSlider.value = 0; currentProgress = 0; speed = 0; 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(); } }