• Articles
  • Api Documentation
Show / Hide Table of Contents
  • Introduction
  • Basic Usage
  • Built-in Graph Types
  • Finders
  • Structure
  • Performance/Heuristics
  • Customization
  • Low Level (Jobs and ECS)
  • Code Generation
  • Examples
  • Caveats and Known Issues

Basic Usage

Installation

Installation requirements:

  • Unity 2020.3 or higher
  • Burst minimum version 1.4.11
  • Unity Collections minimum version 0.15.0 If your project uses the Entities package, Unity Collections will already be included

Import AnyPath.unitypackage into your project. If you encounter any compilation errors, you may need to install these packages manually.

  • Click on Window -> Package Manager
  • On the packages dropdown, select Packages: Unity Registry
  • Locate Burst and install the latest version
  • Click on the + > Add package from GIT url
  • Enter com.unity.collections and hit ENTER

Finding a path

To find a path using AnyPath, first create a PathFinder for the type of graph you want to search. Goto Window->AnyPath Code Generator to create a definition for the finder you want to use. Paste the generated code somewhere in your project.

We'll use the built in 2D grid as an example:

IEnumerator LetsFindAPath()
{
    // Create our Path Finder
    var finder = new SquareGridPathFinder();
    
    // We can build our request by chaining methods
    finder
        .SetGraph(myGrid)
        .SetStartAndGoal(new SquareGridNode(0, 0), new SquareGridNode(10, 10));

    // Make the request and wait for the result
    yield return finder.Schedule();

    if (finder.Result.HasPath)
    {
        // Print out our path
        foreach (var edge in finder.Result.Edges)
        {
            Debug.Log(edge);
        }
    }
    else
    {
        Debug.Log("No path was found!");
    }
}

Evaluating a path

if you just need to know if there is a possible path, we can use an PathEvaluator. Additionaly, we can specify multiple stops in between.

IEnumerator OnlyEvaluateAPath()
{
    // Create our Path Finder
    var finder = new SquareGridPathEvaluator();
    
    // We can also just use properties
    finder.Graph = myGrid;
    
    // the first stop acts as the starting position
    finder.Stops.Add(new SquareGridNode(0, 0));
    
    // we'd like to make an in-between stop at 5,5
    finder.Stops.Add(new SquareGridNode(5, 5));

    // and finally arrive at 10, 10
    finder.Stops.Add(new SquareGridNode(10, 10));
    
    // Make the request and wait for the result
    yield return finder.Schedule();

    Debug.Log("Path found: " + finder.Result.HasPath);
}

Creating a Graph

Because AnyPath uses the Burst compiler, all graphs must be defined as structs. Where and how your store your graphs is completely up to you.

public class HexGridContainer : MonoBehaviour
{
    // assigned via inspector
    public List<(int2, float)> cells;
    
    // agents would reference the container and access this field
    public HexGrid Grid { get; private set; }

    private void Start()
    {
        // The way you create a graph is completely dependant on your own definition.
        // The only restriction is it has to implement IGraph<,> and must be a struct, since the requests will be burst compiled.
        Grid = new HexGrid(new int2(0, 0), new int2(1000, 1000), HexGridType.UnityTilemap, cells, Allocator.Persistent);
    }

    private void OnDestroy()
    {
        // make sure to dispose the graph when the container destroys.
        // this automatically takes care of any possible in flight requests.
        Grid.DisposeGraph();
    }
}

Disposing Graphs

Since the graphs use NativeContainers internally, they need to be disposed. Luckily, AnyPath provides the extension method DisposeGraph which can be called when the graph needs to be disposed. DisposeGraph delays the disposal until all requests that are currently operating on the graph are finished. As a result, you can call DisposeGraph on a graph that will be replaced and you don't have to worry about any possible in flight pathfinding requests that were still running on it.

    // Requests all use this grid
    public static HexGrid Grid { get; private set; }

    public void BuildNewGraph()
    {
        // Actual disposal is postponed until the requests that use the old graph are finished
        Grid.DisposeGraph();
            
        // We can safely replace the grid with a newer version which new requests will use
        Grid = new HexGrid(new int2(0, 0), new int2(1000, 1000), 
            HexGridType.UnityTilemap, 
            GetMostRecentCells(), 
            Allocator.Persistent);
    }
In This Article
Back to top Generated by DocFX