Advertisement
Guest User

TextPopupMessageUIManager Project17 code review

a guest
Oct 24th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.16 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Extensions;
  5.  
  6. public class TextPopupMessageData
  7. {
  8.     public float time = 1f;
  9.     public string text;
  10.     public Color textColor = Color.white;
  11.     public Color effectColor =  new Color(0f, 0f, 0f, 0.23f);
  12.     public bool blinking = false;
  13. }
  14.  
  15. public class TextPopupMessageUIManager : CachedMonoBehaviour
  16. {
  17.     // время изчезания и появления текста
  18.     public const float FadeTime = 0.3f;
  19.     // лабэлка где показывается текст
  20.     [SerializeField] protected UILabel label;
  21.  
  22.     // Очередь сообщений для отображения
  23.     Queue<TextPopupMessageData> messagesQueue = new Queue<TextPopupMessageData>();
  24.  
  25.     // корутина в которой происходит отсчет времени для показа сообщения
  26.     private Coroutine timerCoroutine = null;
  27.     // текущая длительность показа сообщения
  28.     private float currentMessageShowTime;
  29.     // флаг возможности показа нового сообщения
  30.     private bool canShow = true;
  31.  
  32.     private void OnEnable()
  33.     {
  34.         NGUITools.SetActive(label.cachedGameObject, false);
  35.     }
  36.  
  37.     public void ShowMessage(TextPopupMessageData messageData)
  38.     {
  39.         if (canShow && timerCoroutine == null)
  40.         {
  41.             SetupLabel(messageData);
  42.             FadeIn(messageData.blinking);
  43.  
  44.             currentMessageShowTime = messagesQueue.Count > 0 ? 1f : messageData.time;
  45.             timerCoroutine = StartCoroutine(TimerCororoutine());
  46.             canShow = false;
  47.         }
  48.         else
  49.         {
  50.             currentMessageShowTime = 1f;
  51.             messagesQueue.Enqueue(messageData);
  52.         }
  53.     }
  54.  
  55.     // таймер отсчет время для сообщения
  56.     private IEnumerator TimerCororoutine()
  57.     {
  58.         float currentMessageStartedTime = Time.time;
  59.         NGUITools.SetActive(label.cachedGameObject, true);
  60.         while((currentMessageStartedTime + currentMessageShowTime) > Time.time)
  61.         {
  62.             yield return null;
  63.         }
  64.         FadeOut();
  65.         timerCoroutine = null;
  66.     }
  67.  
  68.     public void ClearQueue()
  69.     {
  70.         messagesQueue.Clear();
  71.     }
  72.  
  73.     // установка значений для лабелки
  74.     public void SetupLabel(TextPopupMessageData messageData)
  75.     {
  76.         WidgetsInitializationExtensions.InitializeLabelWithTextSafely(label, messageData.text);
  77.         label.color = messageData.textColor;
  78.         label.effectColor = messageData.effectColor;
  79.     }
  80.  
  81.     // появление сообщения. возможно задать мигания текста
  82.     public void FadeIn(bool blinking)
  83.     {
  84.         TweenAlpha ta = TweenAlpha.Begin<TweenAlpha>(label.cachedGameObject, FadeTime);
  85.         ta.delay = 0;
  86.         ta.from = 0;
  87.         ta.to = 1f;
  88.         ta.style = UITweener.Style.Once;
  89.         ta.method = UITweener.Method.EaseIn;
  90.         if (blinking)
  91.         {
  92.             EventDelegate onFinishedEventDelegate = new EventDelegate(delegate ()
  93.             {
  94.                 TweenAlpha ta2 = TweenAlpha.Begin<TweenAlpha>(label.cachedGameObject, 0.8f);
  95.                 ta2.delay = 0;
  96.                 ta2.from = 1f;
  97.                 ta2.to = 0f;
  98.                 ta2.style = UITweener.Style.PingPong;
  99.                 ta2.method = UITweener.Method.EaseOut;
  100.                 ta2.Sample(0, false);
  101.             });
  102.             onFinishedEventDelegate.oneShot = true;
  103.             ta.SetOnFinished(onFinishedEventDelegate);
  104.         }
  105.         ta.Sample(0, false);
  106.     }
  107.  
  108.     // изчезновение текста
  109.     public void FadeOut()
  110.     {
  111.         TweenAlpha ta = TweenAlpha.Begin<TweenAlpha>(label.cachedGameObject, FadeTime);
  112.         ta.delay = 0;
  113.         ta.from = ta.value;
  114.         ta.to = 0;
  115.         ta.style = UITweener.Style.Once;
  116.         ta.method = UITweener.Method.EaseOut;
  117.         EventDelegate onFinishedEventDelegate = new EventDelegate(delegate ()
  118.         {
  119.             canShow = true;
  120.             NGUITools.SetActive(label.cachedGameObject, false);
  121.             if (messagesQueue.Count > 0)
  122.             {
  123.                 ShowMessage(messagesQueue.Dequeue());
  124.             }
  125.         });
  126.         onFinishedEventDelegate.oneShot = true;
  127.         ta.SetOnFinished(onFinishedEventDelegate);
  128.         ta.Sample(0, false);
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement