Low level usage (Jobs and ECS)
The managed finders are just a layer of abstraction on top of the AnyPath.Native functions. All of the methods in the AnyPath.Native namespace are Burst compatible.
To do a low level pathfinding request, create an instance of AStar<TNode> and call the FindPath, EvalPath or one of the more complex extension methods found on AStarStops, AStarOption or AStarCheapestOption.
See below for an example.
A note on ECS
Entities can store dynamic elements in a so called DynamicBuffer component. Unfortunately, as for now, Unity does not provide a way to completely generalize between a DynamicBuffer and NativeList.
In order to fully support all the functionality AnyPath provides, a choice had to be made. That is why all of the native AnyPath methods operate on NativeList and NativeSlice structs. If you need to store your path in a DynamicBuffer, you can provide a temporary or persistent backing memory NativeList and copy from that list after. Here's an example:
// This can run as part of a burst compiled job, or in an ECS system
public static bool FindPathAndStoreInDynamicBuffer(ref SquareGrid grid, SquareGridCell start, SquareGridCell goal, ref DynamicBuffer<SquareGridCell> path)
{
// these could also be persistent in the system or job to prevent reallocation. Note that simultaneous requests need their own instance.
var aStar = new AStar<SquareGridCell>(Allocator.Temp);
var tempPathBuffer = new NativeList<SquareGridCell>(128, Allocator.Temp);
aStar.FindPath(ref grid, start, goal,
// default heuristic for a 2D grid
default(SquareGridHeuristicProvider),
// we don't use any edge modifiers
default(NoEdgeMod<SquareGridCell>),
// no path processing (just the raw node output)
default(NoProcessing<SquareGridCell>),
// the path will be appended to this list
tempPathBuffer);
if (!result.evalResult.hasPath)
return false;
// if you don't use DynamicBuffer, the path will be in tempPathBuffer now
// otherwise, copy the path to the DynamicBuffer. This uses a memcpy internally so it's very fast
path.CopyFrom(tempPathBuffer);
return true;
}
Note
If Unity ever introduces a safe way to generalize between NativeList and DynamicBuffer via INativeList (specifically: a way to create a NativeSlice from INativeList) AnyPath will get an update so that it can directly operate on DynamicBuffer.
Alternative way
public static void AnotherWay(NavMeshGraph graph, NativeList<NavMeshGraphLocation> stops, ref NativeList<float3> path)
{
// As an alternative to using the static AStar functions which require a lot of type parameters,
// we can leverage the managed finder's Job struct to make our native query more readable.
// we use the job struct and directly call Execute on it.
// the NavMeshGraphPathFinder code can be generated using the code generator
var job = new NavMeshGraphPathFinder.Job()
{
graph = graph,
stops = stops,
aStar = new AStar<NavMeshGraphLocation>(Allocator.Temp),
path = path
};
job.Execute();
job.aStar.Dispose();
// path will now be filled with the corner points
}