- Home /
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#.
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.
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.
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.
Need to finish up a deadline, so can't test it all out right away, but I wanted to thank you already for the fast and great answer! This will certainly help me get on my way! Upvoted your answer also.
Answer by CarterG81 · Jul 05, 2015 at 05:22 PM
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.
}
If you want to store the tilemap (all tile gameobjects) it's also great I$$anonymous$$O to do it in an array. Really easy to access everything then.
Just slip in the switch(tileType)
GameObjectArray2D[x,y] = tileObject;
I think I did something wrong with replying, though I can't test this out right away as I need to finish up a deadline, but I wanted to let you know that I really appreciate your fast and great answer! Will definitely help me get on my way! Upvoted it also.
$$anonymous$$y comment wasn't an answer, it was a comment about the answer. $$anonymous$$y answer is the answer. $$anonymous$$y comment has nothing to do with the question or the answer, it's just a bonus.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Converting a mesh to terrain 2 Answers
How would you go about 'painting' images onto terrain 0 Answers