/// <summary> /// This method is called when the tile is refreshed. /// </summary> /// <param name="location">Position of the Tile on the Tilemap.</param> /// <param name="tilemap">The Tilemap the tile is present on.</param> public override void RefreshTile(Vector3Int location, ITilemap tilemap) { base.RefreshTile(location, tilemap); Tilemap tilemap_2 = tilemap.GetComponent <Tilemap>(); ReleaseDestroyedTilemapCacheData(); // Prevent memory leak if (IsTilemapUsedTilesChange(tilemap_2)) { CachingTilemapNeighborPositions(tilemap_2); } HashSet <Vector3Int> neighborPositions = m_CacheTilemapsNeighborPositions[tilemap_2].Value; foreach (Vector3Int offset in neighborPositions) { Vector3Int position = GetOffsetPositionReverse(location, offset); TileBase tile = tilemap_2.GetTile(position); RuleTilePlus ruleTile = tile as RuleTilePlus; if (ruleTile) { if (ruleTile.neighborPositions.Contains(offset)) { base.RefreshTile(position, tilemap); } } } }
static void CachingTilemapNeighborPositions(Tilemap tilemap) { int usedTileCount = tilemap.GetUsedTilesCount(); HashSet <TileBase> usedTiles = new HashSet <TileBase>(); HashSet <Vector3Int> neighborPositions = new HashSet <Vector3Int>(); if (m_AllocatedUsedTileArr.Length < usedTileCount) { Array.Resize(ref m_AllocatedUsedTileArr, usedTileCount); } tilemap.GetUsedTilesNonAlloc(m_AllocatedUsedTileArr); for (int i = 0; i < usedTileCount; i++) { TileBase tile = m_AllocatedUsedTileArr[i]; usedTiles.Add(tile); RuleTilePlus ruleTile = tile as RuleTilePlus; if (ruleTile) { foreach (Vector3Int neighborPosition in ruleTile.neighborPositions) { neighborPositions.Add(neighborPosition); } } } m_CacheTilemapsNeighborPositions[tilemap] = new KeyValuePair <HashSet <TileBase>, HashSet <Vector3Int> >(usedTiles, neighborPositions); }