GetEdgesAndNeighbors(PolygonId, NativeSlice<Vector3>, NativeSlice<PolygonId>, NativeSlice<byte>, out int, out int)
Retrieves the vertices of a given node and the identifiers of all the navigation nodes to which it connects.
Read time 6 minutesLast updated 21 days ago
Definition
- Type: Method
- Namespace: UnityEngine.Experimental.AI
- Assembly: UnityEngine.AIModule
GetEdgesAndNeighbors(PolygonId, NativeSlice<Vector3>, NativeSlice<PolygonId>, NativeSlice<byte>, int, int)
public PathQueryStatus GetEdgesAndNeighbors(PolygonId node, NativeSlice<Vector3> edgeVertices, NativeSlice<PolygonId> neighbors, NativeSlice<byte> edgeIndices, out int verticesCount, out int neighborsCount)
Parameters
Identifier of a node from a NavMesh surface, NavMeshLink or OffMeshLink for which the vertices and neighbors need to be retrieved.
The result buffer that contains the world positions describing the geometry of the input navigation . It can have zero capacity.
nodePolygonal nodes of the NavMesh have a minimum of 3 and a maximum of 6 vertices.
OffMeshConnection nodes are always represented by 4 vertices, regardless of their width.
The result buffer that holds the identifiers of all the navigation nodes immediately reachable from the given . It can have zero capacity.
nodeThe helper result buffer that maps each neighbor node to an edge of the given . It can have zero capacity.
nodeThe index of an element in is also an index in the array and the value of that element is an index in the array.
edgeIndicesneighborsedgeIndicesedgeVerticesThe total number of vertices that describe the geometry of the input . This is independent of the capacity of the result buffer.
nodeverticesThe total number of navigation nodes the input connects to. This is independent of the capacity of the result buffers ( and ).
nodeneighborsedgeIndicesReturns
Type | Description |
|---|---|
| PathQueryStatus | |
Remarks
A polygon of a NavMesh surface connects to all other neighboring polygons with which it shares an edge as well as all the OffMeshLinks or NavMeshLinks that leave from anywhere on its surface. The polygon does not connect to other polygons with which it shares only a vertex.
Each point returned in the array represents the start of a 's edge and the subsequent element in the array is the end point of that edge. All vertices form a closed polygonal line. The last and first elements define the last edge.
edgeVerticesnodeAn off-mesh link connects to all the NavMesh polygons that each end of the link intersects with, regardless of whether the link is unidirectional.
For link nodes the returned array contains two pairs of points at indices [0]-[1] and [2]-[3] that define the end points of the start and end edges of the link, in this order. These are the world positions established at the moment when the link is instantiated in the NavMesh world. For nodes added through NavMesh Link or OffMesh Link components the pairs contain the same value in both of their elements.
edgeVerticesA node from the array lies at the edge returned in at the same index.
neighborsedgeIndicesIf both the given and its neighbor are NavMesh polygons, then the corresponding value represents the index of the polygon edge that leads from to the neighbor. E.g. represents the start point of the edge that is common between and the node, and is the end point of that edge.
nodeedgeIndicesnodeedgeVertices[edgeIndices[2]]nodeneighbors[2]edgeVertices[edgeIndices[2] + 1]A NavMesh polygon can have a maximum of 6 edges. This means the value corresponding to a polygon-polygon connection is between 0 and 5, inclusive. An edge usually connects only the two polygons that share it, but edges that sit at a tile border can connect one polygon in the first tile to multiple polygons in the second tile. In this case, report the same value for all of those neighbors.
edgeIndicesedgeIndicesIf either the given or the is a link, then the corresponding value represents the side on the link where the connection is made: 0 for start and 2 for end. When the is a polygon and the is a link the value acts only as information about the side of the link where the two nodes connect and should not be used as an index in the array.
nodeneighboredgeIndicesnodeneighboredgeVerticesWhen the and buffers both have positive capacity, they must be the same size, otherwise you will encounter an when this method executes in the Editor.
neighborsedgeIndicesArgumentExceptionYou can set any of the buffers to have zero capacity for the cases when you do not need the results.
The returned and values express the number of elements that comprise the result in the output buffers of sufficient size. Buffers that are not large enough are still filled with valid nodes up to their full capacity.
verticesCountneighborsCountThe five result parameters (, , , and ) do not act as input and do not change the internal navigation data in any way. Unity only modifies them in the case when the operation returns a status.
edgeVerticesneighborsedgeIndicesverticesCountneighborsCountSuccessAdditional Resources: NavMeshQuery.GetPolygonType, NavMeshQuery.GetPortalPoints
Examples
using Unity.Collections;using UnityEngine;using UnityEngine.Experimental.AI;public class NavMeshNodeEdgesDrawer : MonoBehaviour{ void Update() { var query = new NavMeshQuery(NavMeshWorld.GetDefaultWorld(), Allocator.Temp); var vertices = new NativeArray<Vector3>(6, Allocator.Temp); var neighbors = new NativeArray<PolygonId>(10, Allocator.Temp); var edgeIndices = new NativeArray<byte>(neighbors.Length, Allocator.Temp); int totalVertices; int totalNeighbors; var location = query.MapLocation(transform.position, Vector3.one, 0); var queryStatus = query.GetEdgesAndNeighbors( location.polygon, vertices, neighbors, edgeIndices, out totalVertices, out totalNeighbors); var color = (queryStatus & PathQueryStatus.Success) != 0 ? Color.green : Color.red; Debug.DrawLine(transform.position - Vector3.up, transform.position + Vector3.up, color); for (int i = 0, j = totalVertices - 1; i < totalVertices; j = i++) { Debug.DrawLine(vertices[i], vertices[j], Color.grey); } for (var i = 0; i < totalNeighbors; i++) { if (query.GetPolygonType(neighbors[i]) == NavMeshPolyTypes.OffMeshConnection) { // The link neighbor is not connected through any of the polygon's edges. // Call GetEdgesAndNeighbors() on this specific neighbor in order to retrieve its edges. continue; } var start = edgeIndices[i]; var end = (start + 1) % totalVertices; var neighborColor = Color.Lerp(Color.yellow, Color.magenta, 1f * start / (totalVertices - 1)); Debug.DrawLine(vertices[start], vertices[end], neighborColor); } query.Dispose(); vertices.Dispose(); neighbors.Dispose(); edgeIndices.Dispose(); }}