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 Astrydax · Sep 27, 2016 at 10:38 AM · serializationsaveloadinggoogle play gamescloud

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[]?

Comment
Add comment · Show 2
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 RyanJVR · Aug 05, 2018 at 04:26 PM 0
Share

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.

avatar image rodhakgames · May 12, 2020 at 06:47 PM 0
Share

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

0 Replies

· Add your reply
  • Sort: 

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

I cant get my Xml save script to work. I dont know how. 2 Answers

Serialized data not getting saved in iPhone 1 Answer

Save/load playerprefs 2 Answers

Saving/Load using menu C# 1 Answer

Which types are actually serializable? Is the documentation incorrect or am I? 3 Answers


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