A* Pathfinding Project  4.1.9
The A* Pathfinding Project for Unity 3D
GraphUtilities Class Reference

Contains utility methods for getting useful information out of graph. More...

Detailed Description

Contains utility methods for getting useful information out of graph.

This class works a lot with the Pathfinding.GraphNode class, a useful function to get nodes is AstarPath.GetNearest.

See also
AstarPath.GetNearest
Pathfinding.GraphUpdateUtilities
Pathfinding.PathUtilities

Static Public Member Functions

static List< Vector3 > GetContours (NavGraph graph)
 Convenience method to get a list of all segments of the contours of a graph. More...
 
static void GetContours (INavmesh navmesh, System.Action< List< Int3 >, bool > results)
 Traces the contour of a navmesh. More...
 
static void GetContours (GridGraph grid, System.Action< Vector3[]> callback, float yMergeThreshold, GridNodeBase[] nodes=null)
 Finds all contours of a collection of nodes in a grid graph. More...
 

Member Function Documentation

◆ GetContours() [1/3]

static List<Vector3> GetContours ( NavGraph  graph)
static

Convenience method to get a list of all segments of the contours of a graph.

Returns
A list of segments. Every 2 elements form a line segment. The first segment is (result[0], result[1]), the second one is (result[2], result[3]) etc. The line segments are oriented so that the navmesh is on the right side of the segments when seen from above.

This method works for navmesh, recast, grid graphs and layered grid graphs. For other graph types it will return an empty list.

If you need more information about how the contours are connected you can take a look at the other variants of this method.

// Get the first graph
var navmesh = AstarPath.active.graphs[0];
// Get all contours of the graph (works for grid, navmesh and recast graphs)
var segments = GraphUtilities.GetContours(navmesh);
// Every 2 elements form a line segment. The first segment is (segments[0], segments[1]), the second one is (segments[2], segments[3]) etc.
// The line segments are oriented so that the navmesh is on the right side of the segments when seen from above.
for (int i = 0; i < segments.Count; i += 2) {
var start = segments[i];
var end = segments[i+1];
Debug.DrawLine(start, end, Color.red, 3);
}

◆ GetContours() [2/3]

static void GetContours ( INavmesh  navmesh,
System.Action< List< Int3 >, bool >  results 
)
static

Traces the contour of a navmesh.

Parameters
navmeshThe navmesh-like object to trace. This can be a recast or navmesh graph or it could be a single tile in one such graph.
resultsWill be called once for each contour with the contour as a parameter as well as a boolean indicating if the contour is a cycle or a chain (see second image).

This image is just used to illustrate the difference between chains and cycles. That it shows a grid graph is not relevant.

See also
GetContours(NavGraph)

◆ GetContours() [3/3]

static void GetContours ( GridGraph  grid,
System.Action< Vector3[]>  callback,
float  yMergeThreshold,
GridNodeBase []  nodes = null 
)
static

Finds all contours of a collection of nodes in a grid graph.

Parameters
gridThe grid to find the contours of
callbackThe callback will be called once for every contour that is found with the vertices of the contour. The contour always forms a cycle.
yMergeThresholdContours will be simplified if the y coordinates for adjacent vertices differ by no more than this value.
nodesOnly these nodes will be searched. If this parameter is null then all nodes in the grid graph will be searched.
var grid = AstarPath.active.data.gridGraph;
// Find all contours in the graph and draw them using debug lines
GraphUtilities.GetContours(grid, vertices => {
for (int i = 0; i < vertices.Length; i++) {
Debug.DrawLine(vertices[i], vertices[(i+1)%vertices.Length], Color.red, 4);
}
}, 0);

In the image below you can see the contour of a graph.

In the image below you can see the contour of just a part of a grid graph (when the nodes parameter is supplied)

Contour of a hexagon graph

See also
GetContours(NavGraph)

The documentation for this class was generated from the following file: