Skip to content

Instantly share code, notes, and snippets.

@JonathanReid
Last active August 29, 2015 14:12
Show Gist options
  • Save JonathanReid/6d951d4a976e7f4eb602 to your computer and use it in GitHub Desktop.
Save JonathanReid/6d951d4a976e7f4eb602 to your computer and use it in GitHub Desktop.
A simple wrapper class for Gizmos which quickens up the gizmos drawing process. Also allows you to draw things like circles and AABB's quickly.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace De.Bug
{
public class DebugDrawVO : ScriptableObject {
public List<Vector3> Points;
public Color Color;
public bool AutoDelete;
}
public class Draw : MonoBehaviour {
public List<DebugDrawVO> Vos = new List<DebugDrawVO>();
private List<DebugDrawVO> _removing = new List<DebugDrawVO>();
private static Draw _instance;
public static Draw Instance
{
get{
if(_instance == null)
{
_instance = FindObjectOfType<Draw>();
}
if(_instance == null)
{
GameObject go = new GameObject();
_instance = go.AddComponent<Draw>();
go.name = "Debug Draw";
}
return _instance;
}
}
public static void Line(Color color, params Vector3[] point)
{
DebugDrawVO vo = ScriptableObject.CreateInstance<DebugDrawVO>();
List<Vector3> p = new List<Vector3>();
int i = 0, l = point.Length;
for(;i<l;++i)
{
p.Add(point[i]);
}
vo.Points = p;
vo.Color = color;
vo.AutoDelete = true;
Draw.Instance.Vos.Add(vo);
}
public static void Line(Color color, List<Vector3> points, bool autoDelete = true)
{
DebugDrawVO vo = ScriptableObject.CreateInstance<DebugDrawVO>();
vo.Points = points;
vo.Color = color;
vo.AutoDelete = autoDelete;
Draw.Instance.Vos.Add(vo);
}
public static void AABB(Color color, GameObject gameObject, bool autoDelete = true)
{
if(gameObject.transform.renderer == null)
{
return;
}
DebugDrawVO vo = ScriptableObject.CreateInstance<DebugDrawVO>();
Vector3 min = gameObject.transform.renderer.bounds.min;
Vector3 max = gameObject.transform.renderer.bounds.max;
List<Vector3> p = new List<Vector3>();
p.Add(new Vector3(min.x,min.y,min.z));
p.Add(new Vector3(min.x,min.y,max.z));
p.Add(new Vector3(min.x,min.y,max.z));
p.Add(new Vector3(max.x,min.y,max.z));
p.Add(new Vector3(max.x,min.y,max.z));
p.Add(new Vector3(max.x,min.y,min.z));
p.Add(new Vector3(max.x,min.y,min.z));
p.Add(new Vector3(min.x,min.y,min.z));
p.Add(new Vector3(min.x,max.y,min.z));
p.Add(new Vector3(min.x,max.y,max.z));
p.Add(new Vector3(min.x,max.y,max.z));
p.Add(new Vector3(max.x,max.y,max.z));
p.Add(new Vector3(max.x,max.y,max.z));
p.Add(new Vector3(max.x,max.y,min.z));
p.Add(new Vector3(max.x,max.y,min.z));
p.Add(new Vector3(min.x,max.y,min.z));
p.Add(new Vector3(max.x,max.y,min.z));
p.Add(new Vector3(max.x,min.y,min.z));
p.Add(new Vector3(max.x,min.y,max.z));
p.Add(new Vector3(max.x,max.y,max.z));
p.Add(new Vector3(min.x,max.y,max.z));
p.Add(new Vector3(min.x,min.y,max.z));
vo.Points = p;
vo.Color = color;
vo.AutoDelete = autoDelete;
Draw.Instance.Vos.Add(vo);
}
public static void Circle(Color color, Vector3 point, float radius, Quaternion rotation, bool autoDelete = true)
{
Circle(color,point,radius,rotation.eulerAngles,autoDelete);
}
public static void Circle(Color color, Vector3 point, float radius, Vector3 rotation, bool autoDelete = true)
{
int segments = (int)(radius*50);
float angle = (360f) / (float)segments;
float a = angle;
List<Vector3> points = new List<Vector3>();
int i = 0, l = segments+1;
Matrix4x4 m = Matrix4x4.TRS(Vector3.zero,Quaternion.Euler(rotation),Vector3.one);
for (; i<l; ++i)
{
Vector3 pos = Vector3.zero;
float ca = radius * Mathf.Sin(angle * Mathf.Deg2Rad);
float cb = radius * Mathf.Cos(angle * Mathf.Deg2Rad);
pos = new Vector3(ca,cb,0);
pos = m.MultiplyPoint3x4(pos);
pos.x += point.x;
pos.y += point.y;
pos.z += point.z;
points.Add(pos);
angle += a;
}
DebugDrawVO vo = ScriptableObject.CreateInstance<DebugDrawVO>();
vo.Points = points;
vo.Color = color;
vo.AutoDelete = autoDelete;
Draw.Instance.Vos.Add(vo);
}
public static void Sphere(Color color, Vector3 point, float radius, bool autoDelete = true)
{
int segments = (int)(radius*50);
float angle = (360f) / (float)segments;
float a = angle;
List<Vector3> pointsa = new List<Vector3>();
List<Vector3> pointsb = new List<Vector3>();
List<Vector3> pointsc = new List<Vector3>();
int i = 0, l = (segments*3)+3;
for (; i<l; ++i)
{
Vector3 pos = Vector3.zero;
float ca = radius * Mathf.Sin(angle * Mathf.Deg2Rad);
float cb = radius * Mathf.Cos(angle * Mathf.Deg2Rad);
if(i <= segments)
{
pos = new Vector3(point.x + ca,point.y + cb, point.z);
pointsa.Add(pos);
}
else if (i <= (segments * 2)+1)
{
pos = new Vector3(point.x + ca,point.y, point.z + cb);
pointsb.Add(pos);
}
else
{
pos = new Vector3(point.x,point.y + ca, point.z + cb);
pointsc.Add(pos);
}
angle += a;
}
DebugDrawVO vo = ScriptableObject.CreateInstance<DebugDrawVO>();
vo.Points = pointsa;
vo.Color = color;
vo.AutoDelete = autoDelete;
Draw.Instance.Vos.Add(vo);
vo = ScriptableObject.CreateInstance<DebugDrawVO>();
vo.Points = pointsb;
vo.Color = color;
vo.AutoDelete = autoDelete;
Draw.Instance.Vos.Add(vo);
vo = ScriptableObject.CreateInstance<DebugDrawVO>();
vo.Points = pointsc;
vo.Color = color;
vo.AutoDelete = autoDelete;
Draw.Instance.Vos.Add(vo);
}
private void RemoveOldData(List<DebugDrawVO> vosToRemove)
{
int i = 0, l = vosToRemove.Count;
for(;i<l;++i)
{
int index = Vos.IndexOf(vosToRemove[i]);
Destroy(Vos[index]);
Vos.RemoveAt(index);
}
}
//Do the actual debug stuff in gizmos
void OnDrawGizmos()
{
if(!Debug.isDebugBuild)
{
return;
}
_removing = new List<DebugDrawVO>();
int i = 0, l = Vos.Count;
for(;i<l;++i)
{
DebugDrawVO vo = Vos[i];
if(vo.Points != null)
{
Gizmos.color = vo.Color;
int j = 1, k = vo.Points.Count;
for(;j<k;++j)
{
Gizmos.DrawLine(vo.Points[j-1],vo.Points[j]);
}
}
if(vo.AutoDelete)
{
_removing.Add(vo);
}
}
}
//need to remove the data outside of OnDrawGizmos so they draw in the game view
void LateUpdate()
{
RemoveOldData(_removing);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment