• 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
Question by conguerror · Feb 12, 2022 at 05:47 PM · savegoogle playsave datagoogle play gamesgoogle

Google play save data

Hello,

I am new into google API and I want to implement a save system in a google play services but I don't know how to use it practically. I got a script that saves and loads a string from a cloud but I want to get variables just like in PlayerPrefs so I can load items and the amounts of items in the beginning of the game. Shortly: how do I get load and save data to cloud and access them like in PlayerPrefs? So maybe I can create a function like this:

   void GetData(){
        healthPoints = cloudSave.GetHealthPointsFromCloud();//
     //how can I use google play save cloud to do something like this? 
     }


My whole script just saves data in a singlestring which is honestly is useless to me in this state.

 using UnityEngine;
 using GooglePlayGames;
 using GooglePlayGames.BasicApi;
 using GooglePlayGames.BasicApi.SavedGame;
 using System;
 public class PlayGames : MonoBehaviour
 {
     public int playerScore;
     string leaderboardID = "";
     string achievementID = "";
     public static PlayGamesPlatform platform;
     private bool issaving = false;
     private string SAVE_NAME = "savegames";
     string savedString;//for saves,duh
 
 
     void Start()
     {
         if (platform == null)
         {
             PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder().Build();
             PlayGamesPlatform.InitializeInstance(config);
             PlayGamesPlatform.DebugLogEnabled = true;
             platform = PlayGamesPlatform.Activate();
         }
 
         Social.Active.localUser.Authenticate(success =>
         {
             if (success)
             {
                 Debug.Log("Logged in successfully");
             }
             else
             {
                 Debug.Log("Login Failed");
             }
         });
         UnlockAchievement();
     }
 
     public void AddScoreToLeaderboard()
     {
         if (Social.Active.localUser.authenticated)
         {
             Social.ReportScore(playerScore, leaderboardID, success => { });
         }
     }
 
     public void ShowLeaderboard()
     {
         if (Social.Active.localUser.authenticated)
         {
             platform.ShowLeaderboardUI();
         }
     }
 
     public void ShowAchievements()
     {
         if (Social.Active.localUser.authenticated)
         {
             platform.ShowAchievementsUI();
         }
     }
 
     public void UnlockAchievement()
     {
         if (Social.Active.localUser.authenticated)
         {
             Social.ReportProgress(achievementID, 100f, success => { });
         }
     }
     public void ChangeAchievementID(string newValue){
         achievementID = newValue;
     }
     public void ChangeLeaderBoardID(string newValue){
         leaderboardID = newValue;
     }
     public void SetScore(int newValue){
         playerScore = newValue;
     }
     //cloud save
      public void OpenSaveToCloud(bool saving)
     {
        // debugtext.text = "hello";
         if(Social.localUser.authenticated)
         {
           //  debugtext.text = "hello2";
             issaving = saving;
             ((PlayGamesPlatform)Social.Active).SavedGame.OpenWithAutomaticConflictResolution
                 (SAVE_NAME, GooglePlayGames.BasicApi.DataSource.ReadCacheOrNetwork,
                 ConflictResolutionStrategy.UseLongestPlaytime, SavedGameOpen);
 
         
     }
     }
 
     private void SavedGameOpen(SavedGameRequestStatus status, ISavedGameMetadata meta)
     {
         if(status == SavedGameRequestStatus.Success)
         {
            // debugtext.text = "hello in save1";
             if (issaving)//if is saving is true we are saving our data to cloud
             {
                // debugtext.text = "hello in save2";
                 byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes(GetDataToStoreinCloud());
                 SavedGameMetadataUpdate update = new SavedGameMetadataUpdate.Builder().Build();
                 ((PlayGamesPlatform)Social.Active).SavedGame.CommitUpdate(meta, update, data, SaveUpdate);
             }
             else//if is saving is false we are opening our saved data from cloud
             {
                 ((PlayGamesPlatform)Social.Active).SavedGame.ReadBinaryData(meta, ReadDataFromCloud);
             }
         }
     }
 
     private void ReadDataFromCloud(SavedGameRequestStatus status, byte[] data)
     {
         if(status == SavedGameRequestStatus.Success)
         {
             string savedata = System.Text.ASCIIEncoding.ASCII.GetString(data);
             LoadDataFromCloudToOurGame(savedata);
         }
     }
     private void SaveUpdate(SavedGameRequestStatus status, ISavedGameMetadata meta)
     {
         //use this to debug whether the game is uploaded to cloud
         Debug.Log("successfully add data to cloud");
     }
     private string GetDataToStoreinCloud()//  we seting the value that we are going to store the data in cloud
     {
         string Data = "";
     //data [0]
         Data += savedString;
         Data += "|";
         return Data;
     }
     private void LoadDataFromCloudToOurGame(string savedata)
     {
         string[] data = savedata.Split('|');
         Debug.Log(data[0].ToString());
     }
     public void SetSaveString(string newString){
         savedString = newString;
     }
     
 }
 

Comment

People who like this

0 Show 1
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 conguerror · Feb 12, 2022 at 06:08 PM 0
Share

I need to do it because I need to structure my data properly

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by conguerror · Feb 12, 2022 at 07:36 PM

Watch this: https://www.youtube.com/watch?v=3nUBghKbfEw&ab_channel=ResoCoder

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

145 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Google play save doesn't work 1 Answer

Issue with Google Play Games and PlayGamesPlatform 0 Answers

What's the difference between PlayGamesPlatform & Social.localUser? 1 Answer

how to keep player data after update? 0 Answers

Destroy Banner/Interstitial ads of AdMob? 0 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