GizmoManager.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. 
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. public class GizmosS
  5. {
  6. public static void DrawLine(Vector3 a, Vector3 b, Color color)
  7. {
  8. if (!GizmoManager.Show) return;
  9. GizmoManager.lines.Add(new GizmoManager.GizmoLine(a, b, color));
  10. try
  11. {
  12. UnityEngine.Gizmos.color = color;
  13. UnityEngine.Gizmos.DrawLine(a, b);
  14. }
  15. catch (UnityException unityException)
  16. {
  17. //ignores the annoying
  18. //UnityException: Gizmo drawing functions can only be used in OnDrawGizmos and OnDrawGizmosSelected.
  19. //error
  20. }
  21. }
  22. public static void DrawBox(Vector3 position, Vector3 size, Color color)
  23. {
  24. if (!GizmoManager.Show) return;
  25. Vector3 point1 = new Vector3(position.x - size.x / 2f, position.y - size.y / 2f, position.z - size.z / 2f);
  26. Vector3 point2 = new Vector3(position.x + size.x / 2f, position.y - size.y / 2f, position.z - size.z / 2f);
  27. Vector3 point3 = new Vector3(position.x + size.x / 2f, position.y + size.y / 2f, position.z - size.z / 2f);
  28. Vector3 point4 = new Vector3(position.x - size.x / 2f, position.y + size.y / 2f, position.z - size.z / 2f);
  29. Vector3 point5 = new Vector3(position.x - size.x / 2f, position.y - size.y / 2f, position.z + size.z / 2f);
  30. Vector3 point6 = new Vector3(position.x + size.x / 2f, position.y - size.y / 2f, position.z + size.z / 2f);
  31. Vector3 point7 = new Vector3(position.x + size.x / 2f, position.y + size.y / 2f, position.z + size.z / 2f);
  32. Vector3 point8 = new Vector3(position.x - size.x / 2f, position.y + size.y / 2f, position.z + size.z / 2f);
  33. DrawLine(point1, point2, color);
  34. DrawLine(point2, point3, color);
  35. DrawLine(point3, point4, color);
  36. DrawLine(point4, point1, color);
  37. DrawLine(point5, point6, color);
  38. DrawLine(point6, point7, color);
  39. DrawLine(point7, point8, color);
  40. DrawLine(point8, point5, color);
  41. DrawLine(point1, point5, color);
  42. DrawLine(point2, point6, color);
  43. DrawLine(point3, point7, color);
  44. DrawLine(point4, point8, color);
  45. }
  46. public static void DrawSquare(Vector3 position, Vector3 size, Color color)
  47. {
  48. if (!GizmoManager.Show) return;
  49. Vector3 point1 = new Vector3(position.x - size.x / 2f, position.y - size.y / 2f, position.z);
  50. Vector3 point2 = new Vector3(position.x + size.x / 2f, position.y - size.y / 2f, position.z);
  51. Vector3 point3 = new Vector3(position.x + size.x / 2f, position.y + size.y / 2f, position.z);
  52. Vector3 point4 = new Vector3(position.x - size.x / 2f, position.y + size.y / 2f, position.z);
  53. DrawLine(point1, point2, color);
  54. DrawLine(point2, point3, color);
  55. DrawLine(point3, point4, color);
  56. DrawLine(point4, point1, color);
  57. }
  58. public static void DrawCircle(Vector3 position, float radius, Color color)
  59. {
  60. if (!GizmoManager.Show) return;
  61. DrawPolygon(position, radius, 18, color);
  62. }
  63. public static void DrawPolygon(Vector3 position, float radius, int points, Color color)
  64. {
  65. if (!GizmoManager.Show) return;
  66. float angle = 360f / points;
  67. for (int i = 0; i < points; ++i)
  68. {
  69. float sx = Mathf.Cos(Mathf.Deg2Rad * angle * i) * radius / 2;
  70. float sy = Mathf.Sin(Mathf.Deg2Rad * angle * i) * radius / 2;
  71. float nx = Mathf.Cos(Mathf.Deg2Rad * angle * (i + 1)) * radius / 2;
  72. float ny = Mathf.Sin(Mathf.Deg2Rad * angle * (i + 1)) * radius / 2;
  73. Vector3 a = new Vector3(sx, sy, position.z);
  74. Vector3 b = new Vector3(nx, ny, position.z);
  75. DrawLine(position + a, position + b, color);
  76. }
  77. }
  78. }
  79. //Must be attached to a camera.
  80. public class GizmoManager : MonoBehaviour
  81. {
  82. public struct GizmoLine
  83. {
  84. public Vector3 a;
  85. public Vector3 b;
  86. public Color color;
  87. public GizmoLine(Vector3 a, Vector3 b, Color color)
  88. {
  89. this.a = a;
  90. this.b = b;
  91. this.color = color;
  92. }
  93. }
  94. public bool showGizmos = true;
  95. public Material material;
  96. internal static List<GizmoLine> lines = new List<GizmoLine>();
  97. public static bool Show { get; private set; }
  98. private void Update()
  99. {
  100. Show = showGizmos;
  101. }
  102. void OnPostRender()
  103. {
  104. material.SetPass(0);
  105. GL.Begin(GL.LINES);
  106. for (int i = 0; i < lines.Count; i++)
  107. {
  108. GL.Color(lines[i].color);
  109. GL.Vertex(lines[i].a);
  110. GL.Vertex(lines[i].b);
  111. }
  112. GL.End();
  113. lines.Clear();
  114. }
  115. }