- Home /
Google Cloud Save Data returns empty.
Hello, I have been trying to implement Google Cloud Save for the last couple of days but it is not working right. I am able to save my data locally using BinaryFormatter and FileStream but I can't seem to get it working for Google Cloud Save. Somehow I can store my data but when I try to load, it returns empty. I know I am not converting my data to byte correctly but I can't seem to figure out how to do it correctly. I am wondering where did I go wrong and is there a better way to store data to Google Cloud Save (such as using JSON?, Protobuff?, XML?). If so, which one is more ideal for mobile development. Thanks in advance for any help.
public void OnSavedGameOpened(SavedGameRequestStatus status, ISavedGameMetadata game)
{
if (status == SavedGameRequestStatus.Success)
{
if (isSaving) // Writing our data to cloud
{
try
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
CharacterStatistics savedData = new CharacterStatistics();
savedData = PlayerState.Instance.localPlayerData;
bf.Serialize(ms, savedData);
//ms.Flush();
byte[] myByteArray = ms.ToArray();
//byte[] myByteArray;
//using (MemoryStream stream = new MemoryStream())
//{
// bf.Serialize(stream, savedData);
// myByteArray = stream.ToArray();
//}
Debug.Log("Current Leve: " + savedData.Level + " Current XP: " + savedData.CurrentXP);
SavedGameMetadataUpdate.Builder builder = new SavedGameMetadataUpdate.Builder()
.WithUpdatedPlayedTime(TimeSpan.FromMinutes(game.TotalTimePlayed.Minutes + 1))
.WithUpdatedDescription("Saved game at " + DateTime.Now);
SavedGameMetadataUpdate updatedMetadata = builder.Build();
((PlayGamesPlatform)Social.Active).SavedGame.CommitUpdate(game, updatedMetadata, myByteArray, OnSavedGameWritten);
}
catch (Exception e)
{
Debug.Log(" Saved Game Write: convert exception");
}
}
else // Reading our data from the cloud
{
((PlayGamesPlatform)Social.Active).SavedGame.ReadBinaryData(game, OnSavedGameDataRead);
Debug.Log("Reading our data from cloud.");
}
}
else
{
Debug.Log("Error, cannot save data: " + status);
}
}
public void OnSavedGameDataRead(SavedGameRequestStatus status, byte[] data)
{
if (status == SavedGameRequestStatus.Success)
{
if (isReading) // Reading our data from the Cloud
{
// handle processing the byte array data
Debug.Log("Reading data from cloud saved.");
}
else // Loading our data from the Cloud.
{
// handle processing the byte array data
if (data == null)
{
Debug.Log("No data saved to the cloud yet...");
//LoadLocal();
return;
}
CharacterStatistics savedData = FromBytes<CharacterStatistics>(data);
Debug.Log("Loading data from cloud.");
}
}
else
{
// handle error
Debug.Log("Error reading game: " + status);
}
}
Have you Enable save game in developer console.?
Have you added this code :
PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
// enables saving game progress.
.EnableSavedGames()
// registers a callback to handle game invitations received while the game is not running.
.WithInvitationDelegate(<callback method>)
// registers a callback for turn based match notifications received while the
// game is not running.
.With$$anonymous$$atchDelegate(<callback method>)
// require access to a player's Google+ social graph (usually not needed)
.RequireGooglePlus()
.Build();
.EnableSavedGames() is used to enable Save game . Please check this .
https://github.com/playgameservices/play-games-plugin-for-unity
And you can check this link. There is some bug in save data https://github.com/playgameservices/play-games-plugin-for-unity/issues/1272
Yes Google Cloud Save is Enable and I have read about that issue. However, I am not sure if I am saving any data to Google Cloud because when I try to load the data back it says SerializationException: serializationS$$anonymous$$m supports seeking, but its length is 0. I am not sure if I am even converting my data to by$$anonymous$$rray and saving any data.
Here is my FromByte $$anonymous$$ethod:
T FromBytes<T>(byte[] data)
{
BinaryFormatter bf = new BinaryFormatter();
$$anonymous$$emoryStream mem = new $$anonymous$$emoryStream(data);
return (T)bf.Deserialize(mem);
}
Here is my LogCat,
09-29 11:55:59.434 1521 1575 I Unity : (ProjectRunner) Signed in!
09-29 11:55:59.434 1521 1575 I Unity :
09-29 11:55:59.434 1521 1575 I Unity : (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 42)
09-29 11:55:59.434 1521 1575 I Unity :
09-29 11:56:06.584 1521 1930 I Unity : [Play Games Plugin DLL] 09/29/16 11:56:06 -07:00 DEBUG: Entering internal callback for Snapshot$$anonymous$$anager#OpenCallback
09-29 11:56:06.584 1521 1930 I Unity :
09-29 11:56:06.584 1521 1930 I Unity : (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 42)
09-29 11:56:06.584 1521 1930 I Unity :
09-29 11:56:06.614 1521 1575 I Unity : Current Leve: 1 Current XP: 5
09-29 11:56:06.614 1521 1575 I Unity :
09-29 11:56:06.614 1521 1575 I Unity : (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 42)
09-29 11:56:06.614 1521 1575 I Unity :
09-29 11:56:07.294 1521 1930 I Unity : [Play Games Plugin DLL] 09/29/16 11:56:07 -07:00 DEBUG: Entering internal callback for Snapshot$$anonymous$$anager#CommitCallback
09-29 11:56:07.294 1521 1930 I Unity :
09-29 11:56:07.294 1521 1930 I Unity : (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 42)
09-29 11:56:07.294 1521 1930 I Unity :
09-29 11:56:07.314 1521 1575 I Unity : Current Game: Saved game at 09/29/2016 11:56:06 written
09-29 11:56:07.314 1521 1575 I Unity :
09-29 11:56:07.314 1521 1575 I Unity : (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 42)
09-29 11:56:07.314 1521 1575 I Unity :
09-29 11:56:09.984 1521 1575 I Unity : Trying to load Cloud Data.
09-29 11:56:09.984 1521 1575 I Unity :
09-29 11:56:09.984 1521 1575 I Unity : (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 42)
09-29 11:56:09.984 1521 1575 I Unity :
09-29 11:56:11.274 1521 1930 I Unity : [Play Games Plugin DLL] 09/29/16 11:56:11 -07:00 DEBUG: Entering internal callback for Snapshot$$anonymous$$anager#OpenCallback
09-29 11:56:11.274 1521 1930 I Unity :
09-29 11:56:11.274 1521 1930 I Unity : (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 42)
09-29 11:56:11.274 1521 1930 I Unity :
09-29 11:56:11.294 1521 1575 I Unity : Reading our data from cloud.
09-29 11:56:11.294 1521 1575 I Unity :
09-29 11:56:11.294 1521 1575 I Unity : (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 42)
09-29 11:56:11.294 1521 1575 I Unity :
09-29 11:56:11.294 1521 1930 I Unity : [Play Games Plugin DLL] 09/29/16 11:56:11 -07:00 DEBUG: Entering internal callback for Snapshot$$anonymous$$anager#ReadCallback
09-29 11:56:11.294 1521 1930 I Unity :
09-29 11:56:11.294 1521 1930 I Unity : (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 42)
09-29 11:56:11.294 1521 1930 I Unity :
09-29 11:56:11.314 1521 1575 I Unity : SerializationException: serializationStream supports seeking, but its length is 0
09-29 11:56:11.314 1521 1575 I Unity : at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.NoCheckDeserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.$$anonymous$$essaging.HeaderHandler handler) [0x00000] in <filename unknown>:0
09-29 11:56:11.314 1521 1575 I Unity : at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream) [0x00000] in <filename unknown>:0
09-29 11:56:11.314 1521 1575 I Unity : at GooglePlay$$anonymous$$anagerSetup.FromBytes[CharacterStatistics] (System.Byte[] data) [0x00000] in <filename unknown>:0
09-29 11:56:11.314 1521 1575 I Unity : at GooglePlay$$anonymous$$anagerSetup.OnSavedGameDataRead (SavedGameRequestStatus status, System.Byte[] data) [0x00000] in <filename unknown>:0
09-29 11:56:11.314 1521 1575 I Unity : at GooglePlayGames.Native.NativeSavedGameClient+<ToOnGameThread>c__AnonStorey67`2+<ToOnGameThread>c__AnonStorey68`2[GooglePlayGames.BasicApi.SavedGame.SavedGameRequestStatus,System.Byte[]].<>m__81 () [0x00000] in <filename unknown>:0
Thanks again!
Answer by Astrydax · Sep 30, 2016 at 11:31 PM
This video is worth a watch.
https://www.youtube.com/watch?v=k293WyNSQEE
Make sure your initialization looks something like this:
PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder().EnableSavedGames().Build();
PlayGamesPlatform.InitializeInstance(config);
PlayGamesPlatform.Activate();
take special note to call Activate() after initializing. Make sure you're not calling Activate() anywhere else. This tends to be a gotcha for a lot of people.
For parsing in the byte[] data. This block worked for me in my test script.
if(status == SavedGameRequestStatus.Success)
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream(data);
SaveTest saveTest = (SaveTest)bf.Deserialize(ms);
Instance.score = saveTest.score;
}
Further, the error you're getting: "SerializationException: serializationStream supports seeking, but its length is 0" suggests that your save is not actually writing anything.
Try the same implementation on a local filestream save and open up the resulting file in your operating system and see if the file is empty or not.
Alternatively you could be getting this error due to mismatching the saved filenames or other reasons completely. Would need to see the methods that call OnSavedGameOpened() to troubleshoot that.
Your answer
Follow this Question
Related Questions
Save multiple scriptableObjects in just one binary file 2 Answers
BinaryFormatter - saving and loading a list containing sprites. 0 Answers
is it bad practice to have multiple serialized binary files for one save game? 1 Answer
Why am I getting SerializationException: Type 'UnityEngine.MonoBehaviour' etc. 1 Answer
Bool array to binaryformatter method 1 Answer