| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- using System;
- using System.Collections;
- using UnityEngine;
- using UnityEngine.UI;
- [DisallowMultipleComponent]
- public class StartScreenView : MonoBehaviour
- {
- [Header("References")]
- [SerializeField] Button gameStartButton;
- [SerializeField] CanvasGroup canvasGroup;
- [Header("Transition")]
- [SerializeField] float fadeDuration = 0.3f;
- public event Action StartRequested;
- Coroutine fadeRoutine;
- bool buttonWired;
- void Awake()
- {
- if (canvasGroup == null)
- canvasGroup = GetComponent<CanvasGroup>();
- WireButton();
- }
- void OnEnable()
- {
- SaveService.EnsureLoaded();
- LocaleService.Changed += RefreshLocalizedText;
- RefreshLocalizedText();
- }
- void OnDisable()
- {
- LocaleService.Changed -= RefreshLocalizedText;
- }
- void RefreshLocalizedText()
- {
- UILocaleScreens.ApplyStart(transform);
- }
- void OnDestroy()
- {
- UnwireButton();
- }
- void WireButton()
- {
- if (buttonWired || gameStartButton == null)
- return;
- gameStartButton.onClick.AddListener(HandleGameStartClicked);
- buttonWired = true;
- }
- void UnwireButton()
- {
- if (!buttonWired || gameStartButton == null)
- return;
- gameStartButton.onClick.RemoveListener(HandleGameStartClicked);
- buttonWired = false;
- }
- public void Show()
- {
- gameObject.SetActive(true);
- WireButton();
- if (fadeRoutine != null)
- StopCoroutine(fadeRoutine);
- if (fadeDuration <= 0f)
- {
- if (canvasGroup != null)
- canvasGroup.alpha = 1f;
- SetInteractable(true);
- return;
- }
- fadeRoutine = StartCoroutine(FadeIn());
- }
- public void ShowPrivacy()
- {
-
- if (string.IsNullOrWhiteSpace(AppSettings.Instance.PrivacyUrl))
- {
- Debug.LogError("PrivacyUrl is not configured in Resources/AppSettings.asset.");
- return;
- }
- Application.OpenURL(AppSettings.Instance.PrivacyUrl);
- }
- public void ShowImmediate(bool interactive = true)
- {
- if (fadeRoutine != null)
- {
- StopCoroutine(fadeRoutine);
- fadeRoutine = null;
- }
- gameObject.SetActive(true);
- WireButton();
- if (canvasGroup != null)
- canvasGroup.alpha = 1f;
- SetInteractable(interactive);
- }
- public void HideImmediate()
- {
- if (fadeRoutine != null)
- {
- StopCoroutine(fadeRoutine);
- fadeRoutine = null;
- }
- SetInteractable(false);
- if (canvasGroup != null)
- canvasGroup.alpha = 0f;
- gameObject.SetActive(false);
- }
- public void SetInteractable(bool value)
- {
- if (gameStartButton != null)
- gameStartButton.interactable = value;
- if (canvasGroup != null)
- {
- canvasGroup.interactable = value;
- canvasGroup.blocksRaycasts = value;
- }
- }
- IEnumerator FadeIn()
- {
- if (canvasGroup != null)
- canvasGroup.alpha = 0f;
- SetInteractable(false);
- float elapsed = 0f;
- while (elapsed < fadeDuration)
- {
- elapsed += Time.unscaledDeltaTime;
- if (canvasGroup != null)
- canvasGroup.alpha = Mathf.Clamp01(elapsed / fadeDuration);
- yield return null;
- }
- if (canvasGroup != null)
- canvasGroup.alpha = 1f;
- SetInteractable(true);
- fadeRoutine = null;
- }
- void HandleGameStartClicked()
- {
- GameAudio.NotifyInteraction();
- GameAudio.PlaySfx(GameSfx.Tap);
- StartRequested?.Invoke();
- }
- }
|