DrawPolygon() public static method

Draws a polygon, defined by all verteces of the polygon, using Debug.Draw
public static DrawPolygon ( Vector2 worldPoints, Color color, float duration = 0.01f ) : void
worldPoints UnityEngine.Vector2 World points, defining each vertex of the polygon in world space.
color UnityEngine.Color Color for Debug.Draw.
duration float Duration to show the polygon.
return void
コード例 #1
0
        /// <summary>
        /// Draws the PolyCollider2D using DebugDraw
        /// </summary>
        /// <param name="polyCollider">Poly collider instance.</param>
        /// <param name="color">Color for DebugDraw.</param>
        public static void DebugDraw(this PolygonCollider2D polyCollider, Color color)
        {
            if (polyCollider.pathCount >= 1)
            {
                Transform colliderTransform = polyCollider.transform;
                Vector2   position          = colliderTransform.position;
                Vector2[] path            = polyCollider.GetPath(0);
                Vector2[] transformedPath = new Vector2[path.Length];

                // Get transformed Offset
                Vector3 transformedOffset = colliderTransform.TransformVector(polyCollider.offset);

                // Transform the points in the path
                for (int i = 0; i < path.Length; i++)
                {
                    transformedPath[i]  = colliderTransform.TransformVector(path[i]);
                    transformedPath[i] += position + (Vector2)transformedOffset;
                }

                DebugUtility.DrawPolygon(transformedPath, color);
            }
        }
コード例 #2
0
        /// <summary>
        /// Draws the BoxCollider2D using DebugDraw
        /// </summary>
        /// <param name="boxCollider">Box collider instance.</param>
        /// <param name="color">Color for DebugDraw.</param>
        public static void DebugDraw(this BoxCollider2D boxCollider, Color color)
        {
            // Define the corners about the origin
            Vector2 halfSize = boxCollider.size * 0.5f;
            Vector2 cornerTL = new Vector2(-halfSize.x, halfSize.y);
            Vector2 cornerBR = new Vector2(halfSize.x, -halfSize.y);
            Vector2 cornerTR = new Vector2(halfSize.x, halfSize.y);
            Vector2 cornerBL = new Vector2(-halfSize.x, -halfSize.y);

            // Transform the corners
            Transform boxcolliderTransform = boxCollider.transform;

            cornerTL = boxcolliderTransform.TransformVector(cornerTL);
            cornerBR = boxcolliderTransform.TransformVector(cornerBR);
            cornerBL = boxcolliderTransform.TransformVector(cornerBL);
            cornerTR = boxcolliderTransform.TransformVector(cornerTR);

            // Get offset transformed
            Vector2 offset   = boxcolliderTransform.TransformVector(boxCollider.offset);
            Vector2 position = boxcolliderTransform.position;

            cornerTL += position + offset;
            cornerBR += position + offset;
            cornerBL += position + offset;
            cornerTR += position + offset;

            DebugUtility.DrawPolygon(
                new Vector2[]
            {
                cornerTL,
                cornerTR,
                cornerBR,
                cornerBL
            },
                color);
        }