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 Convert file to byte array and visa versa

Discussion in 'Scripting' started by ZeroKuru, Nov 19, 2021.

  1. ZeroKuru

    ZeroKuru

    Joined:
    Jul 29, 2020
    Posts:
    3
    Hi everyone,

    I am using a Data save/load solution called Databox which I used to store all of my data like score, kills, gold, purchases etc. This works very well for my needs, but I'm having a hard time using the Google Play Games Services (GPGS for short) Saved Games to save my data on Android. I am using another solution called Easy Mobile, but regardless of that, Android only saves/loads data using a bytew array.

    I've configured Databox to store my data in the Persistent Data path as a .json file. It's encrypted in a binary format, but that's probably irrelevant in this situation.

    How would I go about converting this json file to a data array, and then later convert it back to json from the data array downloaded from the cloud?

    I've been pouring over the GPGS, Unity, Microsoft and Easy Mobile documentation but not really seeing anything. I found that there's a method called File.ReadAllBytes and File.WriteAllBytes and I think I figured out how to use the ReadAllBytes, but not WriteAllBytes to convert the byte array back to my json file.

    This is what I've managed to come up with for reading the json file to byte array.

    Code (CSharp):
    1.     public void WriteSavedGame(SavedGame savedGame)
    2.     {
    3.         if (savedGame.IsOpen)
    4.         {
    5.             byte[] data = File.ReadAllBytes(Application.persistentDataPath + "/Data");
    6.             GameServices.SavedGames.WriteSavedGameData(savedGame, data, WriteSavedGameCallback);
    7.         }
    8.     }
     
    Last edited: Nov 19, 2021
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,384
    It appears you have it already.

    File.WriteAllBytes is just like ReadAllBytes, just that it writes it:
    https://docs.microsoft.com/en-us/dotnet/api/system.io.file.writeallbytes?view=net-6.0

    You pass in a string for the path, and the byte array.

    Code (csharp):
    1. File.WriteAllBytes(Application.persistentDataPath + "/Data", data);
    What you need to do is GET that byte array from GameServices. I don't know what GameServices is or what methods are on that to retrieve said byte array... you'd have to consult whatever documentation there is for GameServices.SavedGames that exist.
     
    Bunny83 likes this.
  3. ZeroKuru

    ZeroKuru

    Joined:
    Jul 29, 2020
    Posts:
    3
    I tried that, but I've been getting an error "cannot assign 'void' to an implicitly-typed local variable".

    I'll do some more digging. Right now I have about 15 open tabs in my browser :D

    Edit: Thank you for your quick response. Means a lot
    Edit2: I have figured it out. Thanks again for your help. I was getting an error because I tried to return a variable from a function that has no return value

    Code (CSharp):
    1. //This returned the error
    2. var cloudData = File.WriteAllBytes(Application.persistentDataPath + "/Data", data);
    3.  
    4. //This worked fine
    5. File.WriteAllBytes(Application.persistentDataPath + "/Data", data);
     
    Last edited: Nov 19, 2021