- Home /
Serialized class to google games services cloud save
I am just now incorporating google play game services into my project. I already have a system in place for saving local snapshots. I am now trying to save these snapshot classes to the cloud. Here is an example class to illustrate my method:
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using GooglePlayGames;
using UnityEngine.SocialPlatforms;
public class GMan : MonoBehaviour {
public int score;
public Text text;
public Text connected;
public bool IsConnectedToGoogleServices;
public static GMan Instance;
void Awake()
{
Instance = this;
PlayGamesPlatform.Activate();
PlayGamesPlatform.DebugLogEnabled = true;
}
// Use this for initialization
void Start () {
ConnectToGoogleServices();
Load();
}
public void AuthTest()
{
Social.localUser.Authenticate((bool success) =>
{
IsConnectedToGoogleServices = success;
});
}
public bool ConnectToGoogleServices()
{
if (!IsConnectedToGoogleServices)
{
Social.localUser.Authenticate((bool success) =>
{
IsConnectedToGoogleServices = success;
});
}
return IsConnectedToGoogleServices;
}
void OnApplicationFocus(bool focusStatus)
{
if (focusStatus == false)
{
Instance.Save();
}
}
// Update is called once per frame
void Update () {
text.text = score.ToString();
string str;
if (IsConnectedToGoogleServices)
{
str = "true";
}else
{
str = "false";
}
connected.text = "Connected: " + str;
}
public void Tick()
{
score++;
}
public void ToAchieve()
{
Social.ShowAchievementsUI();
}
public void Save()
{
print("Saving Game");
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/SaveData.dat");
SaveData saveData = new SaveData();
saveData.score = Instance.score;
bf.Serialize(file, saveData);
file.Close();
}
public void Load()
{
if (File.Exists(Application.persistentDataPath + "/SaveData.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/SaveData.Dat", FileMode.Open);
SaveData saveData = (SaveData)bf.Deserialize(file);
file.Close();
Instance.score = saveData.score;
}
}
}
[System.Serializable]
public class SaveData
{
public int score;
}
Play game services takes a byte[] as the payload param. How would I convert the instance of saveData to byte[] for saving. How would I turn a byte[] into an instance of saveData for loading?
Is a serialized class just a byte[]?
Hi Astrydax.
Was wondering if you ever figured this out because I'm struggling with the same problem. I have existing local serialized saved files that I want to send to google play services cloud save. Any help will be appreciated, thank-you.
hello :) did you find any good solution to this??? I have the same issue, I have serialized class for my game and I'm about to enable cloud service Thanks