Files
AZTECH_WB/Assets/Scripts/UI/Panel_Entry.cs
2026-01-16 09:23:25 +09:00

227 lines
6.6 KiB
C#

using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace AZTECHWB
{
public class LoginInfo
{
public string ID;
public string Password;
}
public class Panel_Entry : MonoBehaviour
{
public CanvasGroup Loading;
public Image Logo;
public Slider Slider_LoadingProgress;
public TMP_Text Text_Loading;
public float fadeOutDuration = 0.8f;
public float simulationDuration = 4f;
Coroutine loadingCoroutine;
public Action onLoadingComplete;
public CanvasGroup Login;
public TMP_InputField InputField_ID;
public TMP_InputField InputField_Password;
public Button Button_Login;
public CanvasGroup LoginError;
public TextMeshProUGUI Text_Error;
public float errorFadeDuration = 0.25f;
public float errorShowTime = 2.0f;
Coroutine errorRoutine;
public List<LoginInfo> loginInfos = new();
void Awake()
{
var loginData = Resources.Load<TextAsset>("LoginInfo").text;
loginInfos = JsonConvert.DeserializeObject<List<LoginInfo>>(loginData);
var canvasGroupDict = transform.GetChildComponentsByName<CanvasGroup>();
Loading = canvasGroupDict.GetOrNull(nameof(Loading));
Login = canvasGroupDict.GetOrNull(nameof(Login));
LoginError = canvasGroupDict.GetOrNull(nameof(LoginError));
Logo = Loading.GetComponentInChildren<Image>();
Slider_LoadingProgress = Loading.GetComponentInChildren<Slider>();
Text_Loading = Loading.GetComponentInChildren<TextMeshProUGUI>();
var inputFieldDict = Login.transform.GetChildComponentsByName<TMP_InputField>();
InputField_ID = inputFieldDict.GetOrNull(nameof(InputField_ID));
InputField_Password = inputFieldDict.GetOrNull(nameof(InputField_Password));
Button_Login = Login.GetComponentInChildren<Button>();
Text_Error = LoginError.GetComponentInChildren<TextMeshProUGUI>();
transform.SetAsLastSibling();
onLoadingComplete += FadeInLoginPanel;
Button_Login.onClick.AddListener(OnClickLoginButton);
}
private void Start()
{
StartLoadingRoutine();
}
#region Loading
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);
UpdateLogoFade(progress);
yield return null;
}
yield return new WaitForSeconds(0.3f);
yield return StartCoroutine(FadeOutLoadingPanel());
}
void UpdateProgress(float progress01)
{
Slider_LoadingProgress.value = progress01;
Text_Loading.SetText($"Loading..({(progress01 * 100f):F0}%)");
}
void UpdateLogoFade(float progress01)
{
float t = progress01 * 5f;
// 삼각파 형태 만들기
float wave = 1f - Mathf.Abs((t % 2f) - 1f);
// 마지막 구간은 항상 Fade In으로 고정
if (t >= 4f)
wave = t - 4f;
float alpha = Mathf.Lerp(0.1f, 1f, Mathf.Clamp01(wave));
var c = Logo.color;
c.a = alpha;
Logo.color = c;
}
IEnumerator FadeOutLoadingPanel()
{
float t = 0f;
float start = Loading.alpha;
while (t < fadeOutDuration)
{
t += Time.deltaTime;
Loading.alpha = Mathf.Lerp(start, 0f, t / fadeOutDuration);
yield return null;
}
Loading.alpha = 0f;
onLoadingComplete?.Invoke();
}
#endregion
public void FadeInLoginPanel()
{
StopAllCoroutines();
StartCoroutine(FadeIn());
}
IEnumerator FadeIn()
{
float t = 0f;
float start = Login.alpha;
while (t < fadeOutDuration)
{
t += Time.deltaTime;
Login.alpha = Mathf.Lerp(start, 1f, t / fadeOutDuration);
yield return null;
}
Login.alpha = 1f;
}
private void OnClickLoginButton()
{
string id = InputField_ID.text.Trim();
string pw = InputField_Password.text.Trim();
if (string.IsNullOrEmpty(id) || string.IsNullOrEmpty(pw))
{
ShowLoginError("ID와 비밀번호를 입력해주세요.");
return;
}
LoginInfo matched = null;
foreach (var info in loginInfos)
{
if (info.ID == id && info.Password == pw)
{
matched = info;
break;
}
}
if (matched == null)
{
ShowLoginError("ID 또는 비밀번호가 올바르지 않습니다.");
return;
}
OnLoginSuccess();
}
private void ShowLoginError(string msg)
{
if (errorRoutine != null)
StopCoroutine(errorRoutine);
errorRoutine = StartCoroutine(ErrorRoutine(msg));
}
private void OnLoginSuccess()
{
gameObject.SetActive(false);
}
IEnumerator ErrorRoutine(string msg)
{
Text_Error.text = msg;
yield return StartCoroutine(FadeErrorPanel(LoginError, 0f, 1f, errorFadeDuration));
yield return new WaitForSeconds(errorShowTime);
yield return StartCoroutine(FadeErrorPanel(LoginError, 1f, 0f, errorFadeDuration));
}
IEnumerator FadeErrorPanel(CanvasGroup cg, float from, float to, float duration)
{
float t = 0f;
cg.alpha = from;
while (t < duration)
{
t += Time.deltaTime;
cg.alpha = Mathf.Lerp(from, to, t / duration);
yield return null;
}
cg.alpha = to;
}
}
}