[This is preliminary documentation and is subject to change.]

The final step in the NMGen build process is to create the navigation mesh. As a minimum you must have PolyMesh data. PolyMeshDetail and ConnectionSet data are optional.

See the The Navigation Mesh topic for the general creation process. This topic will cover two helpful extensions.

Creating Tile Data

Before you can create a navigation mesh you need to convert your source data into tile data. There are various ways of doing this.

One method is to use the GetBuildData(BuildContext, Int32, Int32, PolyMeshData, PolyMeshDetailData, ConnectionSet, Boolean) utility method.

CopyC#
// Example: Using the tile build data utility method.

BuildContext logger = new BuildContext();

NavmeshTileBuildData tbd = NMBuild.GetBuildData(
            logger
            tileX, tileZ    // The tile location.  (0, 0) for single tile meshes.
            , polyData      // A PolyMeshData object.
            , detailData  // Optional PolyMeshDetailData object.
            , connections // Optional ConnectionSet object.
            , true);

if (tbd == null)
{
    // Perform error handling.
    // The build context will contain error messages.
}

// Use the build data to create the navigation mesh...

Another method is to use the the TileBuildTask class to create the tile data. This method is especially useful if you are building a multi-tile navigation mesh or performing background builds.

CopyC#
// Example: Using the tile build task.

TileBuildTask task = TileBuildTask.Create(
        tileX, tileZ    // The tile location.  (0, 0) for single tile meshes.
        , polyData      // A PolyMeshData object.
        , detailData  // Optional PolyMeshDetailData object.
        , connections // Optional ConnectionSet object.
        , false       // Is the task tread-safe?  Not applicable in this case.
        , 0);                    // Task priority.  Not applicable in this case.

task.Run();

if (task.TaskState == BuildTaskState.Aborted)
{
    // Perform error handling.
    // Check task messages for details.
}

// Get the tile assets.
TileBuildAssets tassets = task.Result;

// Use the tile data to build the navigation mesh...

Creating the Navigation Mesh

Once you have the tile data you can use one of two methods to create the final navigation mesh.

CopyC#
// Example: Create a single tile mesh.

// Where 'buildData' is a NavmeshTileBuildData object.

Navmesh navmesh;
NavStatus status = Navmesh.Create(buildData, out nm)));

if ((status & NavStatus.Success) == 0)
{
    // Perform error handling.
}

// Use the navitgation mesh...

CopyC#
// Example: Create an empty mesh, then add tiles.

// Where 'tiles' is a list containing NavmeshTileData objects.

NavmeshParams nconfig = new NavmeshParams(meshOrigin
        , tileSetWidth, tileSetDepth
        , tiles.Count
        , maxPolysPerTile);

Navmesh navmesh;

NavStatus status = Navmesh.Create(nconfig, out navmesh)

if ((status & NavStatus.Success) == 0)
{
        // Perform error handling.
}

// Add the tiles to the navigation mesh.
foreach (NavmeshTileData tile in tiles)
{
        // Note: Allowing the navigation mesh to assign the tile reference.
        uint trash;
        navmesh.AddTile(tile, Navmesh.NullTile, out trash);
}

// Use the navigation mesh.

See Also