Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've been searching all over the internet to find a efficient way to create procedural tilemap without creating a gameobject per each tile. There is none, or simply i couldn't find it, because i really don't know how to make a search for it. All the TileMap tutorials i found are creating tiles by creating hunderds of gameobjects. Thus the hierarchy in Unity is expanding uncontrollably.

I'm pretty sure this is not the "right" way to do it. Especially after seeing the new Unity 2D tools that supports tilemaps. Unity made it so, you can create tiles in "one" gameobject, not for each tile.

How to do it in the right way?

share|improve this question
1  
You've cross posted this to two SE sites. Pick one. –  Almo Jun 18 at 18:56
1  
What's your specific concern with creating large numbers of GameObjects? If it's just hierarchy clutter, there are easy ways to fix that. Unity's batching should combine similar tiles into a single batch anyway, so there's unlikely to be a major draw call penalty to doing it this way. Minimizing offscreen tiles and time spent in tile update functions are both solvable problems too. Have you profiled your game and identified a specific bottleneck you're trying to solve? –  DMGregory Jun 18 at 19:27
    
@DMGregory Well if we think about 100*100 map in screen (if we assume we zoomed out to see whole map), it will make 10.000 gameobjects in the scene. It also means 10.000 unncesary transform component. I don't even know if unity can support 10.000 object in hieararchy window. I know it freezes after couple hundred. –  BerkayDrsn Jun 18 at 20:02

2 Answers 2

As you (I think) alluded to, Unity's roadmap has plans for a tilemap editor. I'm looking forward to it, because right now it's a little confusing how to proceed.

In the meantime, the best approach I'm seeing is to make your map in Tiled and then use X-UniTMX to import it into Unity. Obviously that's not procedural generation though; I'd imagine you would use the same technique as X-UniTMX is using to create the mesh.

share|improve this answer

You could just have the tiles created deeper in the hierarchy and close that folder and don't be bothered. What you can do as well is hide your game objects from the hierarchy `myTile.hideFlags = HideFlags.HideInHierarchy" not sure about how much performance would be gained.

Showing 100x100 textures on the screen is never a good idea drawing textures is more heavy then drawing polygons anyway. If you need to zoom out that far you are better off with bigger textures. Same goes for being zoomed in, you should not draw the textures that do not need to be drawn.

What I usually do (I'm not a Unity guy) is creating a Class for a map and use a 2 dimensional array for holding all your tiles. This array could be simply of ints or a Tile class with a couple of ints that refer to your textures, tile type, objects, etc. Then one would draw it as follows:

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);
    }
}

The draw method I use seems to be for drawing a GUI but might be perfectly suitable for the task. Otherwise you have to dig into the sprite manager and find a way that works with this method.

You could use a dictionary for texture reference like int 1 in your tile class refers to some texture and int 2 refers to another. In the line you are drawing your texture you would use your dictionary to give the right texture object. This way it should not create a GameObject and thus not create all unnecessary overhead.

You can do the same for pretty much everything that does not need the level of control of a GameObject. Mind though, this way it will draw every tile of your map it most probably does not auto cull the objects like I think unity does with game objects. But just drawing what needs to be on screen is very easy. You know the screen/viewport size, camera position and the tile size. Some simple math should draw exactly what's needed.

Still, unity is a 3D engine. It just supports 2D in a 3D way. But you can basically do the same thing for quads/meshes with textures and a Orthographic camera. Just drawing those meshes needed is pretty cheap.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.