Search Unity

  1. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice

What is ITilemap?

Discussion in '2017.2 Beta' started by edwardrowe, Oct 4, 2017.

  1. edwardrowe


    Feb 11, 2014
    I'm trying to do some somewhat complicated auto-tiling, and I'm running into some code that's stumped me.

    Custom tiles and brushes handle operations on ITilemaps, but I am also working with Tilemaps. They have similar APIs but can't be converted to and from each other. At first I was excited when I found ITilemap because expected ITilemap to be an interface that Tilemap implemented. But alas, it's just a standard class. Furthermore, Tilemaps can't be cast or converted to ITilemaps and vice versa.

    So my question is - can someone with source or from Unity explain the difference between ITilemap and Tilemap and why both exist? The documentation doesn't quite make their relationship clear to me. Why isn't ITilemap an actual interface, and how would you recommend converting from one to the other (or is that a bad idea)?

    rakkarage likes this.
  2. ChuanXin


    Unity Technologies

    Apr 7, 2015
    ITilemap is meant to be a read-only helper for retrieving Tilemap information when Tiles are being acted on (through RefreshTile, GetTileData and so on). Tiles are not expected to modify through their operations, hence the difference between ITilemap and Tilemap, and the methods of TileBase being given an ITilemap.

    While ITilemap is not the same as a Tilemap, if you really need access to the Tilemap it is acting on, you can do:
    Code (CSharp):
    1. var actualTilemap = iTilemap.GetComponent<Tilemap>(); // iTilemap is a ITilemap instance
    If you are using TileBase methods directly in your code (thus requiring the need to convert your own ITilemap from Tilemap), do share with us your use-cases and we will try to help with that!
    APSchmidt and CheekySparrow78 like this.
  3. edwardrowe


    Feb 11, 2014
    That's very helpful, thank you for the response! It makes a lot of sense to keep the data passed to the TileBase read-only.

    Here's my use case but I've refactored it to not be an issue:

    I made a `HeightTile : TileBase` tile that had a `float height` property, accessed through `GetHeight(Vector3 position, Tilemap tiles)` - which required a Tilemap and position because some tiles based their height off their neighbors. This meant I could call "GetHeight" from `GetTileData` and other TileBase methods, but not from a regular component that was monitoring the Tilemap component.

    I ended up just using the tile's Z as height, which is much cleaner, anyway. Still good to know the intent behind ITilemap so that I can use it correctly.