捕获日志——Application.RegisterLogCallback()

作者:追风剑情 发布于:2016-8-11 11:25 分类:Unity3d

  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4.  
  5. public class UITest : MonoBehaviour {
  6.  
  7. string log;
  8. string stack;
  9. string type;
  10.  
  11. void Start () {
  12. //引发一个异常
  13. //或者使用Debug.Log()、Debug.LogWarning()、Debug.LogException()、Debug.LogError()触发LogHandler()
  14. throw new Exception("引发异常");
  15. }
  16.  
  17. void OnEnable()
  18. {
  19. //注册委托
  20. Application.RegisterLogCallback(LogHandler);
  21. }
  22.  
  23. void OnDisable()
  24. {
  25. //取消委托
  26. Application.RegisterLogCallback(null);
  27. }
  28.  
  29. void OnGUI()
  30. {
  31. GUI.Label(new Rect(0, 0, 500, 1000), "LogType: " + type+"\nLog: "+log+"\nStack: "+stack);
  32. }
  33.  
  34. private void LogHandler(string log, string stack, LogType type)
  35. {
  36. //此方法中Unity禁用了Debug.Log()、Debug.LogWarning()、
  37. //Debug.LogException()、Debug.LogError()输出API,避免引起无限递归。
  38.  
  39. this.log = log;
  40. this.stack = stack;
  41. this.type = type.ToString();
  42. }
  43. }

 

运行测试

1111111.png

 

Unity定义的日志级别

  1. namespace UnityEngine
  2. {
  3. public enum LogType
  4. {
  5. Error = 0,
  6. Assert = 1,
  7. Warning = 2,
  8. Log = 3,
  9. Exception = 4,
  10. }
  11. }
 

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号