亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從代碼獲取控制臺記錄的消息

從代碼獲取控制臺記錄的消息

C#
繁星淼淼 2023-08-13 16:02:33
我想知道是否有任何方法可以從代碼中檢索運行時在控制臺中記錄的消息。我正在 Android 上部署一個應用程序,據我所知,控制臺只能在開發版本下打印,而我希望它在穩定版本下打印。
查看完整描述

1 回答

?
幕布斯7119047

TA貢獻1794條經驗 獲得超8個贊

LogCallback您可以使用一個類并添加帶有簽名的回調方法Application.logMessageReceived和/或Application.logMessageReceivedThreaded

并使用初始化類[RuntimeInitializeOnLoadMethod]

例如,用于在運行時收集所有日志輸出

public static class DebugListener

{

? ? public static List<LogEntry> logs = new List<LogEntry>();


? ? [RuntimeInitializeOnLoadMethod]

? ? private static void InitializeOnLoad()

? ? {

? ? ? ? // removing the callback first makes sure it is only added once

? ? ? ? Application.logMessageReceived -= HandleLog;

? ? ? ? Application.logMessageReceived += HandleLog;

? ? }


? ? private static void HandleLog(string logString, string stackTrace, LogType type)

? ? {

? ? ? ? logs.Add(new LogEntry(logString, stackTrace, type));

? ? }

}


[Serializable]

public class LogEntry

{

? ? public string Message;

? ? public string StackTrace;

? ? public LogType Type;


? ? // default constructor is required for serialization

? ? public LogEntry() { }


? ? public LogEntry(string message, string stackTrace, LogType type)

? ? {

? ? ? ? Message = message;

? ? ? ? StackTrace = stackTrace;

? ? ? ? Type = type;

? ? }

}

當然,您不僅可以將它們收集在列表中,HandleLog還可以使用接收到的日志數據,例如將其添加到組件UI.Text


或者,直接顯示文本的最簡單解決方案也是使用之前的方法,但在 MonoBehaviour 組件中,并使用和顯示OnGUI文本GUI.Label

public class DebugListener : MonoBehaviour

{

? ? private string lastMessage;

? ? private string lastStackTrace;

? ? private LogType lastType;


? ? private void OnEnable()

? ? {

? ? ? ? Application.logMessageReceived += HandleLog;

? ? }


? ? private void OnDisable()

? ? {

? ? ? ? Application.logMessageReceived -= HandleLog;

? ? }


? ? private void HandleLog(string message, string stack, LogType type)

? ? {

? ? ? ? lastMessage = message;

? ? ? ? lastStackTrace = stack;

? ? ? ? lastType = type;

? ? }


? ? private void OnGUI()

? ? {

? ? ? ? if(string.IsNullOrEmpty(lastMessage)) return;


? ? ? ? // show text at certain offset from left top corner

? ? ? ? // and certain size

? ? ? ? // e.g. simply place it in the center of the screen?

? ? ? ? // and make it overlay the entire screen

? ? ? ? GUI.Label(new Rect(Screen.width / 2f, Screen.height / 2f, Screen.width, Screen.height), $"{lastType}\n{lastMessage}\n{lastStackTrace}");

? ? }

}


查看完整回答
反對 回復 2023-08-13
  • 1 回答
  • 0 關注
  • 131 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號