using UnityEngine; using UnityEngine.UI; using System.Collections; using System.Collections.Generic; public class Toast : MonoBehaviour { public enum Type { Text, } public RectTransform panelTrans; public Image panelBg; public Text textUI; public Animator animator; public float duration = 2f; private float startShowTime = -1f; private float hiddingDuration = 0.5f; private float startHiddingTime = -1; public string text { set { textUI.text = value; } get { return textUI.text; } } public Type type; public bool useQueue; void Awake() { panelTrans = transform.FindChild("Panel").GetComponent(); panelBg = panelTrans.FindChild("Container").GetComponent(); textUI = panelBg.transform.FindChild("Text").GetComponent(); animator = GetComponent(); type = Type.Text; } void Start() { Show(); } public void Show() { if(useQueue) { if(toastQueue.Count > 0) { if(toastQueue[0] != this) { toastQueue.Add(this); return; } } else { toastQueue.Add(this); } } else { while(toastNoQueueList.Count > 0) { Toast toast = toastNoQueueList[0]; toastNoQueueList.Remove(toast); Destroy(toast.gameObject); } toastNoQueueList.Add(this); } PopUpManager.AddToMainCanvas(gameObject); if(type == Type.Text) { animator.Play("ToastShow", 0, 0); } startShowTime = GameTime.time; } void Update() { if(startHiddingTime >= 0) { if(GameTime.time - startHiddingTime >= hiddingDuration) { toastQueue.Remove(this); if(toastQueue.Count > 0) { toastQueue[0].Show(); } if(!useQueue) toastNoQueueList.Remove(this); Destroy(this.gameObject); } } else if(startShowTime >= 0) { if(type == Type.Text) { if(GameTime.time - startShowTime >= duration) { startHiddingTime = GameTime.time; animator.Play("ToastHide", 0, 0); } } } } private static List toastQueue = new List(); private static List toastNoQueueList = new List(); public static Toast MakeText(string text, bool useQueue=false) { GameObject toastObj = GameObject.Instantiate(Resources.Load("Prefabs/UI/Toast")); Toast toast = toastObj.GetComponent(); toast.text = text; toast.type = Type.Text; toast.useQueue = useQueue; return toast; } }