StartScreenView.cs 3.5 KB

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