StartScreenView.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. [DisallowMultipleComponent]
  6. public class StartScreenView : MonoBehaviour
  7. {
  8. [Header("References")]
  9. [SerializeField] Button gameStartButton;
  10. [SerializeField] CanvasGroup canvasGroup;
  11. [Header("Transition")]
  12. [SerializeField] float fadeDuration = 0.3f;
  13. public event Action StartRequested;
  14. Coroutine fadeRoutine;
  15. bool buttonWired;
  16. void Awake()
  17. {
  18. if (canvasGroup == null)
  19. canvasGroup = GetComponent<CanvasGroup>();
  20. WireButton();
  21. }
  22. void OnEnable()
  23. {
  24. SaveService.EnsureLoaded();
  25. LocaleService.Changed += RefreshLocalizedText;
  26. RefreshLocalizedText();
  27. }
  28. void OnDisable()
  29. {
  30. LocaleService.Changed -= RefreshLocalizedText;
  31. }
  32. void RefreshLocalizedText()
  33. {
  34. UILocaleScreens.ApplyStart(transform);
  35. }
  36. void OnDestroy()
  37. {
  38. UnwireButton();
  39. }
  40. void WireButton()
  41. {
  42. if (buttonWired || gameStartButton == null)
  43. return;
  44. gameStartButton.onClick.AddListener(HandleGameStartClicked);
  45. buttonWired = true;
  46. }
  47. void UnwireButton()
  48. {
  49. if (!buttonWired || gameStartButton == null)
  50. return;
  51. gameStartButton.onClick.RemoveListener(HandleGameStartClicked);
  52. buttonWired = false;
  53. }
  54. public void Show()
  55. {
  56. gameObject.SetActive(true);
  57. WireButton();
  58. if (fadeRoutine != null)
  59. StopCoroutine(fadeRoutine);
  60. if (fadeDuration <= 0f)
  61. {
  62. if (canvasGroup != null)
  63. canvasGroup.alpha = 1f;
  64. SetInteractable(true);
  65. return;
  66. }
  67. fadeRoutine = StartCoroutine(FadeIn());
  68. }
  69. public void ShowImmediate(bool interactive = true)
  70. {
  71. if (fadeRoutine != null)
  72. {
  73. StopCoroutine(fadeRoutine);
  74. fadeRoutine = null;
  75. }
  76. gameObject.SetActive(true);
  77. WireButton();
  78. if (canvasGroup != null)
  79. canvasGroup.alpha = 1f;
  80. SetInteractable(interactive);
  81. }
  82. public void HideImmediate()
  83. {
  84. if (fadeRoutine != null)
  85. {
  86. StopCoroutine(fadeRoutine);
  87. fadeRoutine = null;
  88. }
  89. SetInteractable(false);
  90. if (canvasGroup != null)
  91. canvasGroup.alpha = 0f;
  92. gameObject.SetActive(false);
  93. }
  94. public void SetInteractable(bool value)
  95. {
  96. if (gameStartButton != null)
  97. gameStartButton.interactable = value;
  98. if (canvasGroup != null)
  99. {
  100. canvasGroup.interactable = value;
  101. canvasGroup.blocksRaycasts = value;
  102. }
  103. }
  104. IEnumerator FadeIn()
  105. {
  106. if (canvasGroup != null)
  107. canvasGroup.alpha = 0f;
  108. SetInteractable(false);
  109. float elapsed = 0f;
  110. while (elapsed < fadeDuration)
  111. {
  112. elapsed += Time.unscaledDeltaTime;
  113. if (canvasGroup != null)
  114. canvasGroup.alpha = Mathf.Clamp01(elapsed / fadeDuration);
  115. yield return null;
  116. }
  117. if (canvasGroup != null)
  118. canvasGroup.alpha = 1f;
  119. SetInteractable(true);
  120. fadeRoutine = null;
  121. }
  122. void HandleGameStartClicked()
  123. {
  124. GameAudio.NotifyInteraction();
  125. GameAudio.PlaySfx(GameSfx.Tap);
  126. StartRequested?.Invoke();
  127. }
  128. }