173 lines
4.8 KiB
C#
173 lines
4.8 KiB
C#
using System.Collections;
|
||
using System.Text;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using UnityEngine.Playables;
|
||
using UnityEngine.UI;
|
||
|
||
//Нужно инициализировать
|
||
//Зависит от саунд менеджера. В порядке инициализации должен быть ниже.
|
||
public class NotificationSystem : MonoBehaviour
|
||
{
|
||
public static NotificationSystem instance;
|
||
|
||
[Header("Анимация вывода уведомления")]
|
||
[SerializeField] private float appearTime = 1;
|
||
[SerializeField] private float standTime = 3;
|
||
[SerializeField] private float disappearTime = 1;
|
||
|
||
[SerializeField] private RectTransform NotificationContainer;
|
||
[SerializeField] private TextMeshProUGUI Title;
|
||
[SerializeField] private TextMeshProUGUI Message;
|
||
[SerializeField] private Image Image;
|
||
|
||
|
||
private StringBuilder errors;
|
||
private bool notificationProcessing;
|
||
public void Initialize()
|
||
{
|
||
instance = this;
|
||
errors = new StringBuilder();
|
||
notificationProcessing = false;
|
||
|
||
CheckLinks();
|
||
}
|
||
|
||
public void Notificate(Notification notification)
|
||
{
|
||
if (notificationProcessing) return;
|
||
if (!CheckNotification(notification)) return;
|
||
|
||
|
||
FillNotificationCard(notification);
|
||
StartCoroutine(ShowNotificationProcess(notification));
|
||
|
||
}
|
||
|
||
private bool CheckLinks()
|
||
{
|
||
bool hasNull = false;
|
||
errors.Clear();
|
||
|
||
if(NotificationContainer == null)
|
||
{
|
||
hasNull = true;
|
||
errors.AppendLine("Не указан контейнер.");
|
||
}
|
||
if (Title == null)
|
||
{
|
||
hasNull = true;
|
||
errors.AppendLine("Не указано поле заголовка.");
|
||
}
|
||
if (Message == null)
|
||
{
|
||
hasNull = true;
|
||
errors.AppendLine("Не указано поле сообщения.");
|
||
}
|
||
if (Image == null)
|
||
{
|
||
hasNull = true;
|
||
errors.AppendLine("Не указано поле картинки.");
|
||
}
|
||
|
||
if (hasNull)
|
||
{
|
||
Debug.LogWarning($"Пустые ссылки в системе уведовлений:\n{errors}");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
private bool CheckNotification(Notification notification)
|
||
{
|
||
if (notification == null)
|
||
{
|
||
Debug.LogWarning($"Система уведомлений: Полученное уведомление null.");
|
||
return false;
|
||
}
|
||
|
||
errors.Clear();
|
||
bool hasErrors = false;
|
||
|
||
if (string.IsNullOrEmpty(notification.Title))
|
||
{
|
||
hasErrors = true;
|
||
errors.AppendLine($"В уведомлении не указан заголовок");
|
||
}
|
||
if (string.IsNullOrEmpty(notification.Message))
|
||
{
|
||
hasErrors = true;
|
||
errors.AppendLine($"В уведомлении не указано сообщение");
|
||
}
|
||
if (notification.Image == null)
|
||
{
|
||
hasErrors = true;
|
||
errors.AppendLine($"В уведомлении Image = null");
|
||
}
|
||
if (notification.audioClip == null)
|
||
{
|
||
hasErrors = true;
|
||
errors.AppendLine($"В уведомлении AudioClip = null");
|
||
}
|
||
|
||
|
||
if (hasErrors)
|
||
{
|
||
Debug.LogWarning($"Ошибки в уведомлении:\n{errors}");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
private void FillNotificationCard(Notification notification)
|
||
{
|
||
Title.text = notification.Title;
|
||
Message.text = notification.Message;
|
||
Image.sprite = notification.Image;
|
||
Image.rectTransform.sizeDelta = Vector2.one * notification.ImageSize;
|
||
Image.rectTransform.anchoredPosition = notification.ImagePosition;
|
||
|
||
}
|
||
|
||
|
||
IEnumerator ShowNotificationProcess(Notification notification)
|
||
{
|
||
notificationProcessing = true;
|
||
float t;
|
||
float containerHeight = NotificationContainer.sizeDelta.y;
|
||
//#ВЫДВИНУТЬ ВВЕРХ
|
||
t = 0;
|
||
while (t < 1)
|
||
{
|
||
t += Time.deltaTime / appearTime;
|
||
t = Mathf.Clamp01(t);
|
||
|
||
NotificationContainer.anchoredPosition = Vector2.Lerp(Vector2.zero, Vector2.up*containerHeight, t);
|
||
|
||
yield return null;
|
||
}
|
||
|
||
//подождать
|
||
SoundManager.instance.PlaySfx(notification.audioClip);
|
||
yield return new WaitForSeconds(standTime);
|
||
|
||
//#ЗАДВИНУТЬ ВНИЗ
|
||
t = 0;
|
||
while (t < 1)
|
||
{
|
||
t += Time.deltaTime / disappearTime;
|
||
t = Mathf.Clamp01(t);
|
||
|
||
NotificationContainer.anchoredPosition = Vector2.Lerp(Vector2.up * containerHeight, Vector2.zero, t);
|
||
|
||
yield return null;
|
||
}
|
||
|
||
notificationProcessing = false;
|
||
}
|
||
|
||
|
||
|
||
|
||
}
|