Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Kalashj · Jul 05, 2015 at 04:37 PM · c#editorterrainmesh

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kalashj · Jul 05, 2015 at 09:59 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
4

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.

}

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CarterG81 · Jul 05, 2015 at 05:31 PM 0
Share

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;

avatar image Cherno · Jul 05, 2015 at 08:58 PM 1
Share

Good answer. Upvoted.

avatar image Kalashj · Jul 05, 2015 at 09:05 PM 1
Share

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.

avatar image tanoshimi · Jul 05, 2015 at 10:00 PM 0
Share

Please don't post comments as answers.

avatar image CarterG81 · Jul 06, 2015 at 06:02 PM 0
Share

$$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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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

Initialising List array for use in a custom Editor 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges