139 lines
3.9 KiB
C#
139 lines
3.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.UI;
|
|
|
|
namespace EnglewoodLAB
|
|
{
|
|
public class UILoading : MonoBehaviour
|
|
{
|
|
private static UILoading instance;
|
|
|
|
private const string PREFAB_PATH = "UI/Prefab/Home/UILoading";
|
|
private float loadingDuration = 1.5f;
|
|
private float fadeSpeed = 2.0f;
|
|
|
|
private CanvasGroup logoCanvasGroup; // 로고의 CanvasGroup 연결
|
|
private Image progressBar;
|
|
|
|
private string nextSceneName = "EnglewoodLAB"; // 이동할 다음 씬 이름
|
|
|
|
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/LoadingBarBg/LoadingBarImage");
|
|
if (imageTransform != null)
|
|
{
|
|
progressBar = imageTransform.GetComponentInChildren<Image>();
|
|
}
|
|
else
|
|
{
|
|
// 경로로 못 찾으면 자식 전체에서 탐색
|
|
progressBar = GetComponentInChildren<Image>();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
LoadScene(nextSceneName);
|
|
}
|
|
|
|
public void LoadScene(string sceneName)
|
|
{
|
|
gameObject.SetActive(true);
|
|
SceneManager.sceneLoaded += OnSceneLoaded;
|
|
nextSceneName = sceneName;
|
|
|
|
StartCoroutine(LoadProjectProgress());
|
|
}
|
|
|
|
private IEnumerator LoadProjectProgress()
|
|
{
|
|
logoCanvasGroup.alpha = 1.0f;
|
|
progressBar.fillAmount = 0.0f;
|
|
|
|
AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(nextSceneName);
|
|
asyncOperation.allowSceneActivation = false;
|
|
|
|
float timer = 0.0f;
|
|
while (!asyncOperation.isDone)
|
|
{
|
|
yield return null;
|
|
if (asyncOperation.progress < 0.1f)
|
|
progressBar.fillAmount = asyncOperation.progress;
|
|
else
|
|
{
|
|
timer += Time.unscaledDeltaTime;
|
|
progressBar.fillAmount = Mathf.Lerp(0.1f, 1.0f, timer / 3.0f);
|
|
|
|
if (progressBar.fillAmount >= 1.0f)
|
|
{
|
|
asyncOperation.allowSceneActivation = true;
|
|
yield break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private IEnumerator Fade(bool isFadeIn)
|
|
{
|
|
float timer = 0.0f;
|
|
|
|
while (timer <= 1.0f)
|
|
{
|
|
yield return null;
|
|
timer += Time.unscaledDeltaTime * 3.0f;
|
|
|
|
logoCanvasGroup.alpha = isFadeIn ? Mathf.Lerp(0.0f, 1.0f, timer) : Mathf.Lerp(1.0f, 0.0f, timer);
|
|
}
|
|
|
|
// 페이드 아웃이 끝나면 비활성화
|
|
if (!isFadeIn)
|
|
gameObject.SetActive(false);
|
|
}
|
|
|
|
private void OnSceneLoaded(Scene arg0, LoadSceneMode arg1)
|
|
{
|
|
if (arg0.name == nextSceneName)
|
|
{
|
|
StartCoroutine(Fade(false));
|
|
|
|
SceneManager.sceneLoaded -= OnSceneLoaded;
|
|
}
|
|
}
|
|
}
|
|
} |