In order to calculate a path from A to B, NavTiles has to be told which tiles are walkable and which are not. NavTiles is designed to work with Unity Tile Assets. These tiles have to be linked to a NavTileArea (a representation of a particular floor type, like water or dirt, with an associated pathfinding cost) in order to facilitate pathfinding. There are three different methods of linking a Unity Tile to a NavTileArea.
1. Using NavTiles
NavTiles comes with its own implementation of a Tile, called a NavTile. A NavTile works exactly the same as a normal Tile (e.g. it can be placed on the Tile Palette and painted on a Tilemap). However, it has an extra field to specify what type of area the NavTile represents on the grid.
These NavTiles can be created manually through the “Create Asset” menu in the project hierarchy or by selecting a sprite and clicking on Tools > NavTiles > Create NavTiles From Sprites through the toolbar at the top of the editor.
If you have already used Unity Tiles to create your levels, don’t worry. You can convert the necessary Tiles to NavTiles by selecting them in the project hierarchy and clicking on Tools > NavTiles > Convert Tiles To NavTiles. This will convert all selected Unity Tiles to NavTiles, preserving the tiles’ properties.
Additionally, this will automatically replace placed tiles on existing Tilemaps, allowing you to preserve your already painted worlds.
If you wish to undo this conversion process, you can select the newly created NavTiles in the project hierarchy and click on Tools > NavTiles > Revert NavTiles. This will replace the NavTiles with Unity Tiles, preserving their properties and swapping them on existing Tilemaps.
2. Using NavLinks
In addition to Unity’s normal Tiles, Unity supports custom Tiles, like the ones found in the 2D extras pack. These cannot be converted to NavTiles. To solve this issue, you can use NavLinks. These are separate assets that can link any kind of tile to a NavTileArea.
The easiest way to create a
NavLink is to select the tile(s) you want to link in the project hierarchy and clicking on Tools > NavTiles > Create NavLinks For Tiles. This will create NavLinks for each tile. These NavLinks are placed in the resources folder, where they should remain. They are placed there so NavTiles can easily find and track them. To make sure the NavLinks are registered correctly, you can navigate to the NavTile Settings Window (through Window > AI > NavTile Settings) and take a look at the Links tab. Here you can find all the registered NavLinks.
3. Using the interface
The above methods should enable you to utilize NavTiles’ pathfinding in most cases. However, if you are making custom tiles, it is possible that a tile might represent a different NavTileArea some of the time, based on its properties at that given moment (a locked gate becoming a walkable tile when opened, for instance). NavTiles has a built-in solution for this.
Your custom tile class can implement the IHasNavTileArea interface, through the GetNavTileAreaIndex function. This function expects a return value equal to the index of the NavTileArea the tile represents. Using this function, you have full control over what NavTileArea is used at any given time. The list of NavTileAreas and their indices can be found in the Areas tab in the NavTile Settings Window. The Area Index can also be retrieved through the AreaManager, accessible through the NavTileManager.