Messaging System – NGUI Edition

I needed a way to update a specific label on my player’s GUI without having to reference the UILabel each time I wanted to update the text. Below is a quick system I built that will do this all via a static class.

using UnityEngine;
using System.Collections;

public class MessagingSystem : MonoBehaviour {

  public static UILabel messagingLabel;
  public static UILabel storyLabel;

  void Awake () {
    messagingLabel = GameObject.FindGameObjectWithTag(Tags.messagingLabel).GetComponent<UILabel>();
    storyLabel = GameObject.FindGameObjectWithTag(Tags.storyLabel).GetComponent<UILabel>();
    messagingLabel.alpha = 0;
    storyLabel.alpha = 0;
  }
  
  public static void MessageSend(string text, string messageType) {
    messagingLabel.text = text.ToString();
    if(messageType == "FadeInOut") {
      StaticCoroutine.DoCoroutine(MessageSystemFadeInOut());
    } else if(messageType == "FadeIn") {
      StaticCoroutine.DoCoroutine(MessageSystemFadeIn());
    } else if(messageType == "FadeOut") {
      StaticCoroutine.DoCoroutine(MessageSystemFadeOut());
    }
  }

  public static void StorySend(string text) {
    Destroy(storyLabel.gameObject.GetComponent<TypewriterEffect>());
    storyLabel.text = text.ToString();
    storyLabel.alpha = 1;
    storyLabel.gameObject.AddComponent("TypewriterEffect");
  }

  public static IEnumerator MessageSystemFadeInOut () {
    TweenAlpha.Begin(messagingLabel.gameObject, 0.5f, 1f);
    yield return new WaitForSeconds(1.5f);
    TweenAlpha.Begin(messagingLabel.gameObject, 0.5f, 0f);
  }

  public static IEnumerator MessageSystemFadeIn () {
    TweenAlpha.Begin(messagingLabel.gameObject, 0.5f, 1f);
    yield return null;
  }

  public static IEnumerator MessageSystemFadeOut () {
    yield return new WaitForSeconds(1.5f);
    TweenAlpha.Begin(messagingLabel.gameObject, 0.5f, 0f);
  }
}