68 lines
1.5 KiB
C#
68 lines
1.5 KiB
C#
|
|
using System.Collections;
|
||
|
|
using UnityEngine;
|
||
|
|
using UnityEngine.UI;
|
||
|
|
|
||
|
|
public class NotificationUI : MonoBehaviour
|
||
|
|
{
|
||
|
|
public Text messageText;
|
||
|
|
public CanvasGroup canvasGroup;
|
||
|
|
public float visibleDuration = 2.2f;
|
||
|
|
public float fadeDuration = 0.25f;
|
||
|
|
|
||
|
|
private Coroutine routine;
|
||
|
|
|
||
|
|
private void Awake()
|
||
|
|
{
|
||
|
|
if (canvasGroup == null)
|
||
|
|
{
|
||
|
|
canvasGroup = GetComponent<CanvasGroup>();
|
||
|
|
}
|
||
|
|
|
||
|
|
if (canvasGroup != null)
|
||
|
|
{
|
||
|
|
canvasGroup.alpha = 0f;
|
||
|
|
canvasGroup.blocksRaycasts = false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Show(string message)
|
||
|
|
{
|
||
|
|
if (messageText == null || string.IsNullOrWhiteSpace(message))
|
||
|
|
{
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
messageText.text = message;
|
||
|
|
|
||
|
|
if (routine != null)
|
||
|
|
{
|
||
|
|
StopCoroutine(routine);
|
||
|
|
}
|
||
|
|
|
||
|
|
routine = StartCoroutine(ShowRoutine());
|
||
|
|
}
|
||
|
|
|
||
|
|
private IEnumerator ShowRoutine()
|
||
|
|
{
|
||
|
|
if (canvasGroup == null)
|
||
|
|
{
|
||
|
|
yield break;
|
||
|
|
}
|
||
|
|
|
||
|
|
canvasGroup.alpha = 1f;
|
||
|
|
yield return new WaitForSeconds(Mathf.Max(0.1f, visibleDuration));
|
||
|
|
|
||
|
|
float elapsed = 0f;
|
||
|
|
float duration = Mathf.Max(0.05f, fadeDuration);
|
||
|
|
while (elapsed < duration)
|
||
|
|
{
|
||
|
|
elapsed += Time.deltaTime;
|
||
|
|
canvasGroup.alpha = Mathf.Lerp(1f, 0f, elapsed / duration);
|
||
|
|
yield return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
canvasGroup.alpha = 0f;
|
||
|
|
routine = null;
|
||
|
|
}
|
||
|
|
}
|