Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Hexagonal Tilemap

Discussion in '2D' started by DrDress, Apr 25, 2020.

  1. DrDress

    DrDress

    Joined:
    Sep 11, 2019
    Posts:
    30
    Hi

    I'm trying my first attempt at a resource management strategy game with hex tiles. I figured that I could use the Unity Tilemap stuff. This however seems to be more aimed at platform games or games where you move around the map.

    I would like to be able to do stuff to the tiles in my scripts, like generating a map or calculating the travel time between resources etc.

    So I would like to have coordinates for my map with an origin and a way to say like: "move to the tile to the left or northwest" etc.

    I know this is a messy "question" but any insight into how tilemaps can be used (particularly via code) is helpful. I find a lot of wierd stuf on the net which is not really helpful for what I want.
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @DrDress

    You can find pretty much all you need from Tilemap api. It is not much but basically you can get and set tiles. Create tile asset(s), add a field for it and you can use those in your code.

    There exists also two official repositories related to tilemap, with several examples. One was 2d-extras, can't remember the other's name. Examples in these repositories show how to do these things - how to create custom tiles, how to store extra data that you can use to identify tiles and store tile related data.
     
  3. DrDress

    DrDress

    Joined:
    Sep 11, 2019
    Posts:
    30
    Ok. Thanks. That's not a lot of options, but maybe I can make it work. By tilemap api, you mean the unity documentation page, right?
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @DrDress

    "By tilemap api, you mean the unity documentation page, right?"

    Yes!

    You can set and get tiles. One or many at time.

    If you need to store some data, you need to keep another list/dictionary whatever where you link your coordinate to some tile to get data related to a tile coordinate, check those examples, but very simple list is enough.

    Use bounds to get tile area, and iterate it using a for loop to store tiles into some list for later use.

    You can also instantiate tiles, to make unique tiles. This is one option too, but then you end up having several tile instances in memory.

    For example, I created a test where I stored extra data for tiles. Extra data stores position, health etc. When user clicks a tile, it causes an explosion, neighbor tiles get damaged, and if health goes to zero it explodes and so on.

    tilemap_extradata.gif
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    I know this isn't related to the topic above (sorry!) but I need to see more of these animations for the sheer satisfaction of it. :)
     
    eses likes this.
  6. DrDress

    DrDress

    Joined:
    Sep 11, 2019
    Posts:
    30
    That's really helpful. You wouldn't happen to have some example code? It's a lot easier to figure out that api ducomentation.

    EDIT: Actually I can't seem to find the documentation you describe. This is from Unity documentation:

    https://docs.unity3d.com/Manual/class-Tilemap.html

    There are no get/set stuff. So I'm likely looking the wrong place.
     
    Last edited: Apr 29, 2020
  7. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637