using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class NotificationUI : MonoBehaviour { public static NotificationUI Instance { get; private set; } public Text messageText; public CanvasGroup canvasGroup; public float visibleTime = 2.8f; public float fadeTime = 0.35f; private readonly Queue queue = new Queue(); private float timer; private void Awake() { if (Instance != null && Instance != this) { Destroy(gameObject); return; } Instance = this; if (canvasGroup == null) { canvasGroup = GetComponent(); } } private void Update() { if (messageText == null || canvasGroup == null) { return; } if (timer > 0f) { timer -= Time.deltaTime; canvasGroup.alpha = timer < fadeTime ? Mathf.Clamp01(timer / fadeTime) : 1f; return; } if (queue.Count > 0) { messageText.text = queue.Dequeue(); timer = visibleTime + fadeTime; canvasGroup.alpha = 1f; } else { canvasGroup.alpha = 0f; } } public void Show(string message) { if (string.IsNullOrWhiteSpace(message)) { return; } queue.Enqueue(message); } }