Search Unity

Question SetTile() taking UNGODLY processing power.

Discussion in '2D' started by ScorpionWasp, Nov 10, 2020.

  1. ScorpionWasp

    ScorpionWasp

    Joined:
    Jun 19, 2020
    Posts:
    19
    I'm making a sim game where players can edit terrain in real time, with mouse clicks. The world is 1000 X 500 tiles. There's a tilemap for rock and another for sand; both have Tilemap Collider 2D and Composite Collider 2D for automated physics.

    Recently I added this piece of code:

    if (tile == (int)Manager.Tiles.Erase)
    {

    manager.RockTiles.SetTile(temp2, null);
    manager.SandTiles.SetTile(temp2, null);

    }
    else if (tile == (int)Manager.Tiles.Rock)
    {

    manager.RockTiles.SetTile(temp2, manager.Rock);
    manager.SandTiles.SetTile(temp2, null);

    }
    else if (tile == (int)Manager.Tiles.Sand)
    {

    manager.RockTiles.SetTile(temp2, null);
    manager.SandTiles.SetTile(temp2, manager.Sand);

    }

    Every time the player clicks on the world to add or erase a new tile, the game literally pauses for several seconds. The profiler is accusing frames taking 6 SECONDS and the offending functions appear to be Tilemap based, namely recalculating the geometry of the Composite Collider 2D.

    Is this normal/expected? Am I implementing this wrong? Is there any workaround for this?
     
  2. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    you need to split the tilemaps into chunks(32x32 for example), the problem is that everytime you set tile the collider is recalculating new shape collisions for your huge tilemap( insane cpu cost )
     
  3. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Could you share your setup for your TilemapCollider2D and CompositeCollider2D components, as well as screenshots from the profiler? Thanks! Setting an Offset Distance of 0.0 in the CompositeCollider2D may help a little with this.
     
  4. ScorpionWasp

    ScorpionWasp

    Joined:
    Jun 19, 2020
    Posts:
    19
    Zeroing the Offset Distance has improved performance considerably; still a few orders of magnitude short of a workable solution, of course. I haven't yet attempted dividing the large tilemaps into multiple smaller ones as that will involve a fair bit of scripting.
    Profiler Tile Add.PNG TilemapColliderRock.PNG CompositeRock.PNG TilemapSand.PNG CompositeSand.PNG
     
  5. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    It seems that there may be too many Tiles for the TilemapCollider2D and the CompositeCollider2D to handle quickly for a frame. You may need to split up your Tilemap into smaller Tilemaps for this.

    Just to check, if you set the Max Tile Change Count to 0 in your TilemapCollider2D, do you get any difference in speed with this? Thanks!