Files

68 lines
1.4 KiB
C#
Raw Permalink Normal View History

2026-06-04 20:14:47 +03:00
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<string> queue = new Queue<string>();
private float timer;
private void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
return;
}
Instance = this;
if (canvasGroup == null)
{
canvasGroup = GetComponent<CanvasGroup>();
}
}
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);
}
}