Toast.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. public class Toast : MonoBehaviour {
  6. public enum Type
  7. {
  8. Text,
  9. }
  10. public RectTransform panelTrans;
  11. public Image panelBg;
  12. public Text textUI;
  13. public Animator animator;
  14. public float duration = 2f;
  15. private float startShowTime = -1f;
  16. private float hiddingDuration = 0.5f;
  17. private float startHiddingTime = -1;
  18. public string text
  19. {
  20. set
  21. {
  22. textUI.text = value;
  23. }
  24. get
  25. {
  26. return textUI.text;
  27. }
  28. }
  29. public Type type;
  30. public bool useQueue;
  31. void Awake()
  32. {
  33. panelTrans = transform.FindChild("Panel").GetComponent<RectTransform>();
  34. panelBg = panelTrans.FindChild("Container").GetComponent<Image>();
  35. textUI = panelBg.transform.FindChild("Text").GetComponent<Text>();
  36. animator = GetComponent<Animator>();
  37. type = Type.Text;
  38. }
  39. void Start()
  40. {
  41. Show();
  42. }
  43. public void Show()
  44. {
  45. if(useQueue)
  46. {
  47. if(toastQueue.Count > 0)
  48. {
  49. if(toastQueue[0] != this)
  50. {
  51. toastQueue.Add(this);
  52. return;
  53. }
  54. }
  55. else
  56. {
  57. toastQueue.Add(this);
  58. }
  59. }
  60. else
  61. {
  62. while(toastNoQueueList.Count > 0)
  63. {
  64. Toast toast = toastNoQueueList[0];
  65. toastNoQueueList.Remove(toast);
  66. Destroy(toast.gameObject);
  67. }
  68. toastNoQueueList.Add(this);
  69. }
  70. PopUpManager.AddToMainCanvas(gameObject);
  71. if(type == Type.Text)
  72. {
  73. animator.Play("ToastShow", 0, 0);
  74. }
  75. startShowTime = GameTime.time;
  76. }
  77. void Update()
  78. {
  79. if(startHiddingTime >= 0)
  80. {
  81. if(GameTime.time - startHiddingTime >= hiddingDuration)
  82. {
  83. toastQueue.Remove(this);
  84. if(toastQueue.Count > 0)
  85. {
  86. toastQueue[0].Show();
  87. }
  88. if(!useQueue)
  89. toastNoQueueList.Remove(this);
  90. Destroy(this.gameObject);
  91. }
  92. }
  93. else if(startShowTime >= 0)
  94. {
  95. if(type == Type.Text)
  96. {
  97. if(GameTime.time - startShowTime >= duration)
  98. {
  99. startHiddingTime = GameTime.time;
  100. animator.Play("ToastHide", 0, 0);
  101. }
  102. }
  103. }
  104. }
  105. private static List<Toast> toastQueue = new List<Toast>();
  106. private static List<Toast> toastNoQueueList = new List<Toast>();
  107. public static Toast MakeText(string text, bool useQueue=false)
  108. {
  109. GameObject toastObj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/UI/Toast"));
  110. Toast toast = toastObj.GetComponent<Toast>();
  111. toast.text = text;
  112. toast.type = Type.Text;
  113. toast.useQueue = useQueue;
  114. return toast;
  115. }
  116. }