Android Mobile Debug Console

The following code allows to print out debug on screen for Android mobile device. Most of the time you can use the debug console from the IDE (xcode, Android Studio, Eclipse)  but in the case of Android, you may want to build and run as it is way faster than a Google project and running through an IDE.

In order to use the following code, the script is added to a UI object, a Text component will be added automatically if none is already there.

The component exposed the size corresponding to how many items to display and if the printing should be colourful.Based on the selected alignement of the Text component, the printing will pile up if positioned at the bottom or centered and down if positioned at the top.

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Text;

[RequireComponent(typeof(Text))]
public class Test : MonoBehaviour
{
	[Tooltip("How many items will be show")]
	[SerializeField]
	private uint stackSize = 10;
	[Tooltip("Should printing be colorful (white for debug, yellow for warning, red for errors)")]
	[SerializeField]
	private bool debugColorLogType = false;

	private Text text = null;
	private IList<string> list = new List<string> ();
	private StringBuilder sb = new StringBuilder();
	private System.Action stackDelegate;

	private void Awake()
	{
		Application.logMessageReceived += HandlelogMessageReceived;
		this.text = this.gameObject.GetComponent<Text> ();
		SetText ();
	}

	private void OnDestroy()
	{
		Application.logMessageReceived -= HandlelogMessageReceived;
	}

	private void HandlelogMessageReceived (string message, string stackTrace, LogType type)
	{
		if (this.debugColorLogType == true)
		{
			message = SetColorMessage(message, type);
		}

		this.list.Add(message);
		if(this.list.Count >= this.stackSize)
		{
			this.list.RemoveAt(0);
		}
		sb.Length = 0;
		stackDelegate ();
		this.text.text = sb.ToString();
	}

	private void LowStacking()
	{
		for(int i = 0; i < this.list.Count ;i++)
		{
			sb.Append(list[i]);
			if(i != this.list.Count -1)
			{
				sb.Append("\n");
			}
		}
	}
	private void HighStacking()
	{
		for(int i = list.Count - 1; i >= 0 ; i--)
		{
			sb.Append(list[i] +"\n");
		}
	}

	private string SetColorMessage (string message, LogType type)
	{
		sb.Append ("<color=");
		switch (type)
		{
		case LogType.Assert:
			sb.Append("blue>");
			break;
		case LogType.Error:
			sb.Append("red>");
			break;
		case LogType.Exception:
			sb.Append("red>");
			break;
		case LogType.Log:
			sb.Append("white>");
			break;
		case LogType.Warning:
			sb.Append("yellow>");
			break;
		}
		sb.Append (message);sb.Append ("</color>");
		return sb.ToString ();
	}

	private void SetText()
	{
		if (this.text == null) { return; }
		this.text.text = "";
		TextAnchor ta = this.text.alignment;
		if (ta == TextAnchor.LowerCenter || ta == TextAnchor.LowerLeft || ta == TextAnchor.LowerRight)
		{
			stackDelegate = LowStacking;
		}
		else if (ta == TextAnchor.UpperCenter || ta == TextAnchor.UpperLeft || ta == TextAnchor.UpperRight)
		{
			stackDelegate = HighStacking;
		}
		else
		{
			stackDelegate = LowStacking;
		}
	}
}