CDebug.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using UnityEngine;
  2. using System.Runtime.InteropServices;
  3. using System;
  4. /// <summary>CDebug 类</summary>
  5. public class CDebug
  6. {
  7. /// <summary>是否为调试模式</summary>
  8. public static bool IsDebug = true;
  9. /// <summary>打印普通日志</summary>
  10. public static void Log(string log, UnityEngine.Object logObject = null)
  11. {
  12. if (!IsDebug)
  13. {
  14. return;
  15. }
  16. if (logObject != null)
  17. Debug.Log(log);
  18. else
  19. Debug.Log(log, logObject);
  20. }
  21. public static void LogObj(System.Object x)
  22. {
  23. Debug.Log(x.GetType().GetProperties().Length + " ");
  24. foreach (System.Reflection.PropertyInfo p in x.GetType().GetProperties())
  25. {
  26. var xx = p.Name;
  27. var yy = p.GetValue(x, null);
  28. Debug.Log(xx + " ->" + yy);
  29. }
  30. }
  31. /// <summary>打印错误日志</summary>
  32. public static void LogError(string logError, UnityEngine.Object logObject = null)
  33. {
  34. if (!IsDebug)
  35. {
  36. return;
  37. }
  38. if (logObject != null)
  39. Debug.LogError(logError);
  40. else
  41. Debug.LogError(logError, logObject);
  42. }
  43. /// <summary>打印警告日志</summary>
  44. public static void LogWarning(string logWarning, UnityEngine.Object logObject = null)
  45. {
  46. if (!IsDebug)
  47. {
  48. return;
  49. }
  50. if (logObject != null)
  51. Debug.LogWarning(logWarning);
  52. else
  53. Debug.LogWarning(logWarning, logObject);
  54. }
  55. /// <summary>打印异常日志</summary>
  56. public static void LogException(System.Exception exception, UnityEngine.Object logObject = null)
  57. {
  58. if (!IsDebug)
  59. {
  60. return;
  61. }
  62. if (logObject != null)
  63. Debug.LogException(exception);
  64. else
  65. Debug.LogException(exception, logObject);
  66. }
  67. /// <summary>打印某个对象的内存地址</summary>
  68. public static void LogMemory(System.Object debugObject, string log = "")
  69. {
  70. if (!IsDebug)
  71. {
  72. return;
  73. }
  74. GCHandle gcHandle = System.Runtime.InteropServices.GCHandle.Alloc(debugObject, GCHandleType.Pinned);
  75. IntPtr addr = gcHandle.AddrOfPinnedObject();
  76. CDebug.Log(log + "0x" + addr.ToString());
  77. }
  78. }