Example #1
0
    /// <summary>
    /// Deletes the vertex with the specified id within the given edge.
    /// The id corresponds to the array position within the point array of the edge.
    /// </summary>
    /// <param name="vertexId">ID of the vertex that should be deleted</param>
    /// <returns>Retruns true if path is still valid</returns>
    public static bool DeleteVertex(this EdgeCollider2D edge, int vertexId)
    {
        Undo.RecordObjects(new Object[] { edge, edge.transform }, string.Format("Delete vertex in {0}", edge.name));

        // Delete polygon if only one vertex left.
        if (edge.points.Length <= 2)
        {
            return(false);
        }

        var tmp = new Vector2[edge.points.Length - 1];

        System.Array.Copy(edge.points, 0, tmp, 0, vertexId);
        System.Array.Copy(edge.points, vertexId + 1, tmp, vertexId, tmp.Length - vertexId);

        edge.points = tmp;

        edge.UpdateCenter();
        return(true);
    }