Google Cloud Save (Not saving)
Hey everyone,
I followed the youtube tutorial by N3K for google cloud save. I included a pop-up manager also to debug. My issue right now is, that when I press a button for "CloudSave", nothing happens. Not even a pop-up. I can successfully save and load locally.
My other script contains the necesseities for google play services. I can successfully login, look at achievemens and leaderboards.
Saving Games in Enabled in Google Dev Console.
This is my code;
using UnityEngine;
using GooglePlayGames;
using GooglePlayGames.BasicApi.SavedGame;
using GooglePlayGames.BasicApi;
using System;
public class PlayerManager : MonoBehaviour {
public static PlayerManager Instance { set; get; }
public int[] Currencies { set; get; }
public int[] Weapons { set; get; }
private bool isSaving;
private bool hasBeenWarnedLocalSave;
public bool SaveLoaded { set; get;}
public float LastSave { set; get;}
public float PlaytimeSinceSave { set; get;}
public float TotalPlayTime {get { return (Time.time - LastSave) + PlaytimeSinceSave; }}
// Use this for initialization
void Awake () {
PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
.EnableSavedGames()
.Build();
PlayGamesPlatform.InitializeInstance(config);
Instance = this;
DontDestroyOnLoad(gameObject);
// Create all my arrays
Currencies = new int[Enum.GetNames(typeof(MoneyManager.Currency)).Length];
Weapons = new int[Enum.GetNames(typeof(WeaponManager.Ammunition)).Length];
LoadCloud();
}
void Start()
{
Debug.Log("Total Play Time : " + TotalPlayTime);
}
// Update is called once per frame
void Update () {
}
public void LoadLocal()
{
/*
Currencies = new int[Enum.GetNames(typeof(MoneyManager.Currency)).Length];
Currencies[(int)MoneyManager.Currency.Platinum] = PlayerPrefs.GetInt("CurrencyPlatinum");
Currencies[(int)MoneyManager.Currency.Stardust] = PlayerPrefs.GetInt("CurrencyStardust");
Weapons = new int[Enum.GetNames(typeof(WeaponManager.Ammunition)).Length];
Weapons[(int)WeaponManager.Ammunition.HomingMissiles] = PlayerPrefs.GetInt("AmmunitionHomingMissiles");
Weapons[(int)WeaponManager.Ammunition.Rockets] = PlayerPrefs.GetInt("AmmunitionRockets");
*/
LoadFromString(PlayerPrefs.GetString("SaveString"));
}
public void LoadCloud()
{
isSaving = false;
((PlayGamesPlatform)Social.Active).SavedGame.OpenWithAutomaticConflictResolution
("CTPSave", DataSource.ReadCacheOrNetwork, ConflictResolutionStrategy.UseLongestPlaytime, SavedGameOpened);
}
public void SaveLocal()
{
/*
// Money
PlayerPrefs.SetInt("CurrencyPlatinum", Currencies[(int)MoneyManager.Currency.Platinum]);
PlayerPrefs.SetInt("CurrencyStardust", Currencies[(int)MoneyManager.Currency.Stardust]);
// Ammunition
PlayerPrefs.SetInt("AmmunitionHomingMissiles", Weapons[(int)WeaponManager.Ammunition.HomingMissiles]);
PlayerPrefs.SetInt("AmmunitionRockets", Weapons[(int)WeaponManager.Ammunition.Rockets]);
*/
PlayerPrefs.SetString("SaveString",GetSaveString());
PopupManager.Instance.ShowPopup("Saved", "We just saved this shit");
}
public void SaveCloud()
{
if (Social.localUser.authenticated)
{
isSaving = true;
hasBeenWarnedLocalSave = false;
((PlayGamesPlatform)Social.Active).SavedGame.OpenWithAutomaticConflictResolution
("CTPSave", DataSource.ReadCacheOrNetwork, ConflictResolutionStrategy.UseLongestPlaytime, SavedGameOpened);
PopupManager.Instance.ShowPopup("Saved", "Saved to Google Cloud");
}
else
{
if (!hasBeenWarnedLocalSave)
{
PopupManager.Instance.ShowPopup("Save Cloud Error", "Not Connected");
hasBeenWarnedLocalSave = true;
SaveLocal();
}
}
PopupManager.Instance.ShowPopup("Button Pressed", "Nothing happened.");
}
public void SavedGameOpened(SavedGameRequestStatus status, ISavedGameMetadata game)
{
if (status == SavedGameRequestStatus.Success)
{
if(isSaving) // Writing Data
{
byte[] data = System.Text.Encoding.ASCII.GetBytes(GetSaveString());
TimeSpan playedTime = TimeSpan.FromSeconds(TotalPlayTime);
SavedGameMetadataUpdate.Builder builder = new SavedGameMetadataUpdate.Builder();
SavedGameMetadataUpdate update = builder.Build();
((PlayGamesPlatform)Social.Active).SavedGame.CommitUpdate(game, update, data, SavedGameWritten);
}
else // Reading Data
{
((PlayGamesPlatform)Social.Active).SavedGame.ReadBinaryData(game, SavedGameLoaded);
}
}
else
{
PopupManager.Instance.ShowPopup("Save State", "Error" + status);
}
}
public void SavedGameLoaded(SavedGameRequestStatus status, byte[] data)
{
if(status == SavedGameRequestStatus.Success)
{
LoadFromString(System.Text.ASCIIEncoding.ASCII.GetString(data));
}
else
{
Debug.Log("Error reading game:" + status);
PopupManager.Instance.ShowPopup("Error", "Error reading game :" + status);
}
}
public void SavedGameWritten(SavedGameRequestStatus status, ISavedGameMetadata game)
{
if (status == SavedGameRequestStatus.Success)
{
// handle reading or writing of saved game.
}
else
{
// handle error
}
}
private void LoadFromString (string savedData)
{
if (savedData == "")
return;
string[] data = savedData.Split('|');
// Time
string[] misc = data[0].Split('%');
PlaytimeSinceSave = float.Parse(misc[0]);
// Currency
string[] currency = data[1].Split('%');
Currencies[(int)MoneyManager.Currency.Platinum] = int.Parse(currency[0]);
Currencies[(int)MoneyManager.Currency.Stardust] = int.Parse(currency[1]);
// Ammunition
string[] ammo = data[2].Split('%');
Weapons[(int)WeaponManager.Ammunition.HomingMissiles] = int.Parse(ammo[0]);
Weapons[(int)WeaponManager.Ammunition.Rockets] = int.Parse(ammo[1]);
SaveLoaded = true;
}
private string GetSaveString()
{
string saveData = "";
// Time
saveData += TotalPlayTime.ToString() + '|';
LastSave = Time.time;
// Money
saveData +=
Currencies[(int)MoneyManager.Currency.Platinum].ToString() + '%' +
Currencies[(int)MoneyManager.Currency.Stardust].ToString() + '|';
// Ammunition
saveData +=
Weapons[(int)WeaponManager.Ammunition.HomingMissiles].ToString() + '%' +
Weapons[(int)WeaponManager.Ammunition.Rockets].ToString() + '|';
return saveData;
}
private void OnApplicationQuit()
{
Debug.Log("Yo we just saved this shit");
}
}
I moved around some code to ensure that I have proper order.
Now I know that my "save button" is working, but the important code doesn't run.
public void SaveCloud()
{
if (Social.localUser.authenticated)
{
isSaving = true;
hasBeenWarnedLocalSave = false;
((PlayGamesPlatform)Social.Active).SavedGame.OpenWithAutomaticConflictResolution
("CTPSave", DataSource.ReadCacheOrNetwork, ConflictResolutionStrategy.UseLongestPlaytime, SavedGameOpened);
Popup$$anonymous$$anager.Instance.ShowPopup("Saved", "Saved to Google Cloud");
}
else
{
if (!hasBeenWarnedLocalSave)
{
Popup$$anonymous$$anager.Instance.ShowPopup("Save Cloud Error", "Not Connected");
hasBeenWarnedLocalSave = true;
SaveLocal();
}
}
Popup$$anonymous$$anager.Instance.ShowPopup("Button Pressed", "Nothing happened.");
}
I just get the "Button Pressed, Nothing Happened" message.
Answer by AEssid · Aug 03, 2017 at 11:19 PM
me too same problem .. Have you found a solution to this problem?
Hello, you can use this asset.Hope your problem solved. https://www.assetstore.unity3d.com/en/#!/content/95371
Your answer
Follow this Question
Related Questions
How to import data from serialized file to google play service cloud save? 0 Answers
The best overloaded method match for google play games 1 Answer
Problem With Unity Cloud Save (Google Play Services) 0 Answers
Android game freezes after Timed out connecting to Google Play 0 Answers
I can't get Google cloud saving working 0 Answers