| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- using System.Collections;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
- [DisallowMultipleComponent]
- public class LoadingScreenView : MonoBehaviour
- {
- [Header("References")]
- [SerializeField] Image progressFill;
- [SerializeField] TMP_Text progressLabel;
- [SerializeField] RectTransform icon;
- [SerializeField] CanvasGroup canvasGroup;
- [Header("Idle Animation")]
- [SerializeField] float bobAmplitude = 6f;
- [SerializeField] float bobPeriod = 2.4f;
- Vector2 iconBasePosition;
- bool hasIconBasePosition;
- float progressValue;
- void Awake()
- {
- if (canvasGroup == null)
- canvasGroup = GetComponent<CanvasGroup>();
- CacheIconPosition();
- }
- void OnEnable()
- {
- SaveService.EnsureLoaded();
- CacheIconPosition();
- LocaleService.Changed += HandleLocaleChanged;
- RefreshLocalizedText();
- UILocaleScreens.ApplyLoading(transform);
- }
- void OnDisable()
- {
- LocaleService.Changed -= HandleLocaleChanged;
- if (icon != null && hasIconBasePosition)
- icon.anchoredPosition = iconBasePosition;
- }
- void Update()
- {
- if (icon == null || bobAmplitude <= 0f || bobPeriod <= 0f)
- return;
- if (!hasIconBasePosition)
- CacheIconPosition();
- float phase = Time.unscaledTime * Mathf.PI * 2f / bobPeriod;
- icon.anchoredPosition = iconBasePosition + Vector2.up * (Mathf.Sin(phase) * bobAmplitude);
- }
- void CacheIconPosition()
- {
- if (icon == null)
- return;
- iconBasePosition = icon.anchoredPosition;
- hasIconBasePosition = true;
- }
- public void SetProgress(float normalized)
- {
- progressValue = Mathf.Clamp01(normalized);
- if (progressFill != null)
- progressFill.fillAmount = progressValue;
- RefreshLocalizedText();
- }
- void RefreshLocalizedText()
- {
- if (progressLabel != null)
- progressLabel.text = L.F("common.loading", Mathf.RoundToInt(progressValue * 100f));
- }
- void HandleLocaleChanged()
- {
- RefreshLocalizedText();
- UILocaleScreens.ApplyLoading(transform);
- }
- public void Show()
- {
- gameObject.SetActive(true);
- transform.SetAsLastSibling();
- if (canvasGroup == null)
- return;
- canvasGroup.alpha = 1f;
- canvasGroup.interactable = false;
- canvasGroup.blocksRaycasts = false;
- }
- public void Hide()
- {
- if (canvasGroup != null)
- {
- canvasGroup.alpha = 0f;
- canvasGroup.interactable = false;
- canvasGroup.blocksRaycasts = false;
- }
- gameObject.SetActive(false);
- }
- public IEnumerator FadeOutAndHide(float duration)
- {
- if (canvasGroup == null || duration <= 0f)
- {
- Hide();
- yield break;
- }
- canvasGroup.interactable = false;
- canvasGroup.blocksRaycasts = true;
- float startAlpha = canvasGroup.alpha;
- float elapsed = 0f;
- while (elapsed < duration)
- {
- elapsed += Time.unscaledDeltaTime;
- float t = Mathf.Clamp01(elapsed / duration);
- canvasGroup.alpha = startAlpha * Mathf.SmoothStep(1f, 0f, t);
- yield return null;
- }
- Hide();
- }
- public void SetVisible(bool visible)
- {
- if (visible)
- Show();
- else
- Hide();
- }
- }
|