Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with cloud save #2568

Open
ZaczykLukasz opened this issue May 8, 2019 · 2 comments
Open

Problem with cloud save #2568

ZaczykLukasz opened this issue May 8, 2019 · 2 comments

Comments

@ZaczykLukasz
Copy link

ZaczykLukasz commented May 8, 2019

Hello

I have problem with saving data on cloud. I don't know if the problem is with sending the date or with receiving it. I only know that i open the functions with saving and loading. Below is the code that i'm using

`
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using GooglePlayGames.BasicApi.Multiplayer;
using GooglePlayGames.OurUtils;
using UnityEngine.SocialPlatforms;
using Com.Google.Android.Gms.Games;
using GooglePlayGames.BasicApi.SavedGame;
using UnityEngine.UI;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using TMPro;
using System.Text;
public class Saving : MonoBehaviour
{

void Start()
{
    Opensave(true);
    PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
    .EnableSavedGames()
    .Build();

    PlayGamesPlatform.InitializeInstance(config);
    PlayGamesPlatform.DebugLogEnabled = true;
    PlayGamesPlatform.Activate();
  
    Social.localUser.Authenticate((bool success) =>
    {
        if (!success)
        {
            SSTools.ShowMessage("Failed to login!", SSTools.Position.bottom, SSTools.Time.twoSecond);

        }
        else
        {
            OpenSave(false);
        }
    });
}



//saving
private int lvl = 1;
private int experiance = 500;
private int win = 300;
private int lose = 200;
private Boolean characterowned = true;
private int money = 100;
private Boolean selectedcharcters = false;



private string GetSaveString()
{
    string r = "";
    r += lvl.ToString();
    r += "|";
    r += experiance.ToString();
    r += "|";
    r += win.ToString();
    r += "|";
    r += lose.ToString();
    r += "|";
    r += characterowned.ToString();
    r += "|";
    r += money.ToString();
    r += "|";
    r += selectedcharcters.ToString();
    return r;


}
private int number;
private void LoadSaveString(string save)
{
    string[] data = save.Split('|');
    PlayerPrefs.SetInt("lvl", int.Parse(data[0]));
    number = int.Parse(data[1]);
    lvl = int.Parse(data[0]);
    experiance = int.Parse(data[1]);
    win = int.Parse(data[2]);
    lose = int.Parse(data[3]);
    characterowned = Boolean.Parse(data[4]);
    money = int.Parse(data[5]);
    selectedcharcters = Boolean.Parse(data[6]);
    lvl++;
    // name_text.text = "LVL: " + lvl.ToString();
    SSTools.ShowMessage("wygrane" + win, SSTools.Position.top, SSTools.Time.twoSecond);

}

//cloud saving
private bool isSaving = false;
public void OpenSave(bool saving)
{
    Debug.Log("Open Save");

    if (Social.localUser.authenticated)
    {
        isSaving = saving;

        ((PlayGamesPlatform)Social.Active).SavedGame.OpenWithAutomaticConflictResolution("Simple"
            , GooglePlayGames.BasicApi.DataSource.ReadCacheOrNetwork, ConflictResolutionStrategy.UseLongestPlaytime, SaveGameOpened);

    }
}
private void SaveGameOpened(SavedGameRequestStatus status, ISavedGameMetadata meta)
{

    if (status == SavedGameRequestStatus.Success)
    {
        if (isSaving)//writting
        {

            ///  Debug.Log("writing");
            SSTools.ShowMessage("Data saved:", SSTools.Position.top, SSTools.Time.twoSecond);
            //  name_text.text = "Dane zapisane ";
            byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes(GetSaveString());
            SavedGameMetadataUpdate update = new SavedGameMetadataUpdate.Builder().WithUpdatedDescription("Saved").Build();
            ((PlayGamesPlatform)Social.Active).SavedGame.CommitUpdate(meta, update, data, saveupdate);

            //   ((PlayGamesPlatform)Social.Active).SavedGame.ReadBinaryData(meta, SaveRead);

        }
        else//reading
        {
            // Debug.Log("saving");
            SSTools.ShowMessage("Data read" , SSTools.Position.top, SSTools.Time.twoSecond);
            ((PlayGamesPlatform)Social.Active).SavedGame.ReadBinaryData(meta, SaveRead);
        }
    }
}

//load
private void SaveRead(SavedGameRequestStatus status, byte[] data)
{
    if (status == SavedGameRequestStatus.Success)
    {
      

        string saveData = System.Text.ASCIIEncoding.ASCII.GetString(data);
        LoadSaveString(saveData);
   
    }
}
//success save
private void saveupdate(SavedGameRequestStatus status, ISavedGameMetadata meta)
{
    Debug.Log(status);

}


}

`

@Velorth
Copy link

Velorth commented May 30, 2019

I suppose that there is a problem with com.google.android.gms.play-services-games-17.0.0 library.

I tried to manually replace it with v16.0.0 and it worked.

@ZaczykLukasz
Copy link
Author

Thanks for answer and help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants