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. Dismiss Notice

Question How to write to File using BinarySerialization from Unity Serialization Package?

Discussion in 'C# Job System' started by John_Leorid, Sep 25, 2023.

  1. John_Leorid

    John_Leorid

    Joined:
    Nov 5, 2012
    Posts:
    615
    The Documentation says:

    Source: https://docs.unity3d.com/Packages/c...Serialization.Binary.BinarySerialization.html

    Let's ignore the fact that it says "json data" which has to be wrong because it's binary data and focus on the "File" part of the sentence.
    There has to be a way to write to a binary file with this package. I need the performance and I don't trust any other Asset / Github Repo or anything. I wan't to read/write with Burst performance because in my specific case we are talking about Gigabytes of data.

    Has anyone ever done this? Any examples? Anything?
    I've found literally no results on google. Seems like no one has ever used it in this way. So maybe some Unity Devs could help out?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,721
  3. John_Leorid

    John_Leorid

    Joined:
    Nov 5, 2012
    Posts:
    615
    I see, so the serialization package is unable to access anything on the hard drive?

    I've checked the JSON Part of the Package and there are some special classes for parallel reading/writing from the Hard Drive based on a File-Stream that's then used in parallel. But it only works with "char" which wouldn't work with bytes.

    So basically all the Serialization Package can do for me is converting Objects to byte arrays and vice versa. If I need parallel reading from files, I have to implement it myself, right?

    Still thanks a lot for the code snippets, I'll probably just read bytes, use your code for conversion (byte[] to NativeArray) and then hope for the best. xD
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,721
    A char is a byte in disguise. ;)

    Parallel IO shouldn‘t require more than async read/write methods.
    In one of my examples you‘ll also see that I Gzip the byte array because the time it takes to zip can be far outweighed by the disk read/write performance - depends on the data and how compressible it is and of course the device/hardware.
     
    John_Leorid likes this.