Message Queue

 

What is it?

A simple event based debug message queue. Easily display messages on-screen for a set amount of time from anywhere in your codebase. I use it to quickly show debug messages on mobile devices.

To show a message, simply raise a message event:

Events.instance.Raise (new MessageEvent ("Show this message for 3 seconds", Time.time + 3f, MessagePriority.High));

The colour of messages changes based on the priority.

A message event has associated information such as time raised, how long to display the message, and the message priority.

public interface IMessageEvent
{
DateTime timeRaised { get; }
float displayTime { get; }
MessagePriority priority { get; }
object message { get; }
}

The complete message queue code:

public class MessageQueue : MonoBehaviour
{
public bool logToConsole = false;
public bool prependDateTime = false;
[Header ("Message Colours")]
public Color
highPriorityColour = Color.red;
public Color normalPriorityColour = Color.black;
public Color lowPriorityColour = Color.white;
[Header ("Message Font Style")]
public FontStyle
highPriorityStyle = FontStyle.Bold;
public FontStyle normalPriorityStyle = FontStyle.Normal;
public FontStyle lowPriorityStyle = FontStyle.Normal;
[Header ("Message Location")]
public Vector2
queueLocation = new Vector2 (25, 25); 
public Vector2 messageSize = new Vector2 (200, 15); 
private static readonly GUIStyle LOW_PRIORTY = new GUIStyle (), NORMAL_PRIORITY = new GUIStyle (), HIGH_PRIORITY = new GUIStyle ();
private List<IMessageEvent> _pending = new List<IMessageEvent> ();
void OnEnable ()
{
if (_pending.Count > 0) {
_pending.Clear ();
}
LOW_PRIORTY.normal.textColor = lowPriorityColour;
LOW_PRIORTY.fontStyle = lowPriorityStyle;
NORMAL_PRIORITY.normal.textColor = normalPriorityColour;
NORMAL_PRIORITY.fontStyle = normalPriorityStyle;
HIGH_PRIORITY.normal.textColor = highPriorityColour;
HIGH_PRIORITY.fontStyle = highPriorityStyle;
Events.instance.AddListener<MessageEvent> (OnMessage);
}
void OnDisable ()
{
Events.instance.RemoveListener<MessageEvent> (OnMessage);
}
void Update ()
{
for (int i = _pending.Count - 1; i>=0; i--) {
if (Time.time > _pending [i].displayTime)
_pending.RemoveAt (i);
}
}
void OnMessage (IMessageEvent e)
{
_pending.Add (e);
if (logToConsole) {
Debug.Log ("Message Recieved [" + System.DateTime.Now + "]: " + e.message.ToString ());
}
}
void OnGUI ()
{
float yPos = queueLocation.y;
foreach (var m in _pending) {
GUIStyle style = GetMessageStyle (m);
string message = (prependDateTime) ? "[" + m.timeRaised + "]: " + m.message.ToString () : m.message.ToString ();
GUI.Label (new Rect (queueLocation.x, yPos, messageSize.x, messageSize.y), message, style);
yPos += messageSize.y;
}
}
GUIStyle GetMessageStyle (IMessageEvent e)
{
switch (e.priority) {
case MessagePriority.Low:
return LOW_PRIORTY;
case MessagePriority.Medium:
return NORMAL_PRIORITY;
default:
return HIGH_PRIORITY;
}
}
}

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *