- Home /
Tilemap 2D
Before Unity I was using libGDX(java), now I moving to unity. After some look around unity I realized that it not using any directly draw methods like spritebatch.draw() or something like this. This is good for 3D, you don't need write huge draw code for all reflections, effects and so on, but for 2D games is not really good(I think, maybe I'm wrong). I was trying to rebuild(in unity) my tilemap engine from previous project(in libgdx).
My map is just big two dimensional array of integers, every integer have it's own tile, so in render method for every integer(in view) specific tile is renderer. In this way I also getting nice tile transitions like this:
So my question would be: How to draw tiles in unity?
Also my map is big(1024x1024 tiles, each tile is 16x16), so I do not think that creating game object for every tile is good.
You might want to try this program for tilesets:
and import the maps you make using
http://www.seanba.com/tiled2unity
to learn how to use this go to
https://www.youtube.com/watch?v=NNpLsimyu3I&list=PL_4rJ_acBN$$anonymous$$H3SExL3yIOzaqj5IP5CJLC
Problem is that my map is generated each time you enter the game, so I don't want to use tiled
Answer by tanoshimi · Feb 09, 2016 at 07:23 AM
You're right - you certainly don't need (or want!) to create a separate gameobject for each tile in a large map. Instead, you should simply add them as quad faces to a single dynamic mesh. You assign appropriate UV coordinates to the corners of each quad to look up the appropriate tile texture from your atlas.
There's plenty of example code if you search for "procedural mesh", and here's the documentation: http://docs.unity3d.com/ScriptReference/Mesh.html
Also I wan't to know why I can't use something like:
for (int y = 0; y < mapHeight; y++) {
for(int x = 0; x < mapHeight; x++) {
Graphics.DrawTexture(new Rect(x * tileWidth, y * tileHeight, tileWidth, tileHeigt), tileTexture);
}
}
In unity?
Your answer
Follow this Question
Related Questions
Array of Tilemaps misbehaving 1 Answer
2D Draw text at sprite position with script only 1 Answer
[Tilemap] Can you spawn a Prefab (with colliter) together with a tile when placing it? 0 Answers
Merging Objects (many tiles into one) - 2D 2 Answers
Trouble with tilemaps as prefabs for 2D plattformer 1 Answer