当前位置:   article > 正文

Unity 3D 捕获错误日志 打印在屏幕上_unity 获取打印错误位置

unity 获取打印错误位置

错误日志的现场往往是非常珍贵的,我们需要尽可能的将错误日志保存下来。如果是移动平台,保存和提取日志其实不方便。所以可以监听错误以及异常,并及时打印在屏幕上。

  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. public class ErrorLogOnGUIMyTools : MonoBehaviour
  4. {
  5. private List<string> m_logEntries = new List<string>();
  6. private bool m_IsVisible = false;
  7. private Rect m_WindowRect = new Rect(0, 0, Screen.width, Screen.height);
  8. private Vector2 m_scrollPositionText = Vector2.zero;
  9. private void Start()
  10. {
  11. Application.logMessageReceived += (string condition, string stackTrace, LogType type) =>
  12. {
  13. if (type == LogType.Exception || type == LogType.Error)
  14. {
  15. if (!m_IsVisible)
  16. {
  17. m_IsVisible = true;
  18. }
  19. m_logEntries.Add(string.Format("{0}\n{1}", condition, stackTrace));
  20. }
  21. };
  22. for (int i = 0; i < 30; i++)
  23. {
  24. Debug.LogError("test error!");
  25. }
  26. }
  27. void ConsoleWindow(int windowID)
  28. {
  29. GUILayout.BeginHorizontal();
  30. if (GUILayout.Button("Clear", GUILayout.MaxWidth(200), GUILayout.MaxHeight(100)))
  31. {
  32. m_logEntries.Clear();
  33. }
  34. if (GUILayout.Button("Close", GUILayout.MaxWidth(200), GUILayout.MaxHeight(100)))
  35. {
  36. m_IsVisible = false;
  37. }
  38. GUILayout.EndHorizontal();
  39. m_scrollPositionText = GUILayout.BeginScrollView(m_scrollPositionText);
  40. foreach (var entry in m_logEntries)
  41. {
  42. Color color = GUI.contentColor;
  43. GUI.contentColor = Color.red;
  44. GUILayout.TextArea(entry);
  45. GUI.contentColor = color;
  46. }
  47. GUILayout.EndScrollView();
  48. }
  49. private void OnGUI()
  50. {
  51. if (m_IsVisible)
  52. {
  53. m_WindowRect = GUILayout.Window(0, m_WindowRect, ConsoleWindow, "Console");
  54. }
  55. }
  56. }

监听 Application.logMessageReceived 事件即可捕获错误日志,利用OnGUI() 打印在屏幕上。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/112018
推荐阅读
相关标签
  

闽ICP备14008679号