NavQueryBuffer(NavWorld, Allocator, int)
Creates a new buffer and allocates memory to store the intermediate NavMesh node data used by pathfinding operations.
Read time 2 minutesLast updated 8 hours ago
Definition
- Type: Constructor
- Namespace: Unity.AI.Navigation.LowLevel
- Assembly: UnityEngine.AIModule
public NavQueryBuffer(NavWorld world, Allocator allocator, int maxNodesToVisit = 1024)
Parameters
The allocator type that sets the lifetime of the buffer's safety handle. Unity always allocates the native buffer memory as persistent. With collections checks enabled, a buffer created with Allocator.Temp becomes invalid at the end of the scope where you create it.
The maximum number of nodes that can be stored in the buffer during a search operation. Unity clamps this value to the range from 1 to 65,535. The default value is 1024.
Remarks
Use the parameter to size the buffer for the pathfinding methods that use it, such as NavWorld.BeginFindPath, NavWorld.ContinueFindPath, NavWorld.EndFindPath, and NavWorld.GetResultFromFindPath. If the buffer is too small for the search, the pathfinding method returns a NavQueryStatus.MaxNodesToVisitExceeded status. Unity clamps to the range from 1 to 65,535. A value outside that range doesn't prevent Unity from creating the buffer: Unity logs a warning in the Editor and allocates the nearest allowed size instead.
maxNodesToVisitmaxNodesToVisitIn the Editor, the constructor throws an ArgumentException if isn't valid, and an OutOfMemoryException if Unity can't allocate the buffer.
worldFor more information about the container type that this buffer follows, refer to Introduction to NativeContainer.
Examples
using Unity.Collections;using UnityEngine;using Unity.AI.Navigation.LowLevel;// Attach this script to a GameObject. When the GameObject is enabled, the script creates a// NavQueryBuffer that can store up to 2048 nodes. It disposes the buffer when the GameObject is disabled.public class NavQueryBufferConstructorExample : MonoBehaviour{ NavWorld m_World; NavQueryBuffer m_Buffer; void OnEnable() { m_World = NavWorld.GetDefaultWorld(); // The buffer can only be passed to methods of the world it was created with, // so keep both handles alive for as long as you run queries. m_Buffer = new NavQueryBuffer(m_World, Allocator.Persistent, 2048); } void OnDisable() { m_Buffer.Dispose(); m_World.Dispose(); }}