Need Pointer: How to create 2D map in Unity?

Hey everyone,

I’m relatively new to Unity and want to make a tower defense game, constantly updating and improving it step by step. Though I’m a bit stuck on how to create the map. I’ve found a couple solutions and would really appreciate if anyone could help me with the best option and any possible pointers on how to start with these. I’m really in doubt about what to do here. By the way I’m a bit more known with java, though as I noticed most people in unity write in C#, I’m trying to get a better understanding of this and write my code in C#.

  1. Buildplaces - I created a map by making 700 cubes and set them as “buildplaces”, setting the x and y coord per cube manually and adding some textures to them. Though especially for making multiple levels this takes a long time and doesn’t seem like the best way to go, but at least it’s easy to do. Only wondering here if this doesn’t make my game use up a lot of MB’s and run slower as you’d use so much cubes per level.

  2. Creating a grid where you have 32 by 32 tiles and you can just paint the sprites from your tileset on them by mouseclicking. I could not really find on how to create a grid like this and my scripting/coding skill is still low, which doesn’t make it easier to get a clear grip of where to start on this.

  3. Using TILED to make your map and import it to Unity. Here I’m unsure if you can set the tile settings in Unity with adding prefabs and other stuff. If so, this seems like a pretty easy solution.

I’m wondering on the opinion of people who have more coding experience on what the best way to create a 2D map is. Thanks for your time in advance. Any pointers are tremendously appreciated.

There are many ways you can create 2D maps in Unity.

I have a preferred way to do it, but it’s just one of many ways.

For data, unless your world size is random, a 2D Array works great. Either with a custom class (store multiple information per tile) or just a simple integer 2D Array

int[,] gameMap

Then it’s as simple as iterating through the loop and grabbing information. For example, if it’s just an int[,] 2D Array, then each ‘int’ would stand for a tiletype.

for(int y = 0; y < gameMap.size_Y; y++) //Scan through gameMap, to instantiate tiles.
         {
            for(int x = 0; x < gameMap.size_X; x++)
            {
		int tileType = gameMap[x,y];

                switch tileType 
                {
                    case 0:
                        GameObject tileObject = (GameObject)Instantiate(OceanTile, new Vector3((x * 256) - (256/2) * gameMap.size_X, 0, (y * 256) - (256/2) * gameMap.size_Y), Quaternion.identity);
                        tileObject.name = "[" + x + "," + y + "]Tile_Type" + thisType; //For Convenience in UnityEditor Hierarchy. Also helps in getting the tile type, tile position, etc via GameObject.name;
                        tileObject.SetParent(this.gameObject.transform);
                        break; //So this gameobject holds the entire world. You could attach this script to a gameobject "GameWorld".
		     default:
			Debug.Log("Error! Unknown tileType: " + tileType);
			break;
                }
            }
        }

What I do is have an array of more complex data, such as

public class MapData()
{
Vector2 myTilePosition; //The [X,Y] position of this tile in the worldmap.
int tileType;
}

So it’s pretty simple stuff.

MapData[,] gameMap;

public void InstantiateTilesFromMapArray2D()
{

for(int y = 0; y < gameMap.size_Y; y++) //Scan through gameMap, to instantiate tiles.
          {
             for(int x = 0; x < gameMap.size_X; x++)
             {
         int tileType = gameMap[x,y].tileType;
//etc.

}