Files
SAMKWANG/Assets/Scripts/BranchSelection/UI/Panel_Loading.cs
2025-10-21 18:47:48 +09:00

111 lines
3.0 KiB
C#

using UnityEngine;
using WI;
using UnityEngine.UI;
using System.Collections;
using TMPro;
using System;
namespace Samkwang
{
public class Panel_Loading : MonoBehaviour
{
public CanvasGroup logoRoot;
public Image arcImage;
public GameObject loadingBarRoot;
public Slider slider_LoadingProgress;
public TMP_Text Text_Loading;
public float fadeOutDuration = 0.8f;
public float simulationDuration = 4f;
private static bool hasPlayedOnce = false;
Coroutine loadingCoroutine;
public Action onLoadingComplete;
void Awake()
{
logoRoot = transform.GetComponentInChildren<CanvasGroup>();
arcImage = logoRoot.GetComponentInChildren<Image>();
loadingBarRoot = transform.Find("LoadingBar").gameObject;
slider_LoadingProgress = loadingBarRoot.GetComponentInChildren<Slider>();
Text_Loading = loadingBarRoot.GetComponentInChildren<TextMeshProUGUI>();
transform.SetAsLastSibling();
logoRoot.alpha = 1f;
arcImage.fillAmount = 0f;
}
void Start()
{
if (hasPlayedOnce)
{
gameObject.SetActive(false);
return;
}
hasPlayedOnce = true;
StartLoadingRoutine();
}
public void StartLoadingRoutine()
{
if (loadingCoroutine != null) StopCoroutine(loadingCoroutine);
loadingCoroutine = StartCoroutine(RunLoadingRoutine());
}
IEnumerator RunLoadingRoutine()
{
yield return new WaitForSeconds(0.3f);
float elapsed = 0f;
while (elapsed < simulationDuration)
{
elapsed += Time.deltaTime;
float progress = Mathf.Clamp01(elapsed / simulationDuration);
UpdateProgress(progress);
RotateArc();
yield return null;
}
UpdateProgress(1f);
arcImage.fillAmount = 1f;
loadingBarRoot.SetActive(false);
yield return new WaitForSeconds(0.2f);
yield return StartCoroutine(FadeOutAndDisable());
}
void UpdateProgress(float progress01)
{
slider_LoadingProgress.value = progress01;
Text_Loading.SetText($"Loading..({(progress01 * 100f).ToString("F0")}%)");
}
void RotateArc()
{
arcImage.fillAmount = arcImage.fillAmount < 1 ? arcImage.fillAmount += Time.deltaTime * 0.5f : 0;
}
IEnumerator FadeOutAndDisable()
{
float t = 0f;
float start = logoRoot.alpha;
while (t < fadeOutDuration)
{
t += Time.deltaTime;
logoRoot.alpha = Mathf.Lerp(start, 0f, t / fadeOutDuration);
yield return null;
}
logoRoot.alpha = 0f;
gameObject.SetActive(false);
onLoadingComplete?.Invoke();
}
}
}