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(); 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(); } }