1
\$\begingroup\$

I just finished the Unity Tutorial for creating a simple 2D roguelike Mobile Game. Now I wanted to make my level bigger; finding or creating my own algorithm isn't the problem. My problem is that creating a game object or using a prefab for every single floor or wall tile is a bad idea. (Map 200x200 = 40000 objects). A comment under the tutorial said that and I'm incline to believe it. That much I can understand, but I can't find a alternative. What can I use besides object/prefabs?

Sidenote: I have decent programming skills, but am beginner with Unity.

The tutorial I followed: https://www.youtube.com/playlist?list=PLX2vGYjWbI0SKsNH5Rkpxvxr1dPE0Lw8F I want to make the maps there bigger, but then everything goes slow motion.

\$\endgroup\$
  • 1
    \$\begingroup\$ Can you show us what you want your level to look like? That can suggest ways that we can build the level in larger chunks, or use more efficient building blocks, or window the generation so only a portion of the objects need to be in existence at a time. \$\endgroup\$ – DMGregory May 28 '19 at 22:26
  • \$\begingroup\$ New ECS system can be a good alternative but keeping 40k active objects is not good for any system in general. Are you sure that you really need 40k objects each frame? \$\endgroup\$ – RiotBr3aker May 28 '19 at 22:52
  • 2
    \$\begingroup\$ There's the built-in Tilemap in Unity if you want to make a tile-based 2d game \$\endgroup\$ – Bálint May 28 '19 at 23:10
  • \$\begingroup\$ RiotBr3aker my point was that I want a alternativ that doesnt need 40k game objects alive. But my knowledge with unity is very limited and in the tutorial he makes a new object for every tile. That cant be the best solution. \$\endgroup\$ – Christopher May 29 '19 at 5:28
  • \$\begingroup\$ Balint Tilemap sound like something that I need. \$\endgroup\$ – Christopher May 29 '19 at 5:31
3
\$\begingroup\$

Tile map is one option to use, the other is to maintain only the number of tiles that can be seen on screen at any given moment, and copy the tiles that go offscreen to the location of the next tiles to go on screen.

\$\endgroup\$
1
\$\begingroup\$

The basic alternative to having many game objects would be to construct one game object that has all of your geometry. If the game is tile based, then you could have a game object for each of the unique varieties of tiles and then at runtime you could generate the map mesh programmatically from a data structure e.g. a 2D array which represents which tiles to place at which locations.

Essentially you would need read the geometry from the "unique tile" game objects and copy it into the "map" game object, offsetting vertex coordinates for each tile with the proper x/y offset based on where you want to place it in the map.

This is still not completely optimal in that you may end up with a lot more triangles than you really need. If you were really concerned about performance you could try to further simplify adjacent co-planar triangles, but even if you were not doing any geometry simplification it is still probably a whole lot more efficient than one game object per tile.

(My answer is under the assumption that the tiles have interesting 3D geometry. If they are just flat textured tiles then the Tilemaps suggested by another commenter would probably be the better option. It doesn't seem like Tilemaps support 3D tiles though.)

\$\endgroup\$
  • \$\begingroup\$ First. I only want it to be different textures right now, the tiles arent any different apart from the look. Second. Isnt making a copy still making 2 objects? My knowledge is very limited, so if you say NO, I would totally believe you. \$\endgroup\$ – Christopher May 29 '19 at 5:39
  • \$\begingroup\$ Copying geometry at the vertex level is likely to have a lot less overhead than copying an entire game object. Since you are just wanting to replace textures, the analogous idea to what I said earlier would be to have code which generated the geometry for a grid of squares, each square getting the right texture coordinates for the appropriate tile image. (having all of your tiles in a single texture map would be helpful, then you can just assign a single material to the entire grid). But the existing Tilemap feature is probably about the same thing, and with better tooling presumably. \$\endgroup\$ – uglycoyote May 29 '19 at 6:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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