Documentation

Unity Engine

Download Unity

Unity Engine

NavQueryBuffer

A buffer that stores intermediate node data during NavMesh pathfinding operations in a NavWorld.
Read time 3 minutesLast updated 8 hours ago

Definition

public struct NavQueryBuffer : IDisposable, IEquatable<NavQueryBuffer>

Remarks

You need a
NavQueryBuffer
to use the pathfinding methods of NavWorld. These methods store their intermediate state in the buffer. To find a path between two locations on the NavMesh:
  1. Create a
    NavQueryBuffer
    with the NavQueryBuffer constructor, and set
    maxNodesToVisit
    to a value from 1 to 65,535.
  2. Call NavWorld.BeginFindPath.
  3. Call NavWorld.ContinueFindPath until the search completes.
  4. Call NavWorld.EndFindPath.
  5. Call NavWorld.GetResultFromFindPath to copy the path out of the buffer.
You can call
NavWorld
pathfinding methods that use a
NavQueryBuffer
inside jobs, such as IJob and IJobFor.
Copying this object produces only a new reference to the same underlying buffer. It doesn't duplicate the buffer in memory.
In the Editor,
NavWorld
pathfinding methods throw an exception if the
NavQueryBuffer
you pass isn't valid, for example after you dispose it.

Examples

using Unity.AI.Navigation.LowLevel;using Unity.Collections;using UnityEngine;// Attach this script to a GameObject on a baked NavMesh. Each frame, the script finds a path// from the GameObject to a point 10 units ahead of it and draws the path in yellow in the Scene view.public class NavQueryBufferExample : MonoBehaviour{ NavWorld m_World; NavQueryBuffer m_Buffer; void OnEnable() { m_World = NavWorld.GetDefaultWorld(); m_Buffer = new NavQueryBuffer(m_World, Allocator.Persistent, 1024); } void Update() { int anyAgentType = -1; NavLocation start = m_World.MapLocation(transform.position, 5f * Vector3.one, anyAgentType); int agentTypeAtStart = m_World.GetAgentTypeIdForNode(start.node); NavLocation end = m_World.MapLocation(transform.position + transform.forward * 10f, 5f * Vector3.one, agentTypeAtStart); if (!m_World.IsValid(start) || !m_World.IsValid(end)) { Debug.DrawRay(transform.position, Vector3.up, Color.red); return; } // The buffer carries the pathfinding state across the Begin/Continue/End calls, so the // same instance has to be handed to every one of them. NavQueryStatus status = m_World.BeginFindPath(m_Buffer, start, end); if ((status & NavQueryStatus.InProgress) != 0) status = m_World.ContinueFindPath(m_Buffer, 2000, out int _); if ((status & NavQueryStatus.Success) == 0) return; status = m_World.EndFindPath(m_Buffer, out int pathSize); if ((status & NavQueryStatus.Success) == 0) return; // The result stays in the buffer until it is copied out, which is what makes it safe to // read the path in a later step, or even in a later job. using NativeArray<NavNode> path = new NativeArray<NavNode>(pathSize, Allocator.Temp); int copied = m_World.GetResultFromFindPath(m_Buffer, path); DrawPath(path, copied); } void DrawPath(NativeArray<NavNode> path, int nodeCount) { NavLocation pathStart = m_World.CreateLocation(transform.position, path[0]); Vector3 previousMiddle = pathStart.position; // The path is a corridor of nodes, not a list of waypoints. Draw a path // through the middle of each gate shared by a pair of consecutive nodes. for (int i = 0; i < nodeCount - 1; i++) { if (m_World.GetPortalPoints(path[i], path[i + 1], out Vector3 left, out Vector3 right)) { Vector3 middle = 0.5f * (left + right); Debug.DrawLine(previousMiddle, middle, Color.yellow); previousMiddle = middle; } } Vector3 destination = transform.position + transform.forward * 10f; NavLocation pathEnd = m_World.CreateLocation(destination, path[nodeCount - 1]); Debug.DrawLine(previousMiddle, pathEnd.position, Color.yellow); } void OnDisable() { m_Buffer.Dispose(); m_World.Dispose(); }}

Constructors

Constructor

Description

NavQueryBuffer(Unity.AI.Navigation.LowLevel.NavWorld,Unity.Collections.Allocator,System.Int32)Creates a new buffer and allocates memory to store the intermediate NavMesh node data used by pathfinding operations.

Methods

Method

Description

DisposeDestroys the NavQueryBuffer and frees the memory it uses.
EqualsChecks whether an object is a NavQueryBuffer that refers to the same underlying pathfinding buffer.
GetHashCodeComputes a hash code for identifying this NavQueryBuffer in hash-based collections.

Operators

Operator

Description

operator ==Checks whether two NavQueryBuffer values refer to the same underlying pathfinding buffer.
operator !=Checks whether two NavQueryBuffer values refer to different underlying pathfinding buffers.