Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question How to save Json file on Google Game Services

Discussion in 'Scripting' started by ShovelingLife, Aug 21, 2020.

  1. ShovelingLife

    ShovelingLife

    Joined:
    Feb 4, 2020
    Posts:
    31
    Hello community I am trying to save on the cloud what could be the best way to save a Json file in the cloud?
    The Json file code that I am wishing to save

    Code (CSharp):
    1.  void Save_player_data()
    2.     {
    3.         player_data = new Player_data()
    4.         {
    5.             gold = DataController.instance.gold,
    6.             cash = DataController.instance.cash,
    7.             boost_value = TimeManager.instance.current_boostValue_gameplay,
    8.             ad_info = in_app_manager.banner_ad
    9.         };
    10.         string json = JsonUtility.ToJson(player_data,true);
    11.         File.WriteAllText(Application.persistentDataPath + "/player_data.json", json);
    12.     }
    13.  
    14.  
    15. public void Load_player_data()
    16.     {
    17.         string save_string = File.ReadAllText(Application.persistentDataPath + "/player_data.json");
    18.         player_data = JsonUtility.FromJson<Player_data>(save_string);
    19.         if (File.Exists(Application.persistentDataPath + "/player_data.json"))
    20.         {
    21.             DataController.instance.gold = player_data.gold;
    22.             DataController.instance.cash = player_data.cash;
    23.             TimeManager.instance.current_boostValue_gameplay = player_data.boost_value;
    24.             in_app_manager.banner_ad = player_data.ad_info;
    25.         }
    26.         else return;
    27.     }
    28.  

    And the class which I am wishing to use

    Code (CSharp):
    1.  
    2.     public void LoadFromCloud(Action<string> afterLoadAction)
    3.     {
    4.         if (isAuthenticated && !isProcessing)
    5.         {
    6.             StartCoroutine(LoadFromCloudRoutin(afterLoadAction));
    7.         }
    8.         else
    9.         {
    10.             Login();
    11.         }
    12.     }

    Code (CSharp):
    1. public void SaveToCloud(string dataToSave)
    2.     {
    3.  
    4.         if (isAuthenticated)
    5.         {
    6.             loadedData = dataToSave;
    7.             isProcessing = true;
    8.             ((PlayGamesPlatform)Social.Active).SavedGame.OpenWithAutomaticConflictResolution(m_saveFileName, DataSource.ReadCacheOrNetwork, ConflictResolutionStrategy.UseLongestPlaytime, OnFileOpenToSave);
    9.         }
    10.         else
    11.         {
    12.             Login();
    13.         }
    14.     }
     
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,749
  3. ShovelingLife

    ShovelingLife

    Joined:
    Feb 4, 2020
    Posts:
    31
    Hello what should be the way to save Json file there?
     
  4. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,749
    In the example, the saveString field would contain the data that is to be saved. On line 88 the ToBytes() function is called, which converts the contents of saveString to a byte array, and then passes that to the cloud save function.

    You just need to pass in your Json string instead.
     
  5. ShovelingLife

    ShovelingLife

    Joined:
    Feb 4, 2020
    Posts:
    31
    the LoadFromCloud method and SaveToCloud method? But the value passing to LoadFromCloud method is action<string>
     
  6. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,749
    The LoadFromCloud() method in the example I linked doesn't take any parameters.

    Have you incorporated the linked example into your own app so you can see how the it works? Once you do that you should be able to adapt it to your own needs.
     
  7. ShovelingLife

    ShovelingLife

    Joined:
    Feb 4, 2020
    Posts:
    31
    Sorry I looked for another code, so basically if I want to save I need to save my Json Files which are six of them as a string array, convert to a byte array to save and to load convert a bite array to string array then replace it with the save file that exists?
     
  8. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,749
    Yes that's pretty much it.
     
    ShovelingLife likes this.
  9. ShovelingLife

    ShovelingLife

    Joined:
    Feb 4, 2020
    Posts:
    31
    thank you very much for your help I wanna work it now good luck
     
    Munchy2007 likes this.