InGameNotificationManager.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class InGameNotificationManager : MonoBehaviour {
  6. public static InGameNotificationManager Instance;
  7. public UIElementsGroup NotificationUI;
  8. public Text Title;
  9. public Text Body;
  10. private bool activeNotification;
  11. private List<NotificationData> queuedNotifications = new List<NotificationData>();
  12. public struct NotificationData
  13. {
  14. public string Title;
  15. public string Body;
  16. public NotificationData(string title, string body)
  17. {
  18. Title = title;
  19. Body = body;
  20. }
  21. }
  22. void Awake()
  23. {
  24. Instance = this;
  25. }
  26. void Update()
  27. {
  28. if (Input.GetKeyDown("n"))
  29. PopNotification("Quest Completed!", "Your First Gig");
  30. }
  31. public void PopNotification(string title, string body)
  32. {
  33. if (activeNotification)
  34. {
  35. queuedNotifications.Add(new NotificationData(title, body));
  36. return;
  37. }
  38. NotificationUI.ChangeVisibility(true);
  39. Title.text = title;
  40. Body.text = body;
  41. StartCoroutine(ExtraLetter());
  42. Invoke("CloseNotification", 3);
  43. activeNotification = true;
  44. }
  45. IEnumerator ExtraLetter()
  46. {
  47. yield return null;
  48. Body.text += " ";
  49. yield return null;
  50. Body.text = Body.text.Substring(0, Body.text.Length -1);
  51. }
  52. public void CloseNotification()
  53. {
  54. activeNotification = false;
  55. NotificationUI.ChangeVisibility(false);
  56. CancelInvoke("CloseNotification");
  57. if (queuedNotifications.Count > 0)
  58. {
  59. NotificationUI.ChangeVisibilityImmediate(false);
  60. NotificationData qData = queuedNotifications[0];
  61. PopNotification(qData.Title, qData.Body);
  62. queuedNotifications.RemoveAt(0);
  63. }
  64. }
  65. }