Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Getting the total size of all Tilemaps under a Grid

Discussion in '2D' started by darcpyro, Apr 25, 2019.

  1. darcpyro

    darcpyro

    Joined:
    Dec 28, 2016
    Posts:
    2
    Hi everyone,

    I've been playing with tilemaps for the last few days and been making very slow progress with them (mainly due to my newness to unity itself).

    I'm currently playing around with a script that takes a reference to a Grid (the parent object of all tilemaps) and loops through all tilemaps under the grid so that I can check every given coordinate on all tilemaps for a tile with certain properties (e.g. is there a tile located at 3,5 on any of the tilemaps that is marked as collideable, if so do something).

    My current issue is there doesn't appear to be a way of getting the total area covered by all tilemaps under a grid. For each individual tilemap I can get its origin and its size so I know where its lower left corner is aswell as its width and height but there doesn't appear to be a simple way of determining the overall size of the grid.

    I guess another way of wording my question is is there any way of determining the bounds of a grid as a whole such that the origin is the position of the tile with the lowest x and y values on ANY tile map and the size is the number of tiles from this origin to the furthest tile with the highest x and y values on any layer?

    I hope all of this has made sense.

    Thanks
     
  2. eses

    eses

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

    Well AFAIK grid is just a grid that can host several tilemaps. I don't think grid actually has a concept of size.

    So if you can get tilemap bounds (Tilemap.cellBounds, after you have compressed the bounds) why not just loop all the tilemaps?

    Then if current tilemap bounds min x is smaller than previous min value, set it as your all tilemaps min x. Then do the same for y and z and then do the same for bounds max.

    I think this way you can get the bounds of all contained tilemaps parented to a Grid. Although I have no idea, why you need this.
     
  3. darcpyro

    darcpyro

    Joined:
    Dec 28, 2016
    Posts:
    2
    That's exactly the approach intended up taking, I was just hoping someone had a nicer way.

    As for why, I was playing around with implementing a* and I needed a way of checking each position on all tilemaps to check for impassable tiles.