public void ShowToast(string message, Sprite sprite, float duration) { if (!showing) { showing = true; ToastStruct toast = new ToastStruct(message, sprite, duration); StartCoroutine(Toast(toast)); } }
public void EnqueueToast(string message, Sprite sprite, float duration) { ToastStruct newToast = new ToastStruct(message, sprite, duration); toastsQueue.Enqueue(newToast); if (!coroutineRunning) { StartCoroutine(ToastsQueueCoroutine()); } }
private IEnumerator Toast(ToastStruct toast) { text.text = toast.message; if (toast.sprite != null && toastIcon != null) { toastIcon.sprite = toast.sprite; } StartCoroutine(FadeIn()); yield return(new WaitForSecondsRealtime(toast.duration)); StartCoroutine(FadeOut()); }
private IEnumerator ToastsQueueCoroutine() { coroutineRunning = true; while (toastsQueue.Count != 0) { if (!showing) { showing = true; ToastStruct toast = toastsQueue.Dequeue(); StartCoroutine(Toast(toast)); } yield return(new WaitForEndOfFrame()); } coroutineRunning = false; }
void Dequeue() { if (queue.Count == 0) { return; } if (gameScript.players.Count > 3) { // In 4FG, the leftmost player's viewer popup can block the view of toasts. If it has two or more lines, // wait until it goes away to start dequeueing toasts again. GameObject viewerPopup = GameObject.FindGameObjectWithTag("ViewerPopup"); if (viewerPopup != null && viewerPopup.GetComponent <TextMeshProUGUI>().text.Split('\n').Length > 1) { return; } } timer = TIMER_FRAMES; ToastStruct toastStruct = queue.Dequeue(); GameObject toast = Instantiate(toastPrefab, transform); toasts.Insert(0, toast); SpriteRenderer spriteRenderer = toast.transform.GetChild(0).GetComponent <SpriteRenderer>(); spriteRenderers.Insert(0, spriteRenderer); TextMeshProUGUI tmp = toast.GetComponentInChildren <TextMeshProUGUI>(); tmp.SetText(toastStruct.message); tmp.ForceMeshUpdate(); int lines = Mathf.RoundToInt(tmp.preferredHeight / 44.64f); float tmpWidth = tmp.textBounds.extents.x * 2; float borderWidth = Mathf.Lerp(1, 7, tmpWidth / 600); // text width maxes out at 600. spriteRenderer.size = new Vector2(borderWidth, .5f + lines * .5f); spriteRenderer.gameObject.transform.localPosition = new Vector3(-(7 - borderWidth) / 2 + .05f, 0, 0); toast.transform.localPosition = new Vector3(-400, (spriteRenderer.size.y - 1) * 25f); framesLeft[toast.GetInstanceID()] = 900; Image image = toast.GetComponentInChildren <Image>(); image.sprite = toastTypeIcons[(int)toastStruct.type]; sfxToast.PlayOneShot(sfxToast.clip); }