Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Changing tile sprite forces TilemapCollider to update?

Discussion in '2D' started by christides11, Nov 22, 2018.

  1. christides11

    christides11

    Joined:
    May 19, 2012
    Posts:
    667
    I've been using the tilemap for a terraria-like game, and I've been having performance issues while generating chunks.
    From what I've tested, the problem seems to be coming from here. Here I change the sprite depending on the neighbors for ground tiles.
    Code (CSharp):
    1.     public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData)
    2.     {
    3.         base.GetTileData(position, tilemap, ref tileData);
    4.  
    5.         // Checks each direction for tiles
    6.         top = HasBitmaskTile(position + Vector3Int.up, tilemap) == true ? 1 : 0;
    7.         bottom = HasBitmaskTile(position + Vector3Int.down, tilemap) == true ? 1 : 0;
    8.         left = HasBitmaskTile(position + Vector3Int.left, tilemap) == true ? 1 : 0;
    9.         right = HasBitmaskTile(position + Vector3Int.right, tilemap) == true ? 1 : 0;
    10.         topLeft = HasBitmaskTile(position + ul, tilemap) == true ? 1 & top & left : 0;
    11.         topRight = HasBitmaskTile(position + ur, tilemap) == true ? 1 & top & right : 0;
    12.         bottomLeft = HasBitmaskTile(position + dl, tilemap) == true ? 1 & bottom & left : 0;
    13.         bottomRight = HasBitmaskTile(position + dr, tilemap) == true ? 1 & bottom & right : 0;
    14.  
    15.         // Calculates the sprite id
    16.         mask = (1 * topLeft) + (2 * top) + (4 * topRight) + (8 * left) + (16 * right) + (32 * bottomLeft) + (64 * bottom) + (128 * bottomRight);
    17.  
    18.         tileData.sprite = corners[maskIndex[mask]];
    19.     }
    Specifically, the "tileData.sprite" line. With it there, the profiler looks like this when a new chunk is being generated:


    Compare that to when I comment that line out:


    Right now I'm assuming the sprite change is forcing the collider to update, which is not functionality I want/need here as the different sprite should have the same collision. If this is the issue, is there a way to disable this functionality?
    Also I have tried Unity's rule tiles, they also give me the same performance issue.
     
  2. christides11

    christides11

    Joined:
    May 19, 2012
    Posts:
    667
    After looking a bit I found the "Collider type" option for tiles, changing mine from "Sprite" to "Grid" solved the issue.
     
unityunity