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

91 lines
2.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class UI_LoadingPanel : BasePanel
{
public Slider loadSlider;
private float currentProgress = 0;
public float targetProgress;
public bool isLoading = false;
public TextMeshProUGUI Slider_Text;
public Sprite[] frames; // 将每一帧的Sprite图片依次拖入这个数组中
public float frameRate = 30f; // 每秒播放的帧数
private Image image;
protected override void Awake()
{
base.Awake();
loadSlider = GetControl<Slider>("loadSlider");
Slider_Text = GetControl<TextMeshProUGUI>("%");
image = GetControl<Image>("middleImage");
if (image == null)
{
Debug.LogError("Image component not found!");
}
StartCoroutine(Animate());
}
public override void ShowMe()
{
base.ShowMe();
loadSlider.value = 0;
Slider_Text.text = "0%";
Debug.Log("UI_LoadingPanel ShowMe");
Bootstrap.Instance.eventCenter.AddEventListener<float>(Enum_EventType.UpdateProgress, UpdateProgress);
}
public override void HideMe()
{
base.HideMe();
Debug.Log("UI_LoadingPanel HideMe");
Bootstrap.Instance.eventCenter.RemoveEventListener<float>(Enum_EventType.UpdateProgress, UpdateProgress);
}
private void Update()
{
if (isLoading)
{
if (currentProgress < targetProgress)
{
currentProgress += Time.deltaTime;
if (currentProgress >= targetProgress)
currentProgress = targetProgress;
loadSlider.value = currentProgress;
// 将Slider的value0~1转为百分比0%~100%
float percent = loadSlider.value * 100f;
Slider_Text.text = Mathf.RoundToInt(percent) + "%";
}
else
{
isLoading = false;
Bootstrap.Instance.uiManager.HidePanel<UI_LoadingPanel>();
}
}
}
private void UpdateProgress(float progress)
{
isLoading = true;
targetProgress += progress;
}
private IEnumerator Animate()
{
for (int i = 0; i < frames.Length; i++)
{
image.sprite = frames[i];
yield return new WaitForSeconds(1f / frameRate);
}
// 你可以在这里添加循环播放的代码,或者在特定条件下停止动画
StartCoroutine(Animate()); // 循环播放
}
}