Documentation

Unity Engine

Download Unity

Unity Engine

GetLinkNode(NavMeshLinkInstance)

Retrieves the navigation node of a link that connects to one or more NavMeshes.
Read time 4 minutesLast updated 9 hours ago

Definition

public readonly NavNode GetLinkNode(NavMeshLinkInstance linkInstance)

Parameters

The object that identifies a navigation link created between locations on one or more NavMeshes.

Returns

Type

Description

NavNodeThe identifier of the node that represents the link in the navigation system.

Remarks

The resulting link identifier is valid for any link instance that is valid. This is true even if the link doesn't connect to any NavMeshes, or if the link is deactivated with NavMesh.SetLinkActive. A link instance preserves its unique identifier for as long as the application runs, regardless of whether it connects to new NavMeshes or it disconnects from existing ones. The link identifier becomes invalid when the link is removed from the navigation system, or when all data of the navigation system is removed.
Note: You can't change the properties of an existing link instance. However, you can remove the existing instance and then replace it with one that has the modified properties.
Note: The navigation system doesn't provide a way to retrieve the NavMeshLinkInstance that corresponds to a NavNode. You need to address that use case yourself. For example, you can store link instance and identifier pairs in a list or dictionary, which you can then search when needed.

Examples

using System;using Unity.AI.Navigation.LowLevel;using Unity.Collections;using Unity.Jobs;using UnityEngine;using UnityEngine.AI;/// <summary>/// This component creates a NavMesh Link between two points in the scene./// It draws the link and reports how many NavMesh polygons each of its ends connects to./// </summary>public class ShowNavMeshLinkConnectivityInfo : MonoBehaviour{ public Vector3 linkLocalStart = new(-1f, 0f, 0f); public Vector3 linkLocalEnd = new(1f, 0f, 0f); public float linkWidth = 0.5f; public int connectionsAtStart; public int connectionsAtEnd; NavWorld m_NavWorld; NativeArray<NavMeshLinkInstance> m_LinkInstances; NativeArray<NavNode> m_LinkNodes; NativeArray<byte> m_EdgeIndexToNeighbor; // Remembers what the link was last built from, so that it is only rebuilt when it has to be. bool m_LinkBuilt; Vector3 m_BuiltAtPosition; Quaternion m_BuiltAtRotation; Vector3 m_BuiltStart; Vector3 m_BuiltEnd; float m_BuiltWidth; struct GetLinkIdJob : IJobFor { [ReadOnly] public NavWorld world; [ReadOnly] public NativeArray<NavMeshLinkInstance> linkInstances; public NativeArray<NavNode> linkNodes; public void Execute(int i) { linkNodes[i] = world.GetLinkNode(linkInstances[i]); } } void Awake() { m_NavWorld = NavWorld.GetDefaultWorld(); m_LinkInstances = new NativeArray<NavMeshLinkInstance>(1, Allocator.Persistent); m_LinkNodes = new NativeArray<NavNode>(1, Allocator.Persistent); m_EdgeIndexToNeighbor = new NativeArray<byte>(20, Allocator.Persistent); } bool LinkNeedsRebuild() { return !m_LinkBuilt || transform.position != m_BuiltAtPosition || transform.rotation != m_BuiltAtRotation || linkLocalStart != m_BuiltStart || linkLocalEnd != m_BuiltEnd || !Mathf.Approximately(linkWidth, m_BuiltWidth); } void RefreshLink() { if (NavMesh.IsLinkValid(m_LinkInstances[0])) NavMesh.RemoveLink(m_LinkInstances[0]); m_LinkInstances[0] = NavMesh.AddLink( new NavMeshLinkData { startPosition = linkLocalStart, endPosition = linkLocalEnd, width = linkWidth, agentTypeID = 0 }, transform.position, transform.rotation); m_LinkBuilt = true; m_BuiltAtPosition = transform.position; m_BuiltAtRotation = transform.rotation; m_BuiltStart = linkLocalStart; m_BuiltEnd = linkLocalEnd; m_BuiltWidth = linkWidth; } void Update() { // Adding and removing a link modifies the NavMesh, which invalidates the nodes around // it. Rebuilding it only when something actually changed keeps every NavNode that other // code has cached alive in the meantime. if (LinkNeedsRebuild()) RefreshLink(); // The link data is given in the local space defined by the position and rotation passed // to AddLink, which ignores scale. Vector3 worldStart = transform.position + transform.rotation * linkLocalStart; Vector3 worldEnd = transform.position + transform.rotation * linkLocalEnd; Debug.DrawLine(worldStart, worldEnd, Color.cyan); GetLinkIdJob getLinkIdJob = new GetLinkIdJob { world = m_NavWorld, linkInstances = m_LinkInstances, linkNodes = m_LinkNodes }; JobHandle jobHandle = getLinkIdJob.Schedule(m_LinkInstances.Length, default); m_NavWorld.AddDependency(jobHandle); JobHandle.ScheduleBatchedJobs(); jobHandle.Complete(); // Only the edge indices are needed here, so the vertex and neighbor buffers are left // empty. GetEdgesAndNeighbors accepts a zero-capacity buffer for any result it should // not produce. NavQueryStatus queryStatus = m_NavWorld.GetEdgesAndNeighbors( m_LinkNodes[0], default, default, m_EdgeIndexToNeighbor, out _, out int neighborsCount); if ((queryStatus & NavQueryStatus.Success) != NavQueryStatus.Success) { // A link that reaches no surface at either end has no valid node. Debug.DrawRay(worldStart, Vector3.up, Color.red); Debug.DrawRay(worldEnd, Vector3.up, Color.red); return; } int indicesCount = Math.Min(m_EdgeIndexToNeighbor.Length, neighborsCount); // Edge index 0 is the start of the link, so every other neighbor hangs off its end. int nStartPolygons = 0; for (int i = 0; i < indicesCount; i++) { if (m_EdgeIndexToNeighbor[i] == 0) nStartPolygons++; } int nEndPolygons = neighborsCount - nStartPolygons; // Green where the link reaches the NavMesh, red where it connects to nothing. Debug.DrawRay(worldStart, Vector3.up, nStartPolygons > 0 ? Color.green : Color.red); Debug.DrawRay(worldEnd, Vector3.up, nEndPolygons > 0 ? Color.green : Color.red); if (connectionsAtStart != nStartPolygons || connectionsAtEnd != nEndPolygons) Debug.Log($"Link starts in {nStartPolygons} polygons and ends in {nEndPolygons} polygons."); connectionsAtStart = nStartPolygons; connectionsAtEnd = nEndPolygons; } void OnDisable() { // The link may never have been created, for example when the component is disabled // before its first Update. if (NavMesh.IsLinkValid(m_LinkInstances[0])) NavMesh.RemoveLink(m_LinkInstances[0]); // Force a rebuild if this component is enabled again. m_LinkBuilt = false; connectionsAtStart = 0; connectionsAtEnd = 0; } void OnDestroy() { m_LinkInstances.Dispose(); m_LinkNodes.Dispose(); m_EdgeIndexToNeighbor.Dispose(); m_NavWorld.Dispose(); }}