Search Unity

Tilemap issues when exiting playmode.

Discussion in '2D' started by Chris-Trueman, Nov 12, 2018.

  1. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    Messing around with the Tilemap feature and have come across a problem while exiting playmode.

    I created a few tilemaps as layers that I am adding tiles to at runtime. One of the layers, has a Tilemap Collider. I'm making it 1000 x 250 tiles. I fill these tiles over time starting where the player spawns, working outwards to the edges. Nothing too fancy just a bucket fill of one tile.

    Everything runs fine at run time, removing, adding no problems. When I exit playmode the editor freezes up, which I'm guessing has to do with all the collliders from the 250k tiles that have one.

    Am I pushing the limits of the Tilemap using colliders or is this a bug? Should I ditch the colliders and use a simple cell check for collision?
     
  2. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    The slowdown should be related to the number of individual collider shapes that need to be cleaned up for each Tile.

    Ideally, you would composite the individual collider shapes from the TilemapCollider2D using a CompositeCollider2D if possible to reduce the number of collider shapes, assuming that the shapes can be merged together. This should improve the slowdown you face.
     
  3. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    I added a CompositeCollider2D but it changed nothing. I am adding the tiles at runtime, and from what I've read it doesn't update the composite collider.
     
  4. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Have you set "Used By Composite" in the TilemapCollider2D component? This is required for the Collider 2D to interact with the CompositeCollider2D.

    Code (CSharp):
    1.  
    2. var tilemapCollider2D = gameObject.GetComponent<TilemapCollider2D>();
    3. tilemapCollider2D.usedByComposite = true;
    4.  
     
  5. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    When I use the CompositeCollider2D, everything slows done when adding the tiles overtime due to the constant rebuilding of the composite collider.
     
  6. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Could I check with you how you are adding Tiles at runtime? More specifically, are you constantly adding new Tiles to the Tilemap each frame, or do you add new Tiles to form a level once per Tilemap and not add any more Tiles?

    Depending on the number of Tiles you are adding, you could try disabling the colliders, adding the Tiles and enabling the colliders after you are done for better performance.
     
  7. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    I am constantly adding new tiles each frame until its done filling the tilemap. I don't think I can get the performance I need using colliders. Going to test out cell based collisions. This is just testing purposes, so its only to see if what I want to do will work with tilemaps.
     
  8. ClockworkBeetle

    ClockworkBeetle

    Joined:
    Apr 5, 2021
    Posts:
    3
    Similar problem. Not sure if I'm adding much but here's what worked for my case:

    The editor was quick to start play mode, but could take 5sec~3min+ (or freezing) to exit play mode, and it was essentially proportional to the edge size and complexity of whatever new tile sprites I was trying.
    Having a perfect hexagon be the tile collider for my hexagonal tiles in my hexagonal grid... for my project, at least... is great and fine. Didn't realize it was doing otherwise! This thread helped me see that.
    I read more here: https://docs.unity3d.com/2020.1/Documentation/Manual/class-TilemapCollider2D.html
    All I needed to do was change the Tile Asset's "Collider Type" to "Grid", and everything is fast again!