Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- namespace De.Bug
- {
- public class Draw : MonoBehaviour {
- public List<DebugVO> Vos = new List<DebugVO>();
- 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)
- {
- DebugVO vo = ScriptableObject.CreateInstance<DebugVO>();
- 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)
- {
- DebugVO vo = ScriptableObject.CreateInstance<DebugVO>();
- 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;
- }
- DebugVO vo = ScriptableObject.CreateInstance<DebugVO>();
- 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;
- }
- DebugVO vo = ScriptableObject.CreateInstance<DebugVO>();
- 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;
- }
- DebugVO vo = ScriptableObject.CreateInstance<DebugVO>();
- vo.Points = pointsa;
- vo.Color = color;
- vo.AutoDelete = autoDelete;
- Draw.Instance.Vos.Add(vo);
- vo = ScriptableObject.CreateInstance<DebugVO>();
- vo.Points = pointsb;
- vo.Color = color;
- vo.AutoDelete = autoDelete;
- Draw.Instance.Vos.Add(vo);
- vo = ScriptableObject.CreateInstance<DebugVO>();
- vo.Points = pointsc;
- vo.Color = color;
- vo.AutoDelete = autoDelete;
- Draw.Instance.Vos.Add(vo);
- }
- //Do the actual debug stuff in gizmos
- void OnDrawGizmos()
- {
- int i = 0, l = Vos.Count;
- List<DebugVO> removing = new List<DebugVO>();
- for(;i<l;++i)
- {
- DebugVO 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);
- }
- }
- i = 0; l = removing.Count;
- for(;i<l;++i)
- {
- int index = Vos.IndexOf(removing[i]);
- Destroy(Vos[index]);
- Vos.RemoveAt(index);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement