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

Sprites vs Mesh for Tile Map

Discussion in '2D' started by JymWythawhy, May 11, 2015.

  1. JymWythawhy

    JymWythawhy

    Joined:
    Jan 18, 2015
    Posts:
    25
    Hey all, I hope I can get some advice from you on best practices with tilemaps in Unity2D!

    After going through the fun "Roguelike" tutorial in the Learn section, I decided to make my own rogue-like game for learning purposes, starting with a random dungeon generator. That works great.

    In the tutorial, they made each floor or wall tile an individual sprite, so they instantiated 8X8 (64) floor or wall tiles at the beginning of each level. I have come across old forum posts and youtube videos (like the Quill18 tilemap video) that say that is a very inefficient way of handling things, and they recommended using a large Mesh object instead, using UV mapping to map your tiles to proper spots on the mesh. These forum posts and youtube videos came out before the introduction of the Unity 2D system.

    So, I went ahead and made both systems, using my dungeon generator as the input. I made a 100X100 (10,000) tile map in each. The sprite based tile map appeared on the screen 1.25 seconds after hitting start, according to the profiler, while the Mesh with UV mapping version took over 3.5 seconds. Interestingly, the "Instantiate" part of the Game.Start() function was only 0.3 seconds longer on the Mesh based than on the Sprite based, so the extra 2 seconds must be from some inefficiency getting the info to the mesh builder so it can build the mesh. Even with that, the sprite based system was faster to build.

    So, is the Mesh based system still the preferred method for generating Tilemaps, so you cut down on the number of game objects that need to be Instantiated? I would eventually like to learn how to put the rogue like on an iPhone- which system do you think will do better for that? It seems like it will be easier to implement a fog of war system on the sprite based system- I think you need to copy the entire Mesh's vertex color array over to edit a few cells before coping it all back over- and that sounds like it could be expensive... But I don't have much experience with it.

    What have you found to work well for you?

    Thank you all!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You definitely don't want to instantiate a GameObject for every tile. You don't need to use meshes, though; you can use a pooling system that uses sprites instead. (Example) But 3.5 seconds for a 100x100 tile map using a mesh seems like about 1000X too long.

    --Eric
     
  3. JymWythawhy

    JymWythawhy

    Joined:
    Jan 18, 2015
    Posts:
    25
    Thanks for the heads up, Eric! Your SpriteTile system is on my wishlist- it seems like a great system.

    I think most of the time generating my map comes from the non-monobehaviour based dungeon generator. I am unfamiliar with the profiler, having just gained access to it, but can the profiler only report on monobehaviour scripts? I would love to track down what is taking so long in generating my map! I have a script that chooses the proper wall tile graphic based on the surrounding tiles (so the wall is seamless and flows)- I wonder if that is slowing things down.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The profiler profiles everything. :) Turn on deep profiling to get more info.

    --Eric