/// <summary> /// Refreshes a tile to the area with the highest priority. /// This can be used if a tile was previously overridden and has to be reset. /// </summary> /// <param name="inCoordinate">Coordinate to remove area info for.</param> public void RefreshTile(Vector2Int inCoordinate) { TileBase[] tiles = NavTileManager.Instance.SurfaceManager.GetAllTilesAtPosition(inCoordinate); int highestAreaIndex = -1; for (int i = 0; i < tiles.Length; i++) { int areaIndex = NavLinkManager.GetNavTileAreaIndexForTile(tiles[i]); NavTileArea navArea = NavTileManager.Instance.AreaManager.GetAreaByID(areaIndex); if (navArea == null) { continue; } if (highestAreaIndex == -1 || navArea.Priority > NavTileManager.Instance.AreaManager.GetAreaByID(highestAreaIndex).Priority) { highestAreaIndex = areaIndex; } } if (highestAreaIndex >= 0) { OverrideTile(inCoordinate, highestAreaIndex); } else { RemoveTile(inCoordinate); } }
/// <summary> /// Populate the NavTile data with tiles. /// </summary> public void GenerateNavTileData() { Data.ClearTiles(); Data.InitTilesDictionary(); for (int i = 0; i < NavTileMaps.Count; i++) { Tilemap aTilemap = NavTileMaps[i]; #if UNITY_EDITOR EditorUtility.DisplayProgressBar("Baking NavTiles", $"Baking {aTilemap.name}", (float)i / (NavTileMaps.Count - 1)); #endif // Compress bounds to speed up baking. If extended bounds are needed, remove this call. aTilemap.CompressBounds(); BoundsInt bounds = aTilemap.cellBounds; // Fetch all tiles for current tilemap. TileBase[] tileBases = aTilemap.GetTilesBlock(bounds); int sliceSize = bounds.size.x * bounds.size.z; for (int j = 0; j < tileBases.Length; j++) { TileBase aTile = tileBases[j]; // Get the area index for this tile. // -1 will be returned if this tile is not integrated with NavTile in any way. int tileAreaIndex = NavLinkManager.GetNavTileAreaIndexForTile(aTile); // Skip non-integrated tiles. if (tileAreaIndex == -1) { continue; } // Calculate 3D coordinate based on 1D array position. int y = j / sliceSize; int leftOver = j - (y * sliceSize); int z = leftOver % bounds.size.z; int x = leftOver / bounds.size.z; Vector3Int localCoordinate = new Vector3Int(bounds.x + x, bounds.y + y, bounds.z + z); Vector2Int navCoordinate = localCoordinate.GetVector2Int(); Data.TryAddTile(navCoordinate, tileAreaIndex); } } #if UNITY_EDITOR EditorUtility.ClearProgressBar(); #endif if (Data.HasNoTiles) { Debug.LogWarning("No nav tiles were found on the tilemaps. Please convert tiles to nav tiles in order to enable pathfinding."); } }
/// <summary> /// Called when a different tile is selected in the inspector. /// </summary> /// <param name="inPreviousTile">Previously selected tile.</param> /// <param name="inNewTile">Newly selected tile.</param> public void OnTileChanged(TileBase inPreviousTile, TileBase inNewTile) { NavLinkManager manager = NavTileManager.Instance.LinkManager; NavLink presentLink = manager.GetLinkedTile(inPreviousTile); // Remove any present data. if (presentLink == this) { // Tile present, remove it. manager.RemoveNavLink(inPreviousTile); } // Add new data. if (inNewTile != null) { manager.AddNavLink(inNewTile, this); } // Save dictionary and changed tile. EditorUtility.SetDirty(NavTileManager.Instance); EditorUtility.SetDirty(this); }