Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gnesgaming · Sep 29, 2016 at 02:13 PM · serializationgoogle playsave databinaryformatterbytearray

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);
         }
     }
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SaurabhStudio · Sep 29, 2016 at 06:40 PM 0
Share

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

avatar image gnesgaming SaurabhStudio · Sep 29, 2016 at 06:59 PM 0
Share

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);
 }
avatar image gnesgaming SaurabhStudio · Sep 29, 2016 at 07:00 PM 0
Share

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
avatar image gnesgaming SaurabhStudio · Sep 29, 2016 at 07:00 PM 0
Share
 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!

avatar image SaurabhStudio · Sep 30, 2016 at 04:47 PM 0
Share

You should post in GiHub.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges