123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- 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<RectTransform>();
- panelBg = panelTrans.FindChild("Container").GetComponent<Image>();
- textUI = panelBg.transform.FindChild("Text").GetComponent<Text>();
- animator = GetComponent<Animator>();
- 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<Toast> toastQueue = new List<Toast>();
- private static List<Toast> toastNoQueueList = new List<Toast>();
- public static Toast MakeText(string text, bool useQueue=false)
- {
- GameObject toastObj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/UI/Toast"));
- Toast toast = toastObj.GetComponent<Toast>();
- toast.text = text;
- toast.type = Type.Text;
- toast.useQueue = useQueue;
-
- return toast;
- }
- }
|