123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
-
- using UnityEngine;
- using System.Collections.Generic;
- public class GizmosS
- {
- public static void DrawLine(Vector3 a, Vector3 b, Color color)
- {
- if (!GizmoManager.Show) return;
- GizmoManager.lines.Add(new GizmoManager.GizmoLine(a, b, color));
- try
- {
- UnityEngine.Gizmos.color = color;
- UnityEngine.Gizmos.DrawLine(a, b);
- }
- catch (UnityException unityException)
- {
- //ignores the annoying
- //UnityException: Gizmo drawing functions can only be used in OnDrawGizmos and OnDrawGizmosSelected.
- //error
- }
- }
- public static void DrawBox(Vector3 position, Vector3 size, Color color)
- {
- if (!GizmoManager.Show) return;
- Vector3 point1 = new Vector3(position.x - size.x / 2f, position.y - size.y / 2f, position.z - size.z / 2f);
- Vector3 point2 = new Vector3(position.x + size.x / 2f, position.y - size.y / 2f, position.z - size.z / 2f);
- Vector3 point3 = new Vector3(position.x + size.x / 2f, position.y + size.y / 2f, position.z - size.z / 2f);
- Vector3 point4 = new Vector3(position.x - size.x / 2f, position.y + size.y / 2f, position.z - size.z / 2f);
- Vector3 point5 = new Vector3(position.x - size.x / 2f, position.y - size.y / 2f, position.z + size.z / 2f);
- Vector3 point6 = new Vector3(position.x + size.x / 2f, position.y - size.y / 2f, position.z + size.z / 2f);
- Vector3 point7 = new Vector3(position.x + size.x / 2f, position.y + size.y / 2f, position.z + size.z / 2f);
- Vector3 point8 = new Vector3(position.x - size.x / 2f, position.y + size.y / 2f, position.z + size.z / 2f);
- DrawLine(point1, point2, color);
- DrawLine(point2, point3, color);
- DrawLine(point3, point4, color);
- DrawLine(point4, point1, color);
- DrawLine(point5, point6, color);
- DrawLine(point6, point7, color);
- DrawLine(point7, point8, color);
- DrawLine(point8, point5, color);
- DrawLine(point1, point5, color);
- DrawLine(point2, point6, color);
- DrawLine(point3, point7, color);
- DrawLine(point4, point8, color);
- }
- public static void DrawSquare(Vector3 position, Vector3 size, Color color)
- {
- if (!GizmoManager.Show) return;
- Vector3 point1 = new Vector3(position.x - size.x / 2f, position.y - size.y / 2f, position.z);
- Vector3 point2 = new Vector3(position.x + size.x / 2f, position.y - size.y / 2f, position.z);
- Vector3 point3 = new Vector3(position.x + size.x / 2f, position.y + size.y / 2f, position.z);
- Vector3 point4 = new Vector3(position.x - size.x / 2f, position.y + size.y / 2f, position.z);
- DrawLine(point1, point2, color);
- DrawLine(point2, point3, color);
- DrawLine(point3, point4, color);
- DrawLine(point4, point1, color);
- }
- public static void DrawCircle(Vector3 position, float radius, Color color)
- {
- if (!GizmoManager.Show) return;
- DrawPolygon(position, radius, 18, color);
- }
- public static void DrawPolygon(Vector3 position, float radius, int points, Color color)
- {
- if (!GizmoManager.Show) return;
- float angle = 360f / points;
- for (int i = 0; i < points; ++i)
- {
- float sx = Mathf.Cos(Mathf.Deg2Rad * angle * i) * radius / 2;
- float sy = Mathf.Sin(Mathf.Deg2Rad * angle * i) * radius / 2;
- float nx = Mathf.Cos(Mathf.Deg2Rad * angle * (i + 1)) * radius / 2;
- float ny = Mathf.Sin(Mathf.Deg2Rad * angle * (i + 1)) * radius / 2;
- Vector3 a = new Vector3(sx, sy, position.z);
- Vector3 b = new Vector3(nx, ny, position.z);
- DrawLine(position + a, position + b, color);
- }
- }
- }
- //Must be attached to a camera.
- public class GizmoManager : MonoBehaviour
- {
- public struct GizmoLine
- {
- public Vector3 a;
- public Vector3 b;
- public Color color;
- public GizmoLine(Vector3 a, Vector3 b, Color color)
- {
- this.a = a;
- this.b = b;
- this.color = color;
- }
- }
- public bool showGizmos = true;
- public Material material;
- internal static List<GizmoLine> lines = new List<GizmoLine>();
- public static bool Show { get; private set; }
- private void Update()
- {
- Show = showGizmos;
- }
- void OnPostRender()
- {
- material.SetPass(0);
- GL.Begin(GL.LINES);
- for (int i = 0; i < lines.Count; i++)
- {
- GL.Color(lines[i].color);
- GL.Vertex(lines[i].a);
- GL.Vertex(lines[i].b);
- }
- GL.End();
- lines.Clear();
- }
- }
|