2

I've successfully logged in with Google Play services in Unity, but I have a problem now that i have a class that contains some data like this:

public static class GameData
{

   public static Dictionary<string, object> Name_Dic = new Dictionary<string, object>() 
   {
      {"laura",""},
      {"steph ",""},
      {"Ryan",""},      
   };

   public static Dictionary<string, string> Dialogs_Dic = new Dictionary<string, string>() 
   {
      {"A1", "Hello"},
      {"A2", "Nice"},
      {"A3", "Test"},
   };

   public const int nbrTotal_Int = 2;
   public const int TestNumber_Int = 5;
}

I have to save these data to the Google Cloud and after that load them from cloud, but i m confused i don't understand if i have to convert the dictionary in the GameManager class into String and then in to Bytes[] in order to save them in the Google Cloud or what can i do exactly. and the int variables i should convert them into string then into bytes[]. i ve searched into the internet and i can't found an explanation or tutorial for that and in the official documentation or the samples in the documentation it is not clear what can i do. Can you please help me please. Thanks a lot


Update

Hello, Thanks for your answer, i ve written a code that let me save these variables to Google Cloud, here it is a part of the serialization the dictionary and the ints. is it correct like this. thanks for your answer.

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

public class Connect : MonoBehaviour 
{
public Dictionary Name_Dic = new Dictionary() 
{
{"laura",""},
{"steph ",""},
{"Ryan",""},

};
public Dictionary Dialogs_Dic = new Dictionary() 
{
{"A1", "Hello"},
{"A2", "Nice"},
{"A3", "Test"},
};
public int nbrTotal_Int = 2;
public int TestNumber_Int = 5;

public void WriteFunction() {

ISavedGameMetadata currentGame = null;


Action<SavedGameRequestStatus, ISavedGameMetadata> writeCallback = 
(SavedGameRequestStatus status, ISavedGameMetadata game) => {
    Debug.Log(" Saved Game Write: " + status.ToString());
};

// CALLBACK: Handle the result of a binary read
Action<SavedGameRequestStatus, byte[]> readBinaryCallback = 
(SavedGameRequestStatus status, byte[] data) => 
{
    Debug.Log(" Saved Game Binary Read: " + status.ToString());
    if (status == SavedGameRequestStatus.Success) 
    {

        try {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(Application.persistentDataPath + "/gameInfo.dat",
                                        FileMode.Open);

            GameData data = new GameData();
            // Two Dictionary 
            data.Name_Dic = Name_Dic;
            data.Dialogs_Dic = Dialogs_Dic;
            // Two Int
            data.nbrTotal_Int = nbrTotal_Int;
            data.TestNumber_Int = TestNumber_Int;

            bf.Serialize(file, data);
            file.Close();
        } catch (Exception e) {
            Debug.Log(" Saved Game Write: convert exception");
        }
        WriteSavedGame(currentGame, data, writeCallback);
    }
};

}
}

[Serializable]
public class GameData 
{
 public Dictionary Name_Dic = new Dictionary() 
{
 {"laura",""},
 {"steph ",""},
 {"Ryan",""},

};
public Dictionary Dialogs_Dic = new Dictionary() 
{
{"A1", "Hello"},
{"A2", "Nice"},
{"A3", "Test"},
};
public int nbrTotal_Int = 2;
public int TestNumber_Int = 5;
}``
Happy Apple
  • 726
  • 4
  • 13
  • Converting objects to a byte array and back is called serialization. There's an example of how to use BinaryFormatter in a post here http://stackoverflow.com/questions/22972607/convert-dictionary-or-list-to-byte I haven't used Google Play Services but if you posted a link to any API documentation for what you're trying to do, we may be able to help more. – Happy Apple Mar 21 '16 at 01:10

0 Answers0