Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Big tiled maps

Discussion in '2D' started by Morhem, Mar 2, 2023.

  1. Morhem


    Jan 24, 2015
    Hello! I'm sort of an intermediate noob when it comes to unity scripting, but this question has me a bit stumped:
    How do you handle 2D maps made from tiles? Terraria style. Is each tile a separate game object? I made a 513 * 513 random map (Generated from heights map of "Terrain" component), with 64x64 pixel sprites, but the engine did NOT handle it all that well. Do I only instantiate the tiles that are in/near the camera view? And destroy the ones that are not, when I move? But unity does not handle instantiating a lot of stuff all that well also... (admittedly, my laptop is weak, but still)
    Or maybe there's a better way to do it?
  2. Kurt-Dekker


    Mar 16, 2013
    There is a 2D TileMap system in Unity.

    If you want to do it yourself, first try just making it all at once.

    Unity is a beast of an engine. That will probably work just fine.

    (Although 513^2 is a pretty big number when it comes to GameObjects!)

    Otherwise, yes, you implement a system that creates / destroys the tiles as the map scrolls around.


    DO NOT OPTIMIZE "JUST BECAUSE..." If you don't have a problem, DO NOT OPTIMIZE!

    If you DO have a problem, there is only ONE way to find out. Always start by using the profiler:

    Window -> Analysis -> Profiler

    Failure to use the profiler first means you're just guessing, making a mess of your code for no good reason.

    Not only that but performance on platform A will likely be completely different than platform B. Test on the platform(s) that you care about, and test to the extent that it is worth your effort, and no more.

    Remember that optimized code is ALWAYS harder to work with and more brittle, making subsequent feature development difficult or impossible, or incurring massive technical debt on future development.

    Notes on optimizing UnityEngine.UI setups:

    At a minimum you want to clearly understand what performance issues you are having:

    - running too slowly?
    - loading too slowly?
    - using too much runtime memory?
    - final bundle too large?
    - too much network traffic?
    - something else?

    If you are unable to engage the profiler, then your next solution is gross guessing changes, such as "reimport all textures as 32x32 tiny textures" or "replace some complex 3D objects with cubes/capsules" to try and figure out what is bogging you down.

    Each experiment you do may give you intel about what is causing the performance issue that you identified. More importantly let you eliminate candidates for optimization. For instance if you swap out your biggest textures with 32x32 stamps and you STILL have a problem, you may be able to eliminate textures as an issue and move onto something else.

    This sort of speculative optimization assumes you're properly using source control so it takes one click to revert to the way your project was before if there is no improvement, while carefully making notes about what you have tried and more importantly what results it has had.
  3. SeerSucker69


    Mar 6, 2021
    You could store a huge map in a 2D array Map[x,y]=tiletype.

    Your map display could consist of 30x30 game object grid, and you just change the sprites in those gameobjects to reflect the sprite for the tiletype in that Map[x,y] position.