赞
踩
错误日志的现场往往是非常珍贵的,我们需要尽可能的将错误日志保存下来。如果是移动平台,保存和提取日志其实不方便。所以可以监听错误以及异常,并及时打印在屏幕上。
- using System.Collections.Generic;
- using UnityEngine;
-
- public class ErrorLogOnGUIMyTools : MonoBehaviour
- {
- private List<string> m_logEntries = new List<string>();
-
- private bool m_IsVisible = false;
-
- private Rect m_WindowRect = new Rect(0, 0, Screen.width, Screen.height);
-
- private Vector2 m_scrollPositionText = Vector2.zero;
-
- private void Start()
- {
- Application.logMessageReceived += (string condition, string stackTrace, LogType type) =>
- {
- if (type == LogType.Exception || type == LogType.Error)
- {
- if (!m_IsVisible)
- {
- m_IsVisible = true;
- }
- m_logEntries.Add(string.Format("{0}\n{1}", condition, stackTrace));
- }
- };
-
- for (int i = 0; i < 30; i++)
- {
- Debug.LogError("test error!");
- }
- }
-
- void ConsoleWindow(int windowID)
- {
- GUILayout.BeginHorizontal();
- if (GUILayout.Button("Clear", GUILayout.MaxWidth(200), GUILayout.MaxHeight(100)))
- {
- m_logEntries.Clear();
- }
- if (GUILayout.Button("Close", GUILayout.MaxWidth(200), GUILayout.MaxHeight(100)))
- {
- m_IsVisible = false;
- }
- GUILayout.EndHorizontal();
-
- m_scrollPositionText = GUILayout.BeginScrollView(m_scrollPositionText);
-
- foreach (var entry in m_logEntries)
- {
- Color color = GUI.contentColor;
- GUI.contentColor = Color.red;
- GUILayout.TextArea(entry);
- GUI.contentColor = color;
- }
- GUILayout.EndScrollView();
- }
-
- private void OnGUI()
- {
- if (m_IsVisible)
- {
- m_WindowRect = GUILayout.Window(0, m_WindowRect, ConsoleWindow, "Console");
- }
- }
- }
监听 Application.logMessageReceived 事件即可捕获错误日志,利用OnGUI() 打印在屏幕上。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。