DebugUtility.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.Diagnostics;
  3. using UnityEngine;
  4. namespace kt.Utility
  5. {
  6. public static class DebugUtility
  7. {
  8. public static void LogErrorFromException(Exception exception)
  9. {
  10. DebugUtility.LogErrorFromException(null, exception);
  11. }
  12. public static void LogErrorFromException(string message, Exception exception)
  13. {
  14. if (!string.IsNullOrEmpty(message))
  15. {
  16. UnityEngine.Debug.LogError(message);
  17. }
  18. UnityEngine.Debug.LogException(exception);
  19. }
  20. public static void MessageBox(string f_strTitle, string f_strMsg)
  21. {
  22. NUty.WinMessageBox(NUty.GetWindowHandle(), f_strMsg, f_strTitle, 0);
  23. }
  24. public static void DrawAxis(Vector3 pos, Quaternion rot, Color x_color, Color y_color, Color z_color, float line_length = 0.0625f)
  25. {
  26. UnityEngine.Debug.DrawLine(pos, pos + rot * Vector3.right * line_length, x_color);
  27. UnityEngine.Debug.DrawLine(pos, pos + rot * Vector3.up * line_length, y_color);
  28. UnityEngine.Debug.DrawLine(pos, pos + rot * Vector3.forward * line_length, z_color);
  29. }
  30. public static void DrawAxis(Vector3 pos, Quaternion rot, float line_length = 0.0625f)
  31. {
  32. DebugUtility.DrawAxis(pos, rot, Color.red, Color.green, Color.blue, line_length);
  33. }
  34. public static void DrawObjAxis(Transform obj_trans, Color x_color, Color y_color, Color z_color, float line_length = 0.0625f)
  35. {
  36. DebugUtility.DrawAxis(obj_trans.position, obj_trans.rotation, x_color, y_color, z_color, line_length);
  37. }
  38. public static void DrawObjAxis(Transform obj_trans, float ray_length = 0.0625f)
  39. {
  40. DebugUtility.DrawObjAxis(obj_trans, Color.red, Color.green, Color.blue, ray_length);
  41. }
  42. public static void DrawSquare(Vector3 pos, Quaternion rot, Vector3 forward_axis, Color axis_col, Color square_col, float size = 0.025f)
  43. {
  44. UnityEngine.Debug.DrawRay(pos, rot * forward_axis * size, axis_col);
  45. rot *= Quaternion.FromToRotation(Vector3.forward, forward_axis);
  46. Vector3 vector = pos + rot * (Vector3.right + Vector3.up) * size;
  47. Vector3 vector2 = pos + rot * (Vector3.right + Vector3.down) * size;
  48. Vector3 vector3 = pos + rot * (Vector3.left + Vector3.down) * size;
  49. Vector3 vector4 = pos + rot * (Vector3.left + Vector3.up) * size;
  50. UnityEngine.Debug.DrawLine(vector, vector2, square_col);
  51. UnityEngine.Debug.DrawLine(vector2, vector3, square_col);
  52. UnityEngine.Debug.DrawLine(vector3, vector4, square_col);
  53. UnityEngine.Debug.DrawLine(vector4, vector, square_col);
  54. }
  55. public static void DrawSquare(Vector3 pos, Quaternion rot, Vector3 forward_axis, float size = 0.025f)
  56. {
  57. DebugUtility.DrawSquare(pos, rot, forward_axis, Color.blue, Color.white, size);
  58. }
  59. public static void DrawSquare(Vector3 pos, Quaternion rot, float size = 0.025f)
  60. {
  61. DebugUtility.DrawSquare(pos, rot, Vector3.forward, Color.blue, Color.white, size);
  62. }
  63. public static void Vec3DetailLog(Vector3 vec)
  64. {
  65. UnityEngine.Debug.LogFormat("({0}, {1}, {2})", new object[]
  66. {
  67. vec.x,
  68. vec.y,
  69. vec.z
  70. });
  71. }
  72. public static class Assert
  73. {
  74. public static void IsTrue(bool condition)
  75. {
  76. DebugUtility.Assert.Call(condition, "Assertion failed. Value was False.");
  77. }
  78. public static void IsTrue(bool condition, string message)
  79. {
  80. DebugUtility.Assert.Call(condition, message);
  81. }
  82. public static void IsFalse(bool condition)
  83. {
  84. DebugUtility.Assert.Call(!condition, "Assertion failed. Value was True.");
  85. }
  86. public static void IsFalse(bool condition, string message)
  87. {
  88. DebugUtility.Assert.Call(!condition, message);
  89. }
  90. public static void IsNull<T>(T value) where T : class
  91. {
  92. DebugUtility.Assert.Call(value == null, "Assertion failed. Value was not Null.");
  93. }
  94. public static void IsNull(UnityEngine.Object value, string message)
  95. {
  96. DebugUtility.Assert.Call(value == null, message);
  97. }
  98. public static void IsNull<T>(T value, string message) where T : class
  99. {
  100. DebugUtility.Assert.Call(value == null, message);
  101. }
  102. public static void IsNotNull<T>(T value) where T : class
  103. {
  104. DebugUtility.Assert.Call(value != null, "Assertion failed. Value was Null.");
  105. }
  106. public static void IsNotNull(UnityEngine.Object value, string message)
  107. {
  108. DebugUtility.Assert.Call(value != null, message);
  109. }
  110. public static void IsNotNull<T>(T value, string message) where T : class
  111. {
  112. DebugUtility.Assert.Call(value != null, message);
  113. }
  114. public static void Call(bool condition, string message)
  115. {
  116. if (condition)
  117. {
  118. return;
  119. }
  120. DebugUtility.Assert.Call(message);
  121. }
  122. public static void Call(string message)
  123. {
  124. if (message == null)
  125. {
  126. message = string.Empty;
  127. }
  128. StackFrame stackFrame = new StackFrame(1, true);
  129. string fileName = stackFrame.GetFileName();
  130. int fileLineNumber = stackFrame.GetFileLineNumber();
  131. string f_strMsg = string.Concat(new object[]
  132. {
  133. fileName,
  134. "(",
  135. fileLineNumber,
  136. ") \n",
  137. message
  138. });
  139. UnityEngine.Debug.LogError("Assert! " + message);
  140. DebugUtility.MessageBox("Error", f_strMsg);
  141. UnityEngine.Debug.Break();
  142. Application.Quit();
  143. }
  144. public static void Call(bool condition, Func<string> messageCallback)
  145. {
  146. if (condition)
  147. {
  148. return;
  149. }
  150. DebugUtility.Assert.Call(messageCallback);
  151. }
  152. public static void Call(Func<string> messageCallback)
  153. {
  154. StackFrame stackFrame = new StackFrame(1, true);
  155. string fileName = stackFrame.GetFileName();
  156. int fileLineNumber = stackFrame.GetFileLineNumber();
  157. string text = (messageCallback == null) ? string.Empty : messageCallback();
  158. string f_strMsg = string.Concat(new object[]
  159. {
  160. fileName,
  161. "(",
  162. fileLineNumber,
  163. ") \n",
  164. text
  165. });
  166. UnityEngine.Debug.LogError("Assert! " + text);
  167. DebugUtility.MessageBox("Error", f_strMsg);
  168. UnityEngine.Debug.Break();
  169. Application.Quit();
  170. }
  171. }
  172. }
  173. }