- Home /
Question by
JulySellanes · Jan 13, 2019 at 06:08 PM ·
errorplayerprefssave datagoogle play gamessaveload
Google play services saved games highscore error
Hello, someone who knows how to save the score in the cloud with google play services, can see my code and tell me what is wrong, because I do not know why but it does not work
I have "Saved Games" activated in google play developer
My code:` public static PlayGamesScript Instance { get; private set; }
public int diamonds;
// Use this for initialization
void Start()
{
Instance = this;
GooglePlayGames.BasicApi.PlayGamesClientConfiguration config =
new GooglePlayGames.BasicApi.PlayGamesClientConfiguration.Builder().EnableSavedGames().Build();
PlayGamesPlatform.InitializeInstance (config);
PlayGamesPlatform.Activate();
SignIn();
}
public void SignIn()
{
Social.localUser.Authenticate(success =>
{
OpenSave(false);
});
}
public void SignOut ()
{
PlayGamesPlatform.Instance.SignOut ();
}
#region Saved Games
private string GetSaveString ()
{
string r = "";
r += PlayerPrefs.GetInt ("Highscore").ToString ();
r += "|";
r += diamonds.ToString ();
return r;
}
private void LoadSaveString(string save)
{
string[] data = save.Split('|');
PlayerPrefs.SetInt ("Highscore", int.Parse (data[0]));
diamonds = int.Parse(data[1]);
}
private bool isSaving = false;
public void OpenSave (bool saving)
{
if (Social.localUser.authenticated)
{
isSaving = saving;
((PlayGamesPlatform)Social.Active).SavedGame.OpenWithAutomaticConflictResolution("SaveCloudTest",
GooglePlayGames.BasicApi.DataSource.ReadCacheOrNetwork,
ConflictResolutionStrategy.UseLongestPlaytime, SaveGameOpened);
}
}
private void SaveGameOpened (SavedGameRequestStatus status, ISavedGameMetadata meta)
{
if (status == SavedGameRequestStatus.Success)
{
if (isSaving)
{
byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes (GetSaveString());
SavedGameMetadataUpdate update = new SavedGameMetadataUpdate.Builder().WithUpdatedDescription ("Saved at " + DateTime.Now.ToString ()).Build ();
((PlayGamesPlatform)Social.Active).SavedGame.CommitUpdate(meta, update, data, SaveUpdate);
}
else
{
((PlayGamesPlatform)Social.Active).SavedGame.ReadBinaryData(meta, SaveRead);
}
}
}
private void SaveRead (SavedGameRequestStatus status, byte[] data)
{
if (status == SavedGameRequestStatus.Success)
{
string saveData = System.Text.ASCIIEncoding.ASCII.GetString(data);
LoadSaveString(saveData);
}
}
private void SaveUpdate (SavedGameRequestStatus status, ISavedGameMetadata meta)
{
}`
Comment
When the game ends, if the score is higher than the highscore, I use PlayGamesScript.Instance.OpenSave (true)