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

91 lines
2.2 KiB
C#

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<Slider>("loadSlider");
Slider_Text = GetControl<TextMeshProUGUI>("%");
image = GetControl<Image>("middleImage"); // 琦뻔꺄렴
}
public override void ShowMe()
{
base.ShowMe();
index = 0;
Slider_Text.text = "0%";
Bootstrap.Instance.eventCenter.AddEventListener<float>(Enum_EventType.UpdateProgress, UpdateProgress);
}
public override void HideMe()
{
base.HideMe();
loadSlider.value = 0;
currentProgress = 0;
speed = 0;
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();
}
}