Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 30.37 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3.  
  4. public static class DebugExtensions
  5. {
  6.     #region DebugDrawFunctions
  7.  
  8.     public static void DebugPoint(Vector3 position, Color color, float scale = 1.0f, float duration = 0,
  9.         bool depthTest = true)
  10.     {
  11.         color = color == default(Color) ? Color.white : color;
  12.  
  13.         Debug.DrawRay(position + Vector3.up*(scale*0.5f), -Vector3.up*scale, color, duration, depthTest);
  14.         Debug.DrawRay(position + Vector3.right*(scale*0.5f), -Vector3.right*scale, color, duration, depthTest);
  15.         Debug.DrawRay(position + Vector3.forward*(scale*0.5f), -Vector3.forward*scale, color, duration, depthTest);
  16.     }
  17.  
  18.     public static void DebugPoint(Vector3 position, float scale = 1.0f, float duration = 0, bool depthTest = true)
  19.     {
  20.         DebugPoint(position, Color.white, scale, duration, depthTest);
  21.     }
  22.  
  23.     public static void DebugBounds(Bounds bounds, Color color, float duration = 0, bool depthTest = true)
  24.     {
  25.         var center = bounds.center;
  26.  
  27.         var x = bounds.extents.x;
  28.         var y = bounds.extents.y;
  29.         var z = bounds.extents.z;
  30.  
  31.         var ruf = center + new Vector3(x, y, z);
  32.         var rub = center + new Vector3(x, y, -z);
  33.         var luf = center + new Vector3(-x, y, z);
  34.         var lub = center + new Vector3(-x, y, -z);
  35.  
  36.         var rdf = center + new Vector3(x, -y, z);
  37.         var rdb = center + new Vector3(x, -y, -z);
  38.         var lfd = center + new Vector3(-x, -y, z);
  39.         var lbd = center + new Vector3(-x, -y, -z);
  40.  
  41.         Debug.DrawLine(ruf, luf, color, duration, depthTest);
  42.         Debug.DrawLine(ruf, rub, color, duration, depthTest);
  43.         Debug.DrawLine(luf, lub, color, duration, depthTest);
  44.         Debug.DrawLine(rub, lub, color, duration, depthTest);
  45.  
  46.         Debug.DrawLine(ruf, rdf, color, duration, depthTest);
  47.         Debug.DrawLine(rub, rdb, color, duration, depthTest);
  48.         Debug.DrawLine(luf, lfd, color, duration, depthTest);
  49.         Debug.DrawLine(lub, lbd, color, duration, depthTest);
  50.  
  51.         Debug.DrawLine(rdf, lfd, color, duration, depthTest);
  52.         Debug.DrawLine(rdf, rdb, color, duration, depthTest);
  53.         Debug.DrawLine(lfd, lbd, color, duration, depthTest);
  54.         Debug.DrawLine(lbd, rdb, color, duration, depthTest);
  55.     }
  56.  
  57.     public static void DebugBounds(Bounds bounds, float duration = 0, bool depthTest = true)
  58.     {
  59.         DebugBounds(bounds, Color.white, duration, depthTest);
  60.     }
  61.  
  62.     public static void DebugLocalCube(Transform transform, Vector3 size, Color color, Vector3 center = default(Vector3),
  63.         float duration = 0, bool depthTest = true)
  64.     {
  65.         var lbb = transform.TransformPoint(center + -size*0.5f);
  66.         var rbb = transform.TransformPoint(center + new Vector3(size.x, -size.y, -size.z)*0.5f);
  67.  
  68.         var lbf = transform.TransformPoint(center + new Vector3(size.x, -size.y, size.z)*0.5f);
  69.         var rbf = transform.TransformPoint(center + new Vector3(-size.x, -size.y, size.z)*0.5f);
  70.  
  71.         var lub = transform.TransformPoint(center + new Vector3(-size.x, size.y, -size.z)*0.5f);
  72.         var rub = transform.TransformPoint(center + new Vector3(size.x, size.y, -size.z)*0.5f);
  73.  
  74.         var luf = transform.TransformPoint(center + size*0.5f);
  75.         var ruf = transform.TransformPoint(center + new Vector3(-size.x, size.y, size.z)*0.5f);
  76.  
  77.         Debug.DrawLine(lbb, rbb, color, duration, depthTest);
  78.         Debug.DrawLine(rbb, lbf, color, duration, depthTest);
  79.         Debug.DrawLine(lbf, rbf, color, duration, depthTest);
  80.         Debug.DrawLine(rbf, lbb, color, duration, depthTest);
  81.  
  82.         Debug.DrawLine(lub, rub, color, duration, depthTest);
  83.         Debug.DrawLine(rub, luf, color, duration, depthTest);
  84.         Debug.DrawLine(luf, ruf, color, duration, depthTest);
  85.         Debug.DrawLine(ruf, lub, color, duration, depthTest);
  86.  
  87.         Debug.DrawLine(lbb, lub, color, duration, depthTest);
  88.         Debug.DrawLine(rbb, rub, color, duration, depthTest);
  89.         Debug.DrawLine(lbf, luf, color, duration, depthTest);
  90.         Debug.DrawLine(rbf, ruf, color, duration, depthTest);
  91.     }
  92.  
  93.     public static void DebugLocalCube(Transform transform, Vector3 size, Vector3 center = default(Vector3),
  94.         float duration = 0, bool depthTest = true)
  95.     {
  96.         DebugLocalCube(transform, size, Color.white, center, duration, depthTest);
  97.     }
  98.  
  99.     public static void DebugLocalCube(Matrix4x4 space, Vector3 size, Color color, Vector3 center = default(Vector3),
  100.         float duration = 0, bool depthTest = true)
  101.     {
  102.         color = color == default(Color) ? Color.white : color;
  103.  
  104.         var lbb = space.MultiplyPoint3x4(center + -size*0.5f);
  105.         var rbb = space.MultiplyPoint3x4(center + new Vector3(size.x, -size.y, -size.z)*0.5f);
  106.  
  107.         var lbf = space.MultiplyPoint3x4(center + new Vector3(size.x, -size.y, size.z)*0.5f);
  108.         var rbf = space.MultiplyPoint3x4(center + new Vector3(-size.x, -size.y, size.z)*0.5f);
  109.  
  110.         var lub = space.MultiplyPoint3x4(center + new Vector3(-size.x, size.y, -size.z)*0.5f);
  111.         var rub = space.MultiplyPoint3x4(center + new Vector3(size.x, size.y, -size.z)*0.5f);
  112.  
  113.         var luf = space.MultiplyPoint3x4(center + size*0.5f);
  114.         var ruf = space.MultiplyPoint3x4(center + new Vector3(-size.x, size.y, size.z)*0.5f);
  115.  
  116.         Debug.DrawLine(lbb, rbb, color, duration, depthTest);
  117.         Debug.DrawLine(rbb, lbf, color, duration, depthTest);
  118.         Debug.DrawLine(lbf, rbf, color, duration, depthTest);
  119.         Debug.DrawLine(rbf, lbb, color, duration, depthTest);
  120.  
  121.         Debug.DrawLine(lub, rub, color, duration, depthTest);
  122.         Debug.DrawLine(rub, luf, color, duration, depthTest);
  123.         Debug.DrawLine(luf, ruf, color, duration, depthTest);
  124.         Debug.DrawLine(ruf, lub, color, duration, depthTest);
  125.  
  126.         Debug.DrawLine(lbb, lub, color, duration, depthTest);
  127.         Debug.DrawLine(rbb, rub, color, duration, depthTest);
  128.         Debug.DrawLine(lbf, luf, color, duration, depthTest);
  129.         Debug.DrawLine(rbf, ruf, color, duration, depthTest);
  130.     }
  131.  
  132.     public static void DebugLocalCube(Matrix4x4 space, Vector3 size, Vector3 center = default(Vector3),
  133.         float duration = 0, bool depthTest = true)
  134.     {
  135.         DebugLocalCube(space, size, Color.white, center, duration, depthTest);
  136.     }
  137.  
  138.     public static void DebugCircle(Vector3 position, Vector3 up, Color color, float radius = 1.0f, float duration = 0,
  139.         bool depthTest = true)
  140.     {
  141.         var _up = up.normalized*radius;
  142.         var _forward = Vector3.Slerp(_up, -_up, 0.5f);
  143.         var _right = Vector3.Cross(_up, _forward).normalized*radius;
  144.  
  145.         var matrix = new Matrix4x4();
  146.  
  147.         matrix[0] = _right.x;
  148.         matrix[1] = _right.y;
  149.         matrix[2] = _right.z;
  150.  
  151.         matrix[4] = _up.x;
  152.         matrix[5] = _up.y;
  153.         matrix[6] = _up.z;
  154.  
  155.         matrix[8] = _forward.x;
  156.         matrix[9] = _forward.y;
  157.         matrix[10] = _forward.z;
  158.  
  159.         var _lastPoint = position + matrix.MultiplyPoint3x4(new Vector3(Mathf.Cos(0), 0, Mathf.Sin(0)));
  160.         var _nextPoint = Vector3.zero;
  161.  
  162.         color = color == default(Color) ? Color.white : color;
  163.  
  164.         for (var i = 0; i < 91; i++)
  165.         {
  166.             _nextPoint.x = Mathf.Cos(i*4*Mathf.Deg2Rad);
  167.             _nextPoint.z = Mathf.Sin(i*4*Mathf.Deg2Rad);
  168.             _nextPoint.y = 0;
  169.  
  170.             _nextPoint = position + matrix.MultiplyPoint3x4(_nextPoint);
  171.  
  172.             Debug.DrawLine(_lastPoint, _nextPoint, color, duration, depthTest);
  173.             _lastPoint = _nextPoint;
  174.         }
  175.     }
  176.  
  177.     public static void DebugCircle(Vector3 position, Color color, float radius = 1.0f, float duration = 0,
  178.         bool depthTest = true)
  179.     {
  180.         DebugCircle(position, Vector3.up, color, radius, duration, depthTest);
  181.     }
  182.  
  183.     public static void DebugCircle(Vector3 position, Vector3 up, float radius = 1.0f, float duration = 0,
  184.         bool depthTest = true)
  185.     {
  186.         DebugCircle(position, up, Color.white, radius, duration, depthTest);
  187.     }
  188.  
  189.     public static void DebugCircle(Vector3 position, float radius = 1.0f, float duration = 0, bool depthTest = true)
  190.     {
  191.         DebugCircle(position, Vector3.up, Color.white, radius, duration, depthTest);
  192.     }
  193.  
  194.     public static void DebugWireSphere(Vector3 position, Color color, float radius = 1.0f, float duration = 0,
  195.         bool depthTest = true)
  196.     {
  197.         var angle = 10.0f;
  198.  
  199.         var x = new Vector3(position.x, position.y + radius*Mathf.Sin(0), position.z + radius*Mathf.Cos(0));
  200.         var y = new Vector3(position.x + radius*Mathf.Cos(0), position.y, position.z + radius*Mathf.Sin(0));
  201.         var z = new Vector3(position.x + radius*Mathf.Cos(0), position.y + radius*Mathf.Sin(0), position.z);
  202.  
  203.         Vector3 new_x;
  204.         Vector3 new_y;
  205.         Vector3 new_z;
  206.  
  207.         for (var i = 1; i < 37; i++)
  208.         {
  209.             new_x = new Vector3(position.x, position.y + radius*Mathf.Sin(angle*i*Mathf.Deg2Rad),
  210.                 position.z + radius*Mathf.Cos(angle*i*Mathf.Deg2Rad));
  211.             new_y = new Vector3(position.x + radius*Mathf.Cos(angle*i*Mathf.Deg2Rad), position.y,
  212.                 position.z + radius*Mathf.Sin(angle*i*Mathf.Deg2Rad));
  213.             new_z = new Vector3(position.x + radius*Mathf.Cos(angle*i*Mathf.Deg2Rad),
  214.                 position.y + radius*Mathf.Sin(angle*i*Mathf.Deg2Rad), position.z);
  215.  
  216.             Debug.DrawLine(x, new_x, color, duration, depthTest);
  217.             Debug.DrawLine(y, new_y, color, duration, depthTest);
  218.             Debug.DrawLine(z, new_z, color, duration, depthTest);
  219.  
  220.             x = new_x;
  221.             y = new_y;
  222.             z = new_z;
  223.         }
  224.     }
  225.  
  226.     public static void DebugWireSphere(Vector3 position, float radius = 1.0f, float duration = 0, bool depthTest = true)
  227.     {
  228.         DebugWireSphere(position, Color.white, radius, duration, depthTest);
  229.     }
  230.  
  231.     public static void DebugCylinder(Vector3 start, Vector3 end, Color color, float radius = 1, float duration = 0,
  232.         bool depthTest = true)
  233.     {
  234.         var up = (end - start).normalized*radius;
  235.         var forward = Vector3.Slerp(up, -up, 0.5f);
  236.         var right = Vector3.Cross(up, forward).normalized*radius;
  237.  
  238.         DebugCircle(start, up, color, radius, duration, depthTest);
  239.         DebugCircle(end, -up, color, radius, duration, depthTest);
  240.         DebugCircle((start + end)*0.5f, up, color, radius, duration, depthTest);
  241.  
  242.         Debug.DrawLine(start + right, end + right, color, duration, depthTest);
  243.         Debug.DrawLine(start - right, end - right, color, duration, depthTest);
  244.  
  245.         Debug.DrawLine(start + forward, end + forward, color, duration, depthTest);
  246.         Debug.DrawLine(start - forward, end - forward, color, duration, depthTest);
  247.  
  248.         Debug.DrawLine(start - right, start + right, color, duration, depthTest);
  249.         Debug.DrawLine(start - forward, start + forward, color, duration, depthTest);
  250.  
  251.         Debug.DrawLine(end - right, end + right, color, duration, depthTest);
  252.         Debug.DrawLine(end - forward, end + forward, color, duration, depthTest);
  253.     }
  254.  
  255.     public static void DebugCylinder(Vector3 start, Vector3 end, float radius = 1, float duration = 0,
  256.         bool depthTest = true)
  257.     {
  258.         DebugCylinder(start, end, Color.white, radius, duration, depthTest);
  259.     }
  260.  
  261.     public static void DebugCone(Vector3 position, Vector3 direction, Color color, float angle = 45, float duration = 0,
  262.         bool depthTest = true)
  263.     {
  264.         var length = direction.magnitude;
  265.  
  266.         var _forward = direction;
  267.         var _up = Vector3.Slerp(_forward, -_forward, 0.5f);
  268.         var _right = Vector3.Cross(_forward, _up).normalized*length;
  269.  
  270.         direction = direction.normalized;
  271.  
  272.         var slerpedVector = Vector3.Slerp(_forward, _up, angle/90.0f);
  273.  
  274.         float dist;
  275.         var farPlane = new Plane(-direction, position + _forward);
  276.         var distRay = new Ray(position, slerpedVector);
  277.  
  278.         farPlane.Raycast(distRay, out dist);
  279.  
  280.         Debug.DrawRay(position, slerpedVector.normalized*dist, color);
  281.         Debug.DrawRay(position, Vector3.Slerp(_forward, -_up, angle/90.0f).normalized*dist, color, duration, depthTest);
  282.         Debug.DrawRay(position, Vector3.Slerp(_forward, _right, angle/90.0f).normalized*dist, color, duration, depthTest);
  283.         Debug.DrawRay(position, Vector3.Slerp(_forward, -_right, angle/90.0f).normalized*dist, color, duration,
  284.             depthTest);
  285.  
  286.         DebugCircle(position + _forward, direction, color, (_forward - slerpedVector.normalized*dist).magnitude,
  287.             duration, depthTest);
  288.         DebugCircle(position + _forward*0.5f, direction, color,
  289.             (_forward*0.5f - slerpedVector.normalized*(dist*0.5f)).magnitude, duration, depthTest);
  290.     }
  291.  
  292.     public static void DebugCone(Vector3 position, Vector3 direction, float angle = 45, float duration = 0,
  293.         bool depthTest = true)
  294.     {
  295.         DebugCone(position, direction, Color.white, angle, duration, depthTest);
  296.     }
  297.  
  298.     public static void DebugCone(Vector3 position, Color color, float angle = 45, float duration = 0,
  299.         bool depthTest = true)
  300.     {
  301.         DebugCone(position, Vector3.up, color, angle, duration, depthTest);
  302.     }
  303.  
  304.     public static void DebugCone(Vector3 position, float angle = 45, float duration = 0, bool depthTest = true)
  305.     {
  306.         DebugCone(position, Vector3.up, Color.white, angle, duration, depthTest);
  307.     }
  308.  
  309.     public static void DebugArrow(Vector3 position, Vector3 direction, Color color, float duration = 0,
  310.         bool depthTest = true)
  311.     {
  312.         Debug.DrawRay(position, direction, color, duration, depthTest);
  313.         DebugCone(position + direction, -direction*0.333f, color, 15, duration, depthTest);
  314.     }
  315.  
  316.     public static void DebugArrow(Vector3 position, Vector3 direction, float duration = 0, bool depthTest = true)
  317.     {
  318.         DebugArrow(position, direction, Color.white, duration, depthTest);
  319.     }
  320.  
  321.     public static void DebugCapsule(Vector3 start, float height, Color color, float radius = 1, int direction = 0,
  322.         float duration = 0, bool depthTest = true)
  323.     {
  324.         var capsuleDirection = Vector3.up;
  325.  
  326.         if (direction == 0)
  327.             capsuleDirection = Vector3.right;
  328.         else if (direction == 1)
  329.             capsuleDirection = Vector3.up;
  330.         else if (direction == 2)
  331.             capsuleDirection = Vector3.forward;
  332.  
  333.         var end = start + capsuleDirection.normalized*height;
  334.  
  335.         var up = (end - start).normalized*radius;
  336.         var forward = Vector3.Slerp(up, -up, 0.5f);
  337.         var right = Vector3.Cross(up, forward).normalized*radius;
  338.  
  339.         var sideLength = Mathf.Max(0, height*0.5f - radius);
  340.         var middle = (end + start)*0.5f;
  341.  
  342.         start = middle + (start - middle).normalized*sideLength;
  343.         end = middle + (end - middle).normalized*sideLength;
  344.  
  345.         DebugCircle(start, up, color, radius, duration, depthTest);
  346.         DebugCircle(end, -up, color, radius, duration, depthTest);
  347.  
  348.         Debug.DrawLine(start + right, end + right, color, duration, depthTest);
  349.         Debug.DrawLine(start - right, end - right, color, duration, depthTest);
  350.  
  351.         Debug.DrawLine(start + forward, end + forward, color, duration, depthTest);
  352.         Debug.DrawLine(start - forward, end - forward, color, duration, depthTest);
  353.  
  354.         for (var i = 1; i < 26; i++)
  355.         {
  356.             Debug.DrawLine(Vector3.Slerp(right, -up, i/25.0f) + start, Vector3.Slerp(right, -up, (i - 1)/25.0f) + start,
  357.                 color, duration, depthTest);
  358.             Debug.DrawLine(Vector3.Slerp(-right, -up, i/25.0f) + start,
  359.                 Vector3.Slerp(-right, -up, (i - 1)/25.0f) + start, color, duration, depthTest);
  360.             Debug.DrawLine(Vector3.Slerp(forward, -up, i/25.0f) + start,
  361.                 Vector3.Slerp(forward, -up, (i - 1)/25.0f) + start, color, duration, depthTest);
  362.             Debug.DrawLine(Vector3.Slerp(-forward, -up, i/25.0f) + start,
  363.                 Vector3.Slerp(-forward, -up, (i - 1)/25.0f) + start, color, duration, depthTest);
  364.  
  365.             Debug.DrawLine(Vector3.Slerp(right, up, i/25.0f) + end, Vector3.Slerp(right, up, (i - 1)/25.0f) + end, color,
  366.                 duration, depthTest);
  367.             Debug.DrawLine(Vector3.Slerp(-right, up, i/25.0f) + end, Vector3.Slerp(-right, up, (i - 1)/25.0f) + end,
  368.                 color, duration, depthTest);
  369.             Debug.DrawLine(Vector3.Slerp(forward, up, i/25.0f) + end, Vector3.Slerp(forward, up, (i - 1)/25.0f) + end,
  370.                 color, duration, depthTest);
  371.             Debug.DrawLine(Vector3.Slerp(-forward, up, i/25.0f) + end, Vector3.Slerp(-forward, up, (i - 1)/25.0f) + end,
  372.                 color, duration, depthTest);
  373.         }
  374.     }
  375.  
  376.     public static void DebugCapsule(Vector3 start, float height, float radius = 1, int direction = 0, float duration = 0,
  377.         bool depthTest = true)
  378.     {
  379.         DebugCapsule(start, height, Color.white, radius, direction, duration, depthTest);
  380.     }
  381.  
  382.     #endregion
  383.  
  384.     #region GizmoDrawFunctions
  385.  
  386.     public static void DrawPoint(Vector3 position, Color color, float scale = 1.0f)
  387.     {
  388.         var oldColor = Gizmos.color;
  389.  
  390.         Gizmos.color = color;
  391.         Gizmos.DrawRay(position + Vector3.up*(scale*0.5f), -Vector3.up*scale);
  392.         Gizmos.DrawRay(position + Vector3.right*(scale*0.5f), -Vector3.right*scale);
  393.         Gizmos.DrawRay(position + Vector3.forward*(scale*0.5f), -Vector3.forward*scale);
  394.  
  395.         Gizmos.color = oldColor;
  396.     }
  397.  
  398.     public static void DrawPoint(Vector3 position, float scale = 1.0f)
  399.     {
  400.         DrawPoint(position, Color.white, scale);
  401.     }
  402.  
  403.     public static void DrawBounds(Bounds bounds, Color color)
  404.     {
  405.         var center = bounds.center;
  406.  
  407.         var x = bounds.extents.x;
  408.         var y = bounds.extents.y;
  409.         var z = bounds.extents.z;
  410.  
  411.         var ruf = center + new Vector3(x, y, z);
  412.         var rub = center + new Vector3(x, y, -z);
  413.         var luf = center + new Vector3(-x, y, z);
  414.         var lub = center + new Vector3(-x, y, -z);
  415.  
  416.         var rdf = center + new Vector3(x, -y, z);
  417.         var rdb = center + new Vector3(x, -y, -z);
  418.         var lfd = center + new Vector3(-x, -y, z);
  419.         var lbd = center + new Vector3(-x, -y, -z);
  420.  
  421.         var oldColor = Gizmos.color;
  422.         Gizmos.color = color;
  423.  
  424.         Gizmos.DrawLine(ruf, luf);
  425.         Gizmos.DrawLine(ruf, rub);
  426.         Gizmos.DrawLine(luf, lub);
  427.         Gizmos.DrawLine(rub, lub);
  428.  
  429.         Gizmos.DrawLine(ruf, rdf);
  430.         Gizmos.DrawLine(rub, rdb);
  431.         Gizmos.DrawLine(luf, lfd);
  432.         Gizmos.DrawLine(lub, lbd);
  433.  
  434.         Gizmos.DrawLine(rdf, lfd);
  435.         Gizmos.DrawLine(rdf, rdb);
  436.         Gizmos.DrawLine(lfd, lbd);
  437.         Gizmos.DrawLine(lbd, rdb);
  438.  
  439.         Gizmos.color = oldColor;
  440.     }
  441.  
  442.     public static void DrawBounds(Bounds bounds)
  443.     {
  444.         DrawBounds(bounds, Color.white);
  445.     }
  446.  
  447.     public static void DrawLocalCube(Transform transform, Vector3 size, Color color, Vector3 center = default(Vector3))
  448.     {
  449.         var oldColor = Gizmos.color;
  450.         Gizmos.color = color;
  451.  
  452.         var lbb = transform.TransformPoint(center + -size*0.5f);
  453.         var rbb = transform.TransformPoint(center + new Vector3(size.x, -size.y, -size.z)*0.5f);
  454.  
  455.         var lbf = transform.TransformPoint(center + new Vector3(size.x, -size.y, size.z)*0.5f);
  456.         var rbf = transform.TransformPoint(center + new Vector3(-size.x, -size.y, size.z)*0.5f);
  457.  
  458.         var lub = transform.TransformPoint(center + new Vector3(-size.x, size.y, -size.z)*0.5f);
  459.         var rub = transform.TransformPoint(center + new Vector3(size.x, size.y, -size.z)*0.5f);
  460.  
  461.         var luf = transform.TransformPoint(center + size*0.5f);
  462.         var ruf = transform.TransformPoint(center + new Vector3(-size.x, size.y, size.z)*0.5f);
  463.  
  464.         Gizmos.DrawLine(lbb, rbb);
  465.         Gizmos.DrawLine(rbb, lbf);
  466.         Gizmos.DrawLine(lbf, rbf);
  467.         Gizmos.DrawLine(rbf, lbb);
  468.  
  469.         Gizmos.DrawLine(lub, rub);
  470.         Gizmos.DrawLine(rub, luf);
  471.         Gizmos.DrawLine(luf, ruf);
  472.         Gizmos.DrawLine(ruf, lub);
  473.  
  474.         Gizmos.DrawLine(lbb, lub);
  475.         Gizmos.DrawLine(rbb, rub);
  476.         Gizmos.DrawLine(lbf, luf);
  477.         Gizmos.DrawLine(rbf, ruf);
  478.  
  479.         Gizmos.color = oldColor;
  480.     }
  481.  
  482.     public static void DrawLocalCube(Transform transform, Vector3 size, Vector3 center = default(Vector3))
  483.     {
  484.         DrawLocalCube(transform, size, Color.white, center);
  485.     }
  486.  
  487.     public static void DrawLocalCube(Matrix4x4 space, Vector3 size, Color color, Vector3 center = default(Vector3))
  488.     {
  489.         var oldColor = Gizmos.color;
  490.         Gizmos.color = color;
  491.  
  492.         var lbb = space.MultiplyPoint3x4(center + -size*0.5f);
  493.         var rbb = space.MultiplyPoint3x4(center + new Vector3(size.x, -size.y, -size.z)*0.5f);
  494.  
  495.         var lbf = space.MultiplyPoint3x4(center + new Vector3(size.x, -size.y, size.z)*0.5f);
  496.         var rbf = space.MultiplyPoint3x4(center + new Vector3(-size.x, -size.y, size.z)*0.5f);
  497.  
  498.         var lub = space.MultiplyPoint3x4(center + new Vector3(-size.x, size.y, -size.z)*0.5f);
  499.         var rub = space.MultiplyPoint3x4(center + new Vector3(size.x, size.y, -size.z)*0.5f);
  500.  
  501.         var luf = space.MultiplyPoint3x4(center + size*0.5f);
  502.         var ruf = space.MultiplyPoint3x4(center + new Vector3(-size.x, size.y, size.z)*0.5f);
  503.  
  504.         Gizmos.DrawLine(lbb, rbb);
  505.         Gizmos.DrawLine(rbb, lbf);
  506.         Gizmos.DrawLine(lbf, rbf);
  507.         Gizmos.DrawLine(rbf, lbb);
  508.  
  509.         Gizmos.DrawLine(lub, rub);
  510.         Gizmos.DrawLine(rub, luf);
  511.         Gizmos.DrawLine(luf, ruf);
  512.         Gizmos.DrawLine(ruf, lub);
  513.  
  514.         Gizmos.DrawLine(lbb, lub);
  515.         Gizmos.DrawLine(rbb, rub);
  516.         Gizmos.DrawLine(lbf, luf);
  517.         Gizmos.DrawLine(rbf, ruf);
  518.  
  519.         Gizmos.color = oldColor;
  520.     }
  521.  
  522.     public static void DrawLocalCube(Matrix4x4 space, Vector3 size, Vector3 center = default(Vector3))
  523.     {
  524.         DrawLocalCube(space, size, Color.white, center);
  525.     }
  526.  
  527.     public static void DrawCircle(Vector3 position, Vector3 up, Color color, float radius = 1.0f)
  528.     {
  529.         up = (up == Vector3.zero ? Vector3.up : up).normalized*radius;
  530.         var _forward = Vector3.Slerp(up, -up, 0.5f);
  531.         var _right = Vector3.Cross(up, _forward).normalized*radius;
  532.  
  533.         var matrix = new Matrix4x4();
  534.  
  535.         matrix[0] = _right.x;
  536.         matrix[1] = _right.y;
  537.         matrix[2] = _right.z;
  538.  
  539.         matrix[4] = up.x;
  540.         matrix[5] = up.y;
  541.         matrix[6] = up.z;
  542.  
  543.         matrix[8] = _forward.x;
  544.         matrix[9] = _forward.y;
  545.         matrix[10] = _forward.z;
  546.  
  547.         var _lastPoint = position + matrix.MultiplyPoint3x4(new Vector3(Mathf.Cos(0), 0, Mathf.Sin(0)));
  548.         var _nextPoint = Vector3.zero;
  549.  
  550.         var oldColor = Gizmos.color;
  551.         Gizmos.color = color == default(Color) ? Color.white : color;
  552.  
  553.         for (var i = 0; i < 91; i++)
  554.         {
  555.             _nextPoint.x = Mathf.Cos(i*4*Mathf.Deg2Rad);
  556.             _nextPoint.z = Mathf.Sin(i*4*Mathf.Deg2Rad);
  557.             _nextPoint.y = 0;
  558.  
  559.             _nextPoint = position + matrix.MultiplyPoint3x4(_nextPoint);
  560.  
  561.             Gizmos.DrawLine(_lastPoint, _nextPoint);
  562.             _lastPoint = _nextPoint;
  563.         }
  564.  
  565.         Gizmos.color = oldColor;
  566.     }
  567.  
  568.     public static void DrawCircle(Vector3 position, Color color, float radius = 1.0f)
  569.     {
  570.         DrawCircle(position, Vector3.up, color, radius);
  571.     }
  572.  
  573.     public static void DrawCircle(Vector3 position, Vector3 up, float radius = 1.0f)
  574.     {
  575.         DrawCircle(position, position, Color.white, radius);
  576.     }
  577.  
  578.     public static void DrawCircle(Vector3 position, float radius = 1.0f)
  579.     {
  580.         DrawCircle(position, Vector3.up, Color.white, radius);
  581.     }
  582.  
  583.     public static void DrawCylinder(Vector3 start, Vector3 end, Color color, float radius = 1.0f)
  584.     {
  585.         var up = (end - start).normalized*radius;
  586.         var forward = Vector3.Slerp(up, -up, 0.5f);
  587.         var right = Vector3.Cross(up, forward).normalized*radius;
  588.  
  589.         DrawCircle(start, up, color, radius);
  590.         DrawCircle(end, -up, color, radius);
  591.         DrawCircle((start + end)*0.5f, up, color, radius);
  592.  
  593.         var oldColor = Gizmos.color;
  594.         Gizmos.color = color;
  595.  
  596.         Gizmos.DrawLine(start + right, end + right);
  597.         Gizmos.DrawLine(start - right, end - right);
  598.  
  599.         Gizmos.DrawLine(start + forward, end + forward);
  600.         Gizmos.DrawLine(start - forward, end - forward);
  601.  
  602.         Gizmos.DrawLine(start - right, start + right);
  603.         Gizmos.DrawLine(start - forward, start + forward);
  604.  
  605.         Gizmos.DrawLine(end - right, end + right);
  606.         Gizmos.DrawLine(end - forward, end + forward);
  607.  
  608.         Gizmos.color = oldColor;
  609.     }
  610.  
  611.     public static void DrawCylinder(Vector3 start, Vector3 end, float radius = 1.0f)
  612.     {
  613.         DrawCylinder(start, end, Color.white, radius);
  614.     }
  615.  
  616.     public static void DrawCone(Vector3 position, Vector3 direction, Color color, float angle = 45)
  617.     {
  618.         var length = direction.magnitude;
  619.  
  620.         var _forward = direction;
  621.         var _up = Vector3.Slerp(_forward, -_forward, 0.5f);
  622.         var _right = Vector3.Cross(_forward, _up).normalized*length;
  623.  
  624.         direction = direction.normalized;
  625.  
  626.         var slerpedVector = Vector3.Slerp(_forward, _up, angle/90.0f);
  627.  
  628.         float dist;
  629.         var farPlane = new Plane(-direction, position + _forward);
  630.         var distRay = new Ray(position, slerpedVector);
  631.  
  632.         farPlane.Raycast(distRay, out dist);
  633.  
  634.         var oldColor = Gizmos.color;
  635.         Gizmos.color = color;
  636.  
  637.         Gizmos.DrawRay(position, slerpedVector.normalized*dist);
  638.         Gizmos.DrawRay(position, Vector3.Slerp(_forward, -_up, angle/90.0f).normalized*dist);
  639.         Gizmos.DrawRay(position, Vector3.Slerp(_forward, _right, angle/90.0f).normalized*dist);
  640.         Gizmos.DrawRay(position, Vector3.Slerp(_forward, -_right, angle/90.0f).normalized*dist);
  641.  
  642.         DrawCircle(position + _forward, direction, color, (_forward - slerpedVector.normalized*dist).magnitude);
  643.         DrawCircle(position + _forward*0.5f, direction, color,
  644.             (_forward*0.5f - slerpedVector.normalized*(dist*0.5f)).magnitude);
  645.  
  646.         Gizmos.color = oldColor;
  647.     }
  648.  
  649.     public static void DrawCone(Vector3 position, Vector3 direction, float angle = 45)
  650.     {
  651.         DrawCone(position, direction, Color.white, angle);
  652.     }
  653.  
  654.     public static void DrawCone(Vector3 position, Color color, float angle = 45)
  655.     {
  656.         DrawCone(position, Vector3.up, color, angle);
  657.     }
  658.  
  659.     public static void DrawCone(Vector3 position, float angle = 45)
  660.     {
  661.         DrawCone(position, Vector3.up, Color.white, angle);
  662.     }
  663.  
  664.     public static void DrawArrow(Vector3 position, Vector3 direction, Color color)
  665.     {
  666.         var oldColor = Gizmos.color;
  667.         Gizmos.color = color;
  668.  
  669.         Gizmos.DrawRay(position, direction);
  670.         DrawCone(position + direction, -direction*0.333f, color, 15);
  671.  
  672.         Gizmos.color = oldColor;
  673.     }
  674.  
  675.     public static void DrawArrow(Vector3 position, Vector3 direction)
  676.     {
  677.         DrawArrow(position, direction, Color.white);
  678.     }
  679.  
  680.     public static void DrawCapsule(Vector3 start, float height, Color color, float radius = 1f, int direction = 0)
  681.     {
  682.         var capsuleDirection = Vector3.up;
  683.  
  684.         if (direction == 0)
  685.             capsuleDirection = Vector3.right;
  686.         else if (direction == 1)
  687.             capsuleDirection = Vector3.up;
  688.         else if (direction == 2)
  689.             capsuleDirection = Vector3.forward;
  690.  
  691.         var end = start + capsuleDirection.normalized*height;
  692.  
  693.         var up = (end - start).normalized*radius;
  694.         var forward = Vector3.Slerp(up, -up, 0.5f);
  695.         var right = Vector3.Cross(up, forward).normalized*radius;
  696.  
  697.         var oldColor = Gizmos.color;
  698.         Gizmos.color = color;
  699.  
  700.         var sideLength = Mathf.Max(0, height*0.5f - radius);
  701.         var middle = (end + start)*0.5f;
  702.  
  703.         start = middle + (start - middle).normalized*sideLength;
  704.         end = middle + (end - middle).normalized*sideLength;
  705.  
  706.         DrawCircle(start, up, color, radius);
  707.         DrawCircle(end, -up, color, radius);
  708.  
  709.         Gizmos.DrawLine(start + right, end + right);
  710.         Gizmos.DrawLine(start - right, end - right);
  711.  
  712.         Gizmos.DrawLine(start + forward, end + forward);
  713.         Gizmos.DrawLine(start - forward, end - forward);
  714.  
  715.         for (var i = 1; i < 26; i++)
  716.         {
  717.             Gizmos.DrawLine(Vector3.Slerp(right, -up, i/25.0f) + start, Vector3.Slerp(right, -up, (i - 1)/25.0f) + start);
  718.             Gizmos.DrawLine(Vector3.Slerp(-right, -up, i/25.0f) + start,
  719.                 Vector3.Slerp(-right, -up, (i - 1)/25.0f) + start);
  720.             Gizmos.DrawLine(Vector3.Slerp(forward, -up, i/25.0f) + start,
  721.                 Vector3.Slerp(forward, -up, (i - 1)/25.0f) + start);
  722.             Gizmos.DrawLine(Vector3.Slerp(-forward, -up, i/25.0f) + start,
  723.                 Vector3.Slerp(-forward, -up, (i - 1)/25.0f) + start);
  724.  
  725.             Gizmos.DrawLine(Vector3.Slerp(right, up, i/25.0f) + end, Vector3.Slerp(right, up, (i - 1)/25.0f) + end);
  726.             Gizmos.DrawLine(Vector3.Slerp(-right, up, i/25.0f) + end, Vector3.Slerp(-right, up, (i - 1)/25.0f) + end);
  727.             Gizmos.DrawLine(Vector3.Slerp(forward, up, i/25.0f) + end, Vector3.Slerp(forward, up, (i - 1)/25.0f) + end);
  728.             Gizmos.DrawLine(Vector3.Slerp(-forward, up, i/25.0f) + end, Vector3.Slerp(-forward, up, (i - 1)/25.0f) + end);
  729.         }
  730.  
  731.         Gizmos.color = oldColor;
  732.     }
  733.  
  734.     public static void DrawCapsule(Vector3 start, float height, float radius = 1, int direction = 0)
  735.     {
  736.         DrawCapsule(start, height, Color.white, radius, direction);
  737.     }
  738.  
  739.     #endregion
  740.  
  741.     #region DebugFunctions
  742.  
  743.     public static string MethodsOfObject(object obj, bool includeInfo = false)
  744.     {
  745.         var methods = "";
  746.         var methodInfos = obj.GetType().GetMethods();
  747.         for (var i = 0; i < methodInfos.Length; i++)
  748.         {
  749.             if (includeInfo)
  750.                 methods += methodInfos[i] + "\n";
  751.  
  752.             else methods += methodInfos[i].Name + "\n";
  753.         }
  754.  
  755.         return methods;
  756.     }
  757.  
  758.     public static string MethodsOfType(Type type, bool includeInfo = false)
  759.     {
  760.         var methods = "";
  761.         var methodInfos = type.GetMethods();
  762.         for (var i = 0; i < methodInfos.Length; i++)
  763.         {
  764.             if (includeInfo)
  765.                 methods += methodInfos[i] + "\n";
  766.  
  767.             else methods += methodInfos[i].Name + "\n";
  768.         }
  769.  
  770.         return methods;
  771.     }
  772.  
  773.     #endregion
  774. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement