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. Dismiss Notice

Get 2D Tilemap size

Discussion in '2D' started by LunarMountainGames, Nov 30, 2019.

  1. LunarMountainGames

    LunarMountainGames

    Joined:
    Jul 24, 2019
    Posts:
    10
    Hey all, I'm trying to get the tilemap size either by tile count or actual width. The tilemap is currently a 2x2 map with each tile being 16x16. I've tried:

    m.GetComponent<Tilemap>().size which returns (35,21).

    m.GetComponent<Tilemap>().cellBounds.size which also returns (35,21).

    I'm expecting to get back (32, 32) or (2,2), but I'm obviously doing this incorrectly. How do I go about getting either the tilecount, or the actual bounds of the tilemap?
     
    ethanharlig likes this.
  2. LunarMountainGames

    LunarMountainGames

    Joined:
    Jul 24, 2019
    Posts:
    10
    I found the solution. You have to call the CompressBounds method from the Tilemap and then you get the actual size in tiles:

    m.GetComponent<Tilemap>().CompressBounds();

    m.GetComponent<Tilemap>().size now returns (2,2) !
     
  3. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    876
    Great that you could figure it out by yourself, and thank you for coming back and posting the solution to your question!
     
    ethanharlig likes this.