Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Extensions;
- public class TextPopupMessageData
- {
- public float time = 1f;
- public string text;
- public Color textColor = Color.white;
- public Color effectColor = new Color(0f, 0f, 0f, 0.23f);
- public bool blinking = false;
- }
- public class TextPopupMessageUIManager : CachedMonoBehaviour
- {
- // время изчезания и появления текста
- public const float FadeTime = 0.3f;
- // лабэлка где показывается текст
- [SerializeField] protected UILabel label;
- // Очередь сообщений для отображения
- Queue<TextPopupMessageData> messagesQueue = new Queue<TextPopupMessageData>();
- // корутина в которой происходит отсчет времени для показа сообщения
- private Coroutine timerCoroutine = null;
- // текущая длительность показа сообщения
- private float currentMessageShowTime;
- // флаг возможности показа нового сообщения
- private bool canShow = true;
- private void OnEnable()
- {
- NGUITools.SetActive(label.cachedGameObject, false);
- }
- public void ShowMessage(TextPopupMessageData messageData)
- {
- if (canShow && timerCoroutine == null)
- {
- SetupLabel(messageData);
- FadeIn(messageData.blinking);
- currentMessageShowTime = messagesQueue.Count > 0 ? 1f : messageData.time;
- timerCoroutine = StartCoroutine(TimerCororoutine());
- canShow = false;
- }
- else
- {
- currentMessageShowTime = 1f;
- messagesQueue.Enqueue(messageData);
- }
- }
- // таймер отсчет время для сообщения
- private IEnumerator TimerCororoutine()
- {
- float currentMessageStartedTime = Time.time;
- NGUITools.SetActive(label.cachedGameObject, true);
- while((currentMessageStartedTime + currentMessageShowTime) > Time.time)
- {
- yield return null;
- }
- FadeOut();
- timerCoroutine = null;
- }
- public void ClearQueue()
- {
- messagesQueue.Clear();
- }
- // установка значений для лабелки
- public void SetupLabel(TextPopupMessageData messageData)
- {
- WidgetsInitializationExtensions.InitializeLabelWithTextSafely(label, messageData.text);
- label.color = messageData.textColor;
- label.effectColor = messageData.effectColor;
- }
- // появление сообщения. возможно задать мигания текста
- public void FadeIn(bool blinking)
- {
- TweenAlpha ta = TweenAlpha.Begin<TweenAlpha>(label.cachedGameObject, FadeTime);
- ta.delay = 0;
- ta.from = 0;
- ta.to = 1f;
- ta.style = UITweener.Style.Once;
- ta.method = UITweener.Method.EaseIn;
- if (blinking)
- {
- EventDelegate onFinishedEventDelegate = new EventDelegate(delegate ()
- {
- TweenAlpha ta2 = TweenAlpha.Begin<TweenAlpha>(label.cachedGameObject, 0.8f);
- ta2.delay = 0;
- ta2.from = 1f;
- ta2.to = 0f;
- ta2.style = UITweener.Style.PingPong;
- ta2.method = UITweener.Method.EaseOut;
- ta2.Sample(0, false);
- });
- onFinishedEventDelegate.oneShot = true;
- ta.SetOnFinished(onFinishedEventDelegate);
- }
- ta.Sample(0, false);
- }
- // изчезновение текста
- public void FadeOut()
- {
- TweenAlpha ta = TweenAlpha.Begin<TweenAlpha>(label.cachedGameObject, FadeTime);
- ta.delay = 0;
- ta.from = ta.value;
- ta.to = 0;
- ta.style = UITweener.Style.Once;
- ta.method = UITweener.Method.EaseOut;
- EventDelegate onFinishedEventDelegate = new EventDelegate(delegate ()
- {
- canShow = true;
- NGUITools.SetActive(label.cachedGameObject, false);
- if (messagesQueue.Count > 0)
- {
- ShowMessage(messagesQueue.Dequeue());
- }
- });
- onFinishedEventDelegate.oneShot = true;
- ta.SetOnFinished(onFinishedEventDelegate);
- ta.Sample(0, false);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement