Search Unity

Feedback Placing Tiles

Discussion in '2D' started by AjdiNNNN, Dec 14, 2022.

  1. AjdiNNNN

    AjdiNNNN

    Joined:
    Jan 14, 2019
    Posts:
    22
    Is there any news on preformance optimizations of setTile and setTiles because currently its hella slow to do procedural generation of map....
     
  2. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    490
    Do you perform procedural calculations on something simple to the computer like a List<List<int>> and then translate the result into a Tilemap? People also often forget that you can store a lot of information in one integer using masks (bitwise operations).
     
    Last edited: Dec 14, 2022
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,695
    Do you have profiler data to back this assertion? I would be more inclined to think it was your procedural code, until you could demonstrate otherwise.

    Either way, it's always possible to use an API such as SetTile() in a naive or sub-optimal way, and if you are doing that, then fix that first.

    If you have a performance 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.

    https://forum.unity.com/threads/is-...ng-square-roots-in-2021.1111063/#post-7148770

    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:

    https://forum.unity.com/threads/how...form-data-into-an-array.1134520/#post-7289413

    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.
     
  4. flasker

    flasker

    Joined:
    Aug 5, 2022
    Posts:
    193
    using settile 3000 times in the same frame is sure to cause a spike even on a good machine

    use coroutine to run it only 100x per frame
     
  5. AjdiNNNN

    AjdiNNNN

    Joined:
    Jan 14, 2019
    Posts:
    22
    When I remove SetTiles from script from being unable to load scene I load scene in 5 seconds, with all computation of procedural map, only when actually painting tiles with SetTile or SetTiles it takes ages, painting 1500*1500 map with 100 tiles per map would take 6 minutes to load which still is stupid amount to wait for acutal user... And how I store tilemap information is by taking fixed size TileBase array putting information in it and then painting it with tilemap.SetTiles(position, TileBaseArray)
    Here is profiler of that frame when i execute painting 1500*1500 tilemap
     
    Last edited: Dec 16, 2022
  6. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    490
    So try to create a new scene and a script to generate 1500x1500 tiles. It takes exactly 5-6 seconds (on my Intel Core i3-7100 CPU, 3.90GHz). Settings tiles is very fast. Something else in the function must be forcing you to add extra time.
    1. I hope you don't use Find function (for example to find tilemap)?
    2. Don't you update the map during procedural generation (i.e. you do some calculations and your hidden functions automagically update the tiles)?
    3. You keep the data on the matrix as numbers, identifiers (or even several if you use bit operations), and when adding tiles you only have a function that translates the value from this field to a specific TileBase. Computers like numbers (such operations are lightning fast). So you don't keep in memory 1500x1500 structures with x and y coordinates, field name, terrain type, biome, etc.
     
    Last edited: Dec 16, 2022
  7. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,511
    Do you use a lot of RuleTiles? These are the beefiest ones. Colliders? Composite Colliders?

    Also why is it allocating so much? Like @AngryProgrammer, I suspect there's something else afoot.

    Please use Deep Profiling on a smaller map (Deep Profiling will make it take much more time). Unfurl the major costly bits and repost the profiler results.
     
  8. AjdiNNNN

    AjdiNNNN

    Joined:
    Jan 14, 2019
    Posts:
    22
    I use animated tile with tilemap collider on them which paint on one tilemap and 2nd tilemap which are basic rule tiles, but when i put regular tiles to be painted same thing