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

Unity Tilemap lagging when working with Large maps

Discussion in '2D' started by wawethewaras, Jun 10, 2020.

  1. wawethewaras

    wawethewaras

    Joined:
    Mar 15, 2017
    Posts:
    5
    Hi everyone!

    Does anyone have a solution for performance issues when working with large tilemaps?

    I am working with Large map (300 X 400 tiles) and multiple layers (7) the editor often lags and freezes when trying to use bucket fill or move areas. I tested with smaller tilemap (20X30) and one layer and the tilemap still lagged when I use bucket fill.

    I am currently using Unity 2019.2.15f1
     
  2. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Could you share more details of your Tilemap (including other components like the TilemapCollider2D) and an Editor Profiler screen capture when it lags? This will greatly help identify the problem areas, such as rendering, collider generation or general updating of Tile data, hanks!
     
  3. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,503
    Preferences -> 2D -> Grid Brush -> Show Flood Fill Preview - uncheck it.
    That will stop the previewing behavior which may limit some of the lag.
     
  4. wawethewaras

    wawethewaras

    Joined:
    Mar 15, 2017
    Posts:
    5
    @Lo-renzo Thanks that helps with the lag though it is annoying not the see preview of the fill area.

    @ ChuanXin Here is printscreen of the scene and profiler. Tilemap is 51 X 78 tiles large with no colliders and only one layer. Tiles are standard tiles with no custom properties.
    scene.png

    Profiler using deep profile. Spikes happen when fill tool is moved from side area (blue) to center area (green) :
    profiler.png
    Profiler without deep profile:
    profiler2.png
     
  5. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Thanks for the screenshots! The size of the Tilemap seems reasonable to me.

    Unfortunately, the profiler screenshots do not give much information. If possible, could you save the profiler data and post it here? There is a "Save" button on the Profiler toolbar which produces a file containing the profiler information. This would allow us to go deeper into the Editor calls and see where the time is spent in detail.
     
  6. _Eyesgood_

    _Eyesgood_

    Joined:
    Apr 19, 2013
    Posts:
    55
    I am working on a 2d survival game with maps as large as a million tiles. I also have (currently) 18 tilemaps so that I can render a single terrain type per tilemap. That said, I am certainly not trying to render 18 million tiles. Rather, I wrote a chunking engine to load chunks of the map based on the location of the player. My chunks are 25x25 tiles and I render 9 chunks at a time (player chunk and 8 surrounding chunks). As the player moves around, the chunk engine loads/saves chunk data along with all tree objects. The tilemaps are only used for the terrain. I instantiate all my trees and other game objects directly above each tile. Some things I have picked up so far during this process:

    1. I do not use tilemap colliders. I also do not use polygon or edge colliders. I tried them on my trees and it crushed performance. I use capsule colliders on my trees that collide with my player's capsule collider (the trigger), to allow trees to fade when the player is behind them. Again, the trees are not in the tilemaps. They are objects spawned in front of the tilemaps at the tile locations. The tilemaps are just for terrain, nothing else. Everything else are individually spawned game objects.
    2. I render the tilemaps in chunk mode, not individual.
    3. I use a single sorting layer for my trees with a common sorting group. The sort order for trees are determined by storing a sortId in the class that stores all my tile data and matching the tree to the tile it is on. This provides a natural sorting order that keeps the trees rendered properly (in front or behind other trees). I use the same referenced value for my player, allowing my player to have a value lower than the tile he is on, so he stands in front of the tile's tree. The array that stores this data is kept in a singleton class, but written to json data files for each chunk.
    4. I do not use the Tile Palette to draw my maps. All my maps are procedurally generated at runtime. I took my terrain spritesheets and dropped them onto the Tile Palette so I could save them for use on the tilemaps when I create my tile objects in code. I then deleted them from the palette.
    5. I also do not use the Scriptable Tile feature of the Tile Palette. Instead, I wrote a method to determine which tile asset to render based on the tiles surrounding the target tile. I am sure the Scriptable Tile feature works fine, but again, I wanted to keep it in code.
    6. I am using animation tiles and they are working well. I am having a ton of problems locating lighting and shader Assets for my game, with so few available with the new URP.

    So, in the way of performance I am able to load all 9 chunks of tile data into the scene in less than 1 second. I am not using a sprite atlas yet, just loading the tilemap tile objects created from the tile palette directly into the scene.

    Anyway, good luck with your project. I hope something in this rambling post inspires you with ideas.
     
    Last edited: Jun 16, 2020
  7. wawethewaras

    wawethewaras

    Joined:
    Mar 15, 2017
    Posts:
    5

    Attached Files:

  8. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Thanks! We will check this out!
     
  9. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    @wawethewaras Hi, just to check, is this slowness in Floodfill something you experience often during editing? Would you mind sharing the frequency of this slowness you experience on a daily basis? This will help us understand how often this comes up with your workflow!

    We will try to improve the performance for the slowness in doing the Floodfill preview, however filling in tens of thousands of Tiles per Floodfill activation will still take an amount of time which I hope you will understand. So far, each activation takes less than 100ms from the profiler data for around ~20-30k Tiles. Thanks!
     
  10. wawethewaras

    wawethewaras

    Joined:
    Mar 15, 2017
    Posts:
    5
    I would say I experience it really often because I like to use rule tiles for most of my areas. In my experience the biggest issue is not specifically flood filling super large area. This issue usually comes when I try to fill smaller area (E.g. 10 tiles) and while doing it hover over larger area which results to the lag spike. To avoid this I have to use very specific mouse movement to not accidentally enable fill preview for the large area. Previously mentioned "Show Flood Fill Preview "helps with this but I would still like to see preview of the smaller areas. Some kind of cap value for "Show Flood Fill" Preview could help with this specific issue.
     
  11. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    This sounds fair! We will look into this as well as the performance improvements regarding Flood Fill. Are there other situations where you have experienced slowdowns in?
     
  12. wawethewaras

    wawethewaras

    Joined:
    Mar 15, 2017
    Posts:
    5
    @ChuanXin IIRC moving medium to large tile areas is also slow but other than that I think the performance of the editor is fine.
     
  13. AllanHouse

    AllanHouse

    Joined:
    Oct 6, 2020
    Posts:
    3
    1. I do not use tilemap colliders. I also do not use polygon or edge colliders. I tried them on my trees and it crushed performance. I use capsule colliders on my trees that collide with my player's capsule collider (the trigger), to allow trees to fade when the player is behind them. Again, the trees are not in the tilemaps. They are objects spawned in front of the tilemaps at the tile locations. The tilemaps are just for terrain, nothing else. Everything else are individually spawned game objects.

    If you found this on google, this is the answer. Not always convenient, but there are other ways around some of the collider/object issues (like having a separate tilemap where you would have put colliders, if your code detects a tile there as your player moves onto it, do stuff).
     
  14. akim330

    akim330

    Joined:
    Jul 29, 2021
    Posts:
    6
    I have the same problem and am just discovering this thread. I'm procedurally creating a large tile map (1000 x 1000) and it's totally fine when I don't include a Tilemap Collider 2D + Composite Collider 2D but it crashes when I include the colliders.

    I don't quite understand what the alternative to Tilemap Colliders is. @AllanHouse, what do you mean by using a "separate tilemap where you would have put colliders"?
     
  15. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    You need to understand what the components are doing before asking for alternatives. You've added a CompositeCollider2D which takes all the geometry from colliders (not just tilemaps) and merges it all together. Adding this to a tilemap means that each tile change updates it all.

    If you are procedurally creating something then don't have it active while you do it. If the GO is inactive or the TilemapCollider2D is inactive then it'll accept the change but not perform any merging.