LoadingScreenView.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System.Collections;
  2. using TMPro;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. [DisallowMultipleComponent]
  6. public class LoadingScreenView : MonoBehaviour
  7. {
  8. [Header("References")]
  9. [SerializeField] Image progressFill;
  10. [SerializeField] TMP_Text progressLabel;
  11. [SerializeField] RectTransform icon;
  12. [SerializeField] CanvasGroup canvasGroup;
  13. [Header("Idle Animation")]
  14. [SerializeField] float bobAmplitude = 6f;
  15. [SerializeField] float bobPeriod = 2.4f;
  16. Vector2 iconBasePosition;
  17. bool hasIconBasePosition;
  18. float progressValue;
  19. void Awake()
  20. {
  21. if (canvasGroup == null)
  22. canvasGroup = GetComponent<CanvasGroup>();
  23. CacheIconPosition();
  24. }
  25. void OnEnable()
  26. {
  27. SaveService.EnsureLoaded();
  28. CacheIconPosition();
  29. LocaleService.Changed += HandleLocaleChanged;
  30. RefreshLocalizedText();
  31. UILocaleScreens.ApplyLoading(transform);
  32. }
  33. void OnDisable()
  34. {
  35. LocaleService.Changed -= HandleLocaleChanged;
  36. if (icon != null && hasIconBasePosition)
  37. icon.anchoredPosition = iconBasePosition;
  38. }
  39. void Update()
  40. {
  41. if (icon == null || bobAmplitude <= 0f || bobPeriod <= 0f)
  42. return;
  43. if (!hasIconBasePosition)
  44. CacheIconPosition();
  45. float phase = Time.unscaledTime * Mathf.PI * 2f / bobPeriod;
  46. icon.anchoredPosition = iconBasePosition + Vector2.up * (Mathf.Sin(phase) * bobAmplitude);
  47. }
  48. void CacheIconPosition()
  49. {
  50. if (icon == null)
  51. return;
  52. iconBasePosition = icon.anchoredPosition;
  53. hasIconBasePosition = true;
  54. }
  55. public void SetProgress(float normalized)
  56. {
  57. progressValue = Mathf.Clamp01(normalized);
  58. if (progressFill != null)
  59. progressFill.fillAmount = progressValue;
  60. RefreshLocalizedText();
  61. }
  62. void RefreshLocalizedText()
  63. {
  64. if (progressLabel != null)
  65. progressLabel.text = L.F("common.loading", Mathf.RoundToInt(progressValue * 100f));
  66. }
  67. void HandleLocaleChanged()
  68. {
  69. RefreshLocalizedText();
  70. UILocaleScreens.ApplyLoading(transform);
  71. }
  72. public void Show()
  73. {
  74. gameObject.SetActive(true);
  75. transform.SetAsLastSibling();
  76. if (canvasGroup == null)
  77. return;
  78. canvasGroup.alpha = 1f;
  79. canvasGroup.interactable = false;
  80. canvasGroup.blocksRaycasts = false;
  81. }
  82. public void Hide()
  83. {
  84. if (canvasGroup != null)
  85. {
  86. canvasGroup.alpha = 0f;
  87. canvasGroup.interactable = false;
  88. canvasGroup.blocksRaycasts = false;
  89. }
  90. gameObject.SetActive(false);
  91. }
  92. public IEnumerator FadeOutAndHide(float duration)
  93. {
  94. if (canvasGroup == null || duration <= 0f)
  95. {
  96. Hide();
  97. yield break;
  98. }
  99. canvasGroup.interactable = false;
  100. canvasGroup.blocksRaycasts = true;
  101. float startAlpha = canvasGroup.alpha;
  102. float elapsed = 0f;
  103. while (elapsed < duration)
  104. {
  105. elapsed += Time.unscaledDeltaTime;
  106. float t = Mathf.Clamp01(elapsed / duration);
  107. canvasGroup.alpha = startAlpha * Mathf.SmoothStep(1f, 0f, t);
  108. yield return null;
  109. }
  110. Hide();
  111. }
  112. public void SetVisible(bool visible)
  113. {
  114. if (visible)
  115. Show();
  116. else
  117. Hide();
  118. }
  119. }