You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
4.7 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// Static class for adding More Debug funtions
  6. /// </summary>
  7. public static class DebugExtensions
  8. {
  9. /// <summary>
  10. /// Draws wireframe cube in scene view
  11. /// </summary>
  12. /// <param name="Position">Centre of cube</param>
  13. /// <param name="Extent">extent of cube (half of size)</param>
  14. /// <param name="color">Color of lines</param>
  15. /// <param name="duration">Amount of time cube is displayed</param>
  16. /// <param name="depthTest">Should cube be obsured by otherobjects</param>
  17. public static void DrawCube(Vector3 Position, Vector3 Extent, Color color, float duration, bool depthTest = true, bool isGizmo = false)
  18. {
  19. Vector3 min = Position - Extent;
  20. Vector3 max = Position + Extent;
  21. if (isGizmo)
  22. {
  23. Color oldColor = Gizmos.color;
  24. Gizmos.color = color;
  25. Gizmos.DrawWireCube(Position, Extent * 2);
  26. Gizmos.color = oldColor;
  27. return;
  28. }
  29. //Draw bottom Square
  30. Debug.DrawLine(new Vector3(min.x, min.y, min.z), new Vector3(min.x, min.y, max.z), color, duration, depthTest);
  31. Debug.DrawLine(new Vector3(min.x, min.y, max.z), new Vector3(max.x, min.y, max.z), color, duration, depthTest);
  32. Debug.DrawLine(new Vector3(max.x, min.y, max.z), new Vector3(max.x, min.y, min.z), color, duration, depthTest);
  33. Debug.DrawLine(new Vector3(max.x, min.y, min.z), new Vector3(min.x, min.y, min.z), color, duration, depthTest);
  34. //top square
  35. Debug.DrawLine(new Vector3(min.x, max.y, min.z), new Vector3(min.x, max.y, max.z), color, duration, depthTest);
  36. Debug.DrawLine(new Vector3(min.x, max.y, max.z), new Vector3(max.x, max.y, max.z), color, duration, depthTest);
  37. Debug.DrawLine(new Vector3(max.x, max.y, max.z), new Vector3(max.x, max.y, min.z), color, duration, depthTest);
  38. Debug.DrawLine(new Vector3(max.x, max.y, min.z), new Vector3(min.x, max.y, min.z), color, duration, depthTest);
  39. //connect top and bottom
  40. Debug.DrawLine(new Vector3(min.x, min.y, min.z), new Vector3(min.x, max.y, min.z), color, duration, depthTest);
  41. Debug.DrawLine(new Vector3(min.x, min.y, max.z), new Vector3(min.x, max.y, max.z), color, duration, depthTest);
  42. Debug.DrawLine(new Vector3(max.x, min.y, max.z), new Vector3(max.x, max.y, max.z), color, duration, depthTest);
  43. Debug.DrawLine(new Vector3(max.x, min.y, min.z), new Vector3(max.x, max.y, min.z), color, duration, depthTest);
  44. }
  45. /// <summary>
  46. /// Draws wireframe cube in scene view
  47. /// </summary>
  48. /// <param name="Position">Centre of cube</param>
  49. /// <param name="Extent">extent of cube (half of size)</param>
  50. /// <param name="color">Color of lines</param>
  51. /// <param name="duration">Amount of time cube is displayed</param>
  52. public static void DrawCube(Vector3 Position, float Extent, Color color, float duration, bool isGizmo = false)
  53. {
  54. //Call overload funtion
  55. DrawCube(Position, Vector3.one * Extent, color, duration,false);
  56. }
  57. /// <summary>
  58. /// Draws wireframe around a RectTransform in scene view
  59. /// </summary>
  60. /// <param name="rect">Rect to draw around</param>
  61. /// <param name="color">Color of the lines</param>
  62. /// <param name="duration">How long the lines should be visible for</param>
  63. /// <param name="depthTest">Should the lines be obscured by objects closer to the camera</param>
  64. public static void DrawRect(Rect rect, Color color, float duration = 0.0f, bool depthTest = false)
  65. {
  66. Debug.DrawLine(new Vector3(rect.min.x, rect.max.y, 0), new Vector3(rect.max.x, rect.max.y, 0), color, duration, depthTest);
  67. Debug.DrawLine(new Vector3(rect.max.x, rect.max.y, 0), new Vector3(rect.max.x, rect.min.y, 0), color, duration, depthTest);
  68. Debug.DrawLine(new Vector3(rect.max.x, rect.min.y, 0), new Vector3(rect.min.x, rect.min.y, 0), color, duration, depthTest);
  69. Debug.DrawLine(new Vector3(rect.min.x, rect.min.y, 0), new Vector3(rect.min.x, rect.max.y, 0), color, duration, depthTest);
  70. }
  71. /// <summary>
  72. /// Draws wireframe around a RectTransform in scene view
  73. /// </summary>
  74. /// <param name="transform">Rect transform to draw around</param>
  75. /// <param name="color">Color of the lines</param>
  76. /// <param name="duration">How long the lines should be visible for</param>
  77. /// <param name="depthTest">Should the lines be obscured by objects closer to the camera</param>
  78. public static void DrawRect(RectTransform transform, Color color, float duration = 0.0f, bool depthTest = false)
  79. {
  80. Rect rect = transform.GlobalRect();
  81. DrawRect(rect, color, duration, depthTest);
  82. }
  83. }//End DebugExtentions