Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Draw Tile Map

Discussion in '2D' started by Afropenguinn, Jan 12, 2015.

  1. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    I have a 2 dimensional array that contains tile data for each tile on a map. I want to be able to pull from that and draw the map in the area the camera can currently see. That is because the map will be huge and I don't want to be drawing all of it at once or have a separate object for each tile. How would I go about drawing these tiles? I assume it will include something like a nested for loop that goes through the width and height of the visible area and gets the tile data from that cell then draws the sprite needed for it. The problem is I am not really sure how to draw a tile!
    Here is an example of what I am looking to achieve:
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    SpriteTile creates enough GameObjects to fill the viewport, and swaps out the sprites as needed when the camera moves. So yes, that way the map can be practically unlimited in size without affecting performance. And yes, it does involve for loops. ;) Among other things. You need to create some kind of data format for tiles, and use that data to decide which sprites to use.

    --Eric
     
  3. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    Oh my, I wonder why you are recommending this eh? :) But in all seriousness, this is exactly what I was looking for, thank you! Can you think of any reason this wouldn't work with a system that randomly generated the maps? Also if I wanted to put a perspective camera on the tiles and angle it from an eagle eye perspective (see the screenshot below for an example) would I be able to?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    No reason, just totally random. ;)

    It would definitely work, and that's one of the reasons I made it. Everything can be controlled through code if desired.

    You would, although currently the camera rotation is forced to be non-rotated, so you'd have to comment out the line in the source code which does that, and increase the "add border" amount of the level to fill the screen properly. Better camera rotation support will be provided in the future.

    --Eric