StartScreenView.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 ShowPrivacy()
  70. {
  71. if (string.IsNullOrWhiteSpace(AppSettings.Instance.PrivacyUrl))
  72. {
  73. Debug.LogError("PrivacyUrl is not configured in Resources/AppSettings.asset.");
  74. return;
  75. }
  76. Application.OpenURL(AppSettings.Instance.PrivacyUrl);
  77. }
  78. public void ShowImmediate(bool interactive = true)
  79. {
  80. if (fadeRoutine != null)
  81. {
  82. StopCoroutine(fadeRoutine);
  83. fadeRoutine = null;
  84. }
  85. gameObject.SetActive(true);
  86. WireButton();
  87. if (canvasGroup != null)
  88. canvasGroup.alpha = 1f;
  89. SetInteractable(interactive);
  90. }
  91. public void HideImmediate()
  92. {
  93. if (fadeRoutine != null)
  94. {
  95. StopCoroutine(fadeRoutine);
  96. fadeRoutine = null;
  97. }
  98. SetInteractable(false);
  99. if (canvasGroup != null)
  100. canvasGroup.alpha = 0f;
  101. gameObject.SetActive(false);
  102. }
  103. public void SetInteractable(bool value)
  104. {
  105. if (gameStartButton != null)
  106. gameStartButton.interactable = value;
  107. if (canvasGroup != null)
  108. {
  109. canvasGroup.interactable = value;
  110. canvasGroup.blocksRaycasts = value;
  111. }
  112. }
  113. IEnumerator FadeIn()
  114. {
  115. if (canvasGroup != null)
  116. canvasGroup.alpha = 0f;
  117. SetInteractable(false);
  118. float elapsed = 0f;
  119. while (elapsed < fadeDuration)
  120. {
  121. elapsed += Time.unscaledDeltaTime;
  122. if (canvasGroup != null)
  123. canvasGroup.alpha = Mathf.Clamp01(elapsed / fadeDuration);
  124. yield return null;
  125. }
  126. if (canvasGroup != null)
  127. canvasGroup.alpha = 1f;
  128. SetInteractable(true);
  129. fadeRoutine = null;
  130. }
  131. void HandleGameStartClicked()
  132. {
  133. GameAudio.NotifyInteraction();
  134. GameAudio.PlaySfx(GameSfx.Tap);
  135. StartRequested?.Invoke();
  136. }
  137. }