Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Best way to show a 2d array which updates on Update()?

Discussion in 'General Graphics' started by OrangeJuice224, Nov 7, 2023.

  1. OrangeJuice224

    OrangeJuice224

    Joined:
    Jul 5, 2023
    Posts:
    9
    Hello,

    I've a 2D array. The data in this array is updated on every Update(). What I want is to show this 2D array visually.

    I first thought to add 16 x 16px sprites as a grid programmatically in the scene. Then just update the sprite colors.

    Then I searched a little bit. Unity has a Tilemap package. Should I use this instead?

    Which is better?
    Which is faster?
    Which has the best performance?
    Or should I do it different?

    Water example images - How it will nearly look like:
    Update() 1


    Update() 2 - and so on:


    It will not only be water. Water is just an example.

    Thanks to everybody :)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,271
    You can begin your investigation with the Profiler or the Frame Debugger.

    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.

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

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

    Remember that you are gathering information at this stage. You cannot FIX until you FIND.

    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.

    Don't forget about the Frame Debugger either, available right near the Profiler in the menu system.

    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.

    "Software does not run in a magic fairy aether powered by the fevered dreams of CS PhDs." - Mike Acton
     
    OrangeJuice224 likes this.
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,229
    Really depends on your requirements. Do you need colliders? Smart tiles? Etc? Then Tile maps would be useful.

    Don't need any of that? Just drawing coloured squares? Then maybe you just want to write colours to a texture and draw that. I did that with a falling sand game prototype once.

    Profiling is important here of course, too.
     
    OrangeJuice224 likes this.
  4. sildeflask

    sildeflask

    Joined:
    Aug 16, 2023
    Posts:
    315
    if you dont have to update it every frame the tilemap is the best by far

    if you have to update it every frame then your idea with just changing the color of sprites is best
    (but this will have very bad performance due to many objects on scene)

    on the otherside, the tilemap performs well because its just one object
    (however, the tilemap performs badly this time only when its updated)

    you have a big problem on your hands
     
    OrangeJuice224 likes this.
  5. OrangeJuice224

    OrangeJuice224

    Joined:
    Jul 5, 2023
    Posts:
    9
    Thanks for the answers :)

    Still trying to solve this.

    I don't need colliders. I only need to change the color. That's everything.

    I tested this with the tilemap. When I press play, the FPS is about 340 FPS.
    When I know update the tilemap on every 100ms like this:
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.InvokeRepeating.html
    Code (CSharp):
    1. InvokeRepeating("UpdateTilemap", 2.0f, 0.1f);
    The FPS drops to 110 FPS.

    I've also tried it with a texture which is attached to a quad.
    I change the pixels with SetPixels().
    Same here. The FPS drops.

    My Grid is currently 120 x 68 (grid size can change later)
     
    Last edited: Nov 8, 2023
  6. sildeflask

    sildeflask

    Joined:
    Aug 16, 2023
    Posts:
    315
    its a very nasty problem

    the tilemap must use settile, or settileblock, or refreshtile

    all of this is performance nightmare
    its insane to use this for your purpose, it might work on a small surface but if you intend to scale this up it will never work

    for setpixels its the same dance
    setpixels and .apply() are also insane performance cost, the bigger your texture the worse its gonna be

    the most sensible option would be to have many game objects
    but this is also another problem, because while it will be a breeze to update the visuals, it has a passive permanent cost on performance because many objects creates a lot of geometry being rendered at the same time, even with batching its gonna be too many tris and verts on screen

    i dont know what to reccomend you, except that - if all you want is something the size that you showed, then its easy, just take a bit of performance cost, but i imagine that you want to do something much much bigger, and you will never manage to with any of these 3 options
     
    OrangeJuice224 likes this.
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,105
    When you scale things up, you need to know what you're doing. You cannot brute force stuff and TBH it's not clear what you need here.

    Rendering things in a grid does not require Tilemap if you're going to be changing lots/most of it frequently. It "sort of" works but it's not meant for that purpose.

    You need to manage what is rendered and batch them up for rendering using something like:
    https://docs.unity3d.com/ScriptReference/Graphics.RenderMeshInstanced.html

    You just need to have the mesh/materials you need and then all you need to do is modify what gets rendered where. All that can be done in a job using the Job System.

    I'm not sure what this means specifically TBH. I think for more information on rendering, this post would be better suited on the General Graphics sub-forum.
     
    OrangeJuice224 likes this.
  8. OrangeJuice224

    OrangeJuice224

    Joined:
    Jul 5, 2023
    Posts:
    9
    @MelvMav Thanks for the really good answers. OK. I ask this on the General Graphics sub-forum.
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,105
    I can move this thread there.
     
    OrangeJuice224 likes this.
  10. OrangeJuice224

    OrangeJuice224

    Joined:
    Jul 5, 2023
    Posts:
    9
    @MelvMay Thanks for moving this thread.

    Hope somebody can help me here with this problem.

    In short:
    I have a 2D Array. I want to show this array visually in Play Mode.
    The array values are changed in 100ms interval.
    Now I need to show them as a grid. Please look at the first above post example images.

    Additional:
    I also tested like this: I instantiated Quad game objects in a grid.
    Then I did SetActive(true) when array[x, y] = true else SetActive(false) when array[x, y] = false;
    The FPS drops to 20 FPS.
    Even worse than changing the color.
     
  11. sildeflask

    sildeflask

    Joined:
    Aug 16, 2023
    Posts:
    315
    are u trying to do this?:

     
    OrangeJuice224 likes this.
  12. OrangeJuice224

    OrangeJuice224

    Joined:
    Jul 5, 2023
    Posts:
    9
    No. I know this. That's like Sand Falling.

    I'm just only looking for a something to show an 2D array in Play Mode.
     
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,105
    Saying "a 2D Array" is meaningless too so you should stop referring to that. From what we can figure out so far (because it's not super clear) you want to render lots of sprites (or just quads) in a grid and manipualte the colors.

    To be honest, my reply gives you everything you need to render stuff in batch for efficiency.

    If you're super inexperienced with this kind of stuff then you're going to struggling until you step back and learn how to batch stuff for rendering and how to manipulate lots of data using something like the C# Job System.
     
    OrangeJuice224 likes this.
  14. OrangeJuice224

    OrangeJuice224

    Joined:
    Jul 5, 2023
    Posts:
    9
    I will first try to learn the "C# Job System".
    Thanks.
     
  15. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,105
    I would start with getting the rendering working first with something like https://docs.unity3d.com/ScriptReference/Graphics.RenderMeshInstanced.html You'll learn how to create meshes, materials etc.

    When that's rendering fast without changes (on the CPU) then you can think about speeding-up your changes using something like the Job System assuming it's even a bottleneck at all.
     
    OrangeJuice224 likes this.