first commit
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user