Example #1
0
        /// <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.");
            }
        }