147 lines
4.2 KiB
C#
147 lines
4.2 KiB
C#
using Cysharp.Threading.Tasks;
|
|
using System;
|
|
using System.Collections;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace OCTOPUS_TWIN
|
|
{
|
|
public class UILoading : MonoBehaviour
|
|
{
|
|
private static UILoading instance;
|
|
|
|
private const string PREFAB_PATH = "UI/Prefabs/Home/UILoading";
|
|
private float loadingDuration = 1.5f;
|
|
private float fadeSpeed = 2.0f;
|
|
|
|
private CanvasGroup logoCanvasGroup; // 로고의 CanvasGroup 연결
|
|
private Image progressBar;
|
|
private TextMeshProUGUI percentageText;
|
|
|
|
public static UILoading Instance
|
|
{
|
|
get
|
|
{
|
|
if (instance == null)
|
|
{
|
|
var obj = FindAnyObjectByType<UILoading>();
|
|
|
|
if (obj != null)
|
|
instance = obj;
|
|
else
|
|
instance = Create();
|
|
}
|
|
return instance;
|
|
}
|
|
}
|
|
|
|
private static UILoading Create()
|
|
{
|
|
return Instantiate(Resources.Load<UILoading>(PREFAB_PATH));
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance != this)
|
|
{
|
|
Destroy(gameObject);
|
|
|
|
return;
|
|
}
|
|
DontDestroyOnLoad(gameObject);
|
|
|
|
logoCanvasGroup = GetComponentInChildren<CanvasGroup>();
|
|
|
|
if (progressBar == null)
|
|
{
|
|
var imageTransform = transform.Find("Background/LoadingBarImage");
|
|
if (imageTransform != null)
|
|
{
|
|
progressBar = imageTransform.GetComponentInChildren<Image>();
|
|
}
|
|
else
|
|
{
|
|
// 경로로 못 찾으면 자식 전체에서 탐색
|
|
progressBar = GetComponentInChildren<Image>();
|
|
}
|
|
}
|
|
|
|
if (percentageText == null)
|
|
{
|
|
var textTransform = transform.Find("Background/PercentageText");
|
|
if (textTransform != null)
|
|
{
|
|
percentageText = textTransform.GetComponent<TextMeshProUGUI>();
|
|
}
|
|
else
|
|
{
|
|
// 경로로 못 찾으면 자식 전체에서 탐색
|
|
percentageText = GetComponentInChildren<TextMeshProUGUI>();
|
|
}
|
|
}
|
|
}
|
|
|
|
public async UniTaskVoid LoadProject(Action onFadeInComplete)
|
|
{
|
|
StopAllCoroutines();
|
|
|
|
gameObject.SetActive(true);
|
|
StartCoroutine(LoadProjectProgress(onFadeInComplete));
|
|
}
|
|
|
|
private IEnumerator LoadProjectProgress(Action onFadeInComplete)
|
|
{
|
|
logoCanvasGroup.alpha = 1.0f;
|
|
progressBar.fillAmount = 0.0f;
|
|
|
|
if (percentageText != null) percentageText.text = "0%";
|
|
|
|
onFadeInComplete?.Invoke();
|
|
|
|
float timer = 0.0f;
|
|
|
|
while (timer < loadingDuration)
|
|
{
|
|
yield return null;
|
|
timer += Time.unscaledDeltaTime;
|
|
|
|
// 0에서 1까지 3초 동안 부드럽게 채움
|
|
float currentFill = Mathf.Lerp(0.0f, 1.0f, timer / loadingDuration);
|
|
progressBar.fillAmount = currentFill;
|
|
|
|
if (percentageText != null)
|
|
{
|
|
// 0.0 ~ 1.0 값을 0 ~ 100 정수로 변환
|
|
int percent = Mathf.RoundToInt(currentFill * 100);
|
|
percentageText.text = $"{percent}%";
|
|
}
|
|
}
|
|
|
|
progressBar.fillAmount = 1.0f;
|
|
if (percentageText != null)
|
|
percentageText.text = "100%";
|
|
|
|
yield return StartCoroutine(Fade(false));
|
|
}
|
|
|
|
private IEnumerator Fade(bool isFadeIn)
|
|
{
|
|
float timer = 0.0f;
|
|
|
|
while (timer <= 1.0f)
|
|
{
|
|
yield return null;
|
|
timer += Time.unscaledDeltaTime * fadeSpeed;
|
|
|
|
logoCanvasGroup.alpha = isFadeIn ? Mathf.Lerp(0.0f, 1.0f, timer) : Mathf.Lerp(1.0f, 0.0f, timer);
|
|
}
|
|
|
|
// 페이드 아웃이 끝나면 비활성화
|
|
if (!isFadeIn)
|
|
{
|
|
gameObject.SetActive(false);
|
|
}
|
|
}
|
|
}
|
|
} |