using System; using System.Collections; using RR.Common.Controls; using TMPro; using UnityEngine; using UnityEngine.InputSystem; namespace RR.Common.UI { public class StatsWindowUIController : MonoBehaviour { [Header("Управление")] [SerializeField] private InputActionReference triggerKey; [Header("Тексты статистики")] [SerializeField] private string gamesPlayedTextPrefix = "Игр сыграно: "; [SerializeField] private TextMeshProUGUI gamesPlayedText; [SerializeField] private string prizesGivenTextPrefix = "Призов выдано: "; [SerializeField] private TextMeshProUGUI prizesGivenText; [Header("UI")] [SerializeField] private CanvasGroup canvasGroup; [SerializeField] private float fadeDuration = 0.15f; private string _gamesPlayedKey; private string _prizesGivenKey; private void Awake() { _gamesPlayedKey = PrizeSystem.key_startsNul; _prizesGivenKey = PrizeSystem.key_prizesgivenNul; if (triggerKey != null) triggerKey.action.performed += Toggle; gameObject.SetActive(false); } private void OnDestroy() { if (triggerKey != null) triggerKey.action.performed -= Toggle; } private void Toggle(InputAction.CallbackContext ctx) { if (gameObject.activeSelf) { Close(); } else { Open(); } } private void Open() { gameObject.SetActive(true); UpdateText(); StopAllCoroutines(); StartCoroutine(Fade(0f, 1f, fadeDuration)); } private void Close() { StopAllCoroutines(); StartCoroutine(Fade(1f, 0f, fadeDuration, () => { gameObject.SetActive(false); })); } private void UpdateText() { if (gamesPlayedText) { int gamesPlayed = PlayerPrefs.GetInt(_gamesPlayedKey, 0); gamesPlayedText.text = gamesPlayedTextPrefix + gamesPlayed; } if (prizesGivenText) { int prizesGiven = PlayerPrefs.GetInt(_prizesGivenKey, 0); prizesGivenText.text = prizesGivenTextPrefix + prizesGiven; } } /// /// Метод для плавного перехода (затухания) /// /// Начальная прозрачность /// Конечная прозрачность /// Время перехода /// Действие при выполнении перехода private IEnumerator Fade(float from, float to, float time, System.Action onDone = null) { if (!canvasGroup) { onDone?.Invoke(); yield break; } float t = 0f; canvasGroup.alpha = from; canvasGroup.blocksRaycasts = true; while (t < time) { t += Time.unscaledDeltaTime; canvasGroup.alpha = Mathf.Lerp(from, to, t / time); yield return null; } canvasGroup.alpha = to; canvasGroup.blocksRaycasts = to > 0.99f; onDone?.Invoke(); } } } // namespace RR.Common.UI