嗚嗚喔學習筆記: Unity Print Log to Screen

搜尋此網誌

2019年4月30日 星期二

Unity Print Log to Screen

      
using UnityEngine;
using System.Collections;

public class MyLog : MonoBehaviour
{
 string myLog;
 Queue myLogQueue = new Queue();

 void Start()
 {
  Debug.Log("Im Log");
  Debug.LogWarning("Im Warning Log");
  Debug.LogError("Im Error Log");
 }

 void OnEnable()
 {
  Application.logMessageReceived += HandleLog;
 }

 void OnDisable()
 {
  Application.logMessageReceived -= HandleLog;
 }

 void HandleLog(string logString, string stackTrace, LogType type)
 {
  myLog = logString;
  string newString = "\n [" + type + "] : " + myLog;
  switch (type)
  {
   case LogType.Error:
   case LogType.Exception:
    newString = "" + newString + "";
    break;
   case LogType.Warning:
    newString = "" + newString + "";
    break;
   default:
    newString = "" + newString + "";
    break;
  }

  myLogQueue.Enqueue(newString);
  if (type == LogType.Exception)
  {
   newString = "" + "\n" + stackTrace + "";
   myLogQueue.Enqueue(newString);
  }
  myLog = string.Empty;
  foreach (string mylog in myLogQueue)
  {
   myLog += mylog;
  }
 }

 void OnGUI()
 {
  GUILayout.Label(myLog);
 }
}

沒有留言:

張貼留言