Search Unity

World Generation and Threading

Discussion in '2D' started by Lachlanmac, Mar 19, 2018.

  1. Lachlanmac

    Lachlanmac

    Joined:
    Mar 19, 2018
    Posts:
    1
    So, I've been exploring generating massive top-down maps for a 2D game and have come to a point where performance is holding me back during the initial map generation. The expensive part of my operation is in building a texture for each "Chunk" which I handle through setPixels32. I can generate a 10x10 Chunk Map (100 total textures) in about 30 seconds, but I want much larger worlds. My generation speeds would greatly benefit if I could build these textures at the same time. I'm no stranger to threading in general, but I can't seem to figure out the best way to accomplish this in Unity.


    What I want is some sort of Thread pool that blocks the main thread until all the "Jobs" are done, but Unity doesn’t seem to access to the Threading.Tasks library, so I'm hecka-stuck. I've done some research into the experimental .NET that you can opt into via Unity, but this seems to have changed in Unity 2018 Beta.


    The question really comes down to how should I handle heavy operations with concurrency in Unity to speed up my process.


    Thanks!
     
  2. furroy

    furroy

    Joined:
    Feb 24, 2017
    Posts:
    93
    This indeed does sound like a good candidate for the new Jobs system. You can setup the main thread to just loop waiting for the Jobs queue to finish.

    However, the best performance gains come from a) either not doing it at all or b) caching. Your world might be massive but if the player is in the upper left corner of the map do you really need to generate the entire rest of the map? Or can most of it be done slowly over timer while the player plays? Or can you generate the map piece by piece and save the data to reuse on the next launch so it doesn't all have to be redone from scratch?
     
    JoeStrout likes this.