Search Unity

Low fps with TilemapRenderer

Discussion in 'Scripting' started by Saw9yer, Nov 2, 2021.

  1. Saw9yer

    Saw9yer

    Joined:
    Sep 19, 2020
    Posts:
    72
    Hello everyone,

    I have a level generated using cellular automata.
    My map is 1366/768 and each pixel is a tile (black or white).

    This is the code i use in void Start() to display the map :

    Code (CSharp):
    1. public void RenderMap(bool[,] map, Tilemap tilemap, TileBase tileNone, TileBase tileExist)
    2.     {
    3.         //Clear the map (ensures we dont overlap)
    4.         tilemap.ClearAllTiles();
    5.         //Loop through the width of the map
    6.         for (int x = 0; x < map.GetLength(0); x++)
    7.         {
    8.             //Loop through the height of the map
    9.             for (int y = 0; y < map.GetLength(1); y++)
    10.             {
    11.                 // 1 = tile, 0 = no tile
    12.                 if (map[x, y] == false)
    13.                 {
    14.                     tilemap.SetTile(new Vector3Int(x, y, 0), tileNone);
    15.                 }
    16.                 else if (map[x, y] == true)
    17.                 {
    18.                     tilemap.SetTile(new Vector3Int(x, y, 0), tileExist);
    19.                 }
    20.             }
    21.         }
    22.     }
    I don't know why but i have only 40 fps.
    How can i improve that?

    The final goal is to create a map where i can interact with each pixels (to create a destructible environment)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,692
    That's a lot of tiles, over one million of them.

    I'm surprised you're even getting 40fps!!

    I don't think this approach will work.

    You might have some luck with just showing a texture, then having some kind of optimized way of updating that texture as a result of the interactions.

    Here was my lotto scratcher treatment:



    I use a RenderTexture to update the visuals. Full code in comment links.
     
    Saw9yer likes this.