play-games-plugin-for-unity
play-games-plugin-for-unity copied to clipboard
Cloud Saving
Hello, I'm new to GitHub so I'm not even sure I'm suppose to ask questions here like this. Everything else I can read it and include in pretty quickly and easily but the Cloud saving is giving me a lot of problems.
I just don't understand how to use the cloud saving.
`void OpenSavedGame(string filename) {
ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
savedGameClient.OpenWithAutomaticConflictResolution(filename, DataSource.ReadCacheOrNetwork,
ConflictResolutionStrategy.UseLongestPlaytime, OnSavedGameOpened);
}
public void OnSavedGameOpened(SavedGameRequestStatus status, ISavedGameMetadata game) {
if (status == SavedGameRequestStatus.Success) {
// handle reading or writing of saved game.
} else {
// handle error
}
}`
This look pretty simple. But how do I handle reading or writing of saved game? What would I even put in there? I did come up with another script but with the help of an old YouTube video. It works but will get very complex and long even with a simple app.
Can anyone give any help with this.
Hope this help, it was my old script (I remain code only for cloud save part).
Try to edit in your own way.
#if UNITY_ANDROID
using UnityEngine;
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using GooglePlayGames.BasicApi.SavedGame;
using System.Text;
public class GPG_CloudSaveSystem
{
const string SAVE_FILES_NAME = "xxxxxx";
static GPG_CloudSaveSystem _instance;
public static GPG_CloudSaveSystem Instance
{
get
{
if (_instance == null)
_instance = new GPG_CloudSaveSystem();
return _instance;
}
}
bool _isSaving;
bool _isLoading;
// Methods
public void LoadFromCloud()
{
_isLoading = true;
((PlayGamesPlatform)Social.Active).SavedGame.OpenWithAutomaticConflictResolution(
SAVE_FILES_NAME
, DataSource.ReadCacheOrNetwork
, ConflictResolutionStrategy.UseLongestPlaytime
, LoadCallback);
}
void LoadCallback(SavedGameRequestStatus status, ISavedGameMetadata game)
{
if (status == SavedGameRequestStatus.Success)
((PlayGamesPlatform)Social.Active).SavedGame.ReadBinaryData(game, LoadCallback2);
else
{
_isLoading = false;
}
}
void LoadCallback2(SavedGameRequestStatus status, byte[] data)
{
if (status == SavedGameRequestStatus.Success)
LoadCallback3(data);
else
{
_isLoading = false;
}
}
void LoadCallback3(byte[] cloudData)
{
_isLoading = false;
if (cloudData == null)
return;
LoadCallback4(FromBytes(cloudData));
}
void LoadCallback4(string other)
{
}
public void SaveToCloud()
{
if (_isLoading || _isSaving)
return;
if (Authenticated)
{
if (string.IsNullOrEmpty(GameController.lastSaveString)
|| (!string.IsNullOrEmpty(GameController.lastSaveString) && (GameController.lastSaveString != JsonUtility.ToJson(xxxxxxxxxData))))
{
_isSaving = true;
((PlayGamesPlatform)Social.Active).SavedGame.OpenWithAutomaticConflictResolution(
SAVE_FILES_NAME,
DataSource.ReadCacheOrNetwork,
ConflictResolutionStrategy.UseLongestPlaytime,
SaveCallback);
}
}
}
void SaveCallback(SavedGameRequestStatus status, ISavedGameMetadata game)
{
if (status == SavedGameRequestStatus.Success)
{
SavedGameMetadataUpdate.Builder builder = new SavedGameMetadataUpdate.Builder();
SavedGameMetadataUpdate updatedMetadata = builder.Build();
((PlayGamesPlatform)Social.Active).SavedGame.CommitUpdate(game, updatedMetadata, ToBytes(), SaveCallback2);
}
else
_isSaving = false;
}
void SaveCallback2(SavedGameRequestStatus status, ISavedGameMetadata game)
{
_isSaving = false;
if (status == SavedGameRequestStatus.Success)
{
}
}
byte[] ToBytes()
{
return Encoding.UTF8.GetBytes(JsonUtility.ToJson(xxxxxxxData));
}
string FromBytes(byte[] bytes)
{
return Encoding.UTF8.GetString(bytes);
}
bool Authenticated
{
get { return Social.Active.localUser.authenticated; }
}
}
#endif
Thank you very much for taking the time. I'll take a look and give it a try.
Thank you, again.
On Sun, 8 May 2022, 14:41 Kanin Temsrisuk, @.***> wrote:
Hope this help, it was my old script (I remain code only for cloud save part).
Try to edit in your own way.
#if UNITY_ANDROID using UnityEngine; using GooglePlayGames; using GooglePlayGames.BasicApi; using GooglePlayGames.BasicApi.SavedGame; using System.Text;
public class GPG_CloudSaveSystem { const string SAVE_FILES_NAME = "xxxxxx";
static GPG_CloudSaveSystem _instance; public static GPG_CloudSaveSystem Instance { get { if (_instance == null) _instance = new GPG_CloudSaveSystem(); return _instance; } } bool _isSaving; bool _isLoading; // Methods public void LoadFromCloud() { _isLoading = true; ((PlayGamesPlatform)Social.Active).SavedGame.OpenWithAutomaticConflictResolution( SAVE_FILES_NAME , DataSource.ReadCacheOrNetwork , ConflictResolutionStrategy.UseLongestPlaytime , LoadCallback); } void LoadCallback(SavedGameRequestStatus status, ISavedGameMetadata game) { if (status == SavedGameRequestStatus.Success) ((PlayGamesPlatform)Social.Active).SavedGame.ReadBinaryData(game, LoadCallback2); else { _isLoading = false; } } void LoadCallback2(SavedGameRequestStatus status, byte[] data) { if (status == SavedGameRequestStatus.Success) LoadCallback3(data); else { _isLoading = false; } } void LoadCallback3(byte[] cloudData) { _isLoading = false; if (cloudData == null) return; LoadCallback4(FromBytes(cloudData)); } void LoadCallback4(string other) { } public void SaveToCloud() { if (_isLoading || _isSaving) return; if (Authenticated) { if (string.IsNullOrEmpty(GameController.lastSaveString) || (!string.IsNullOrEmpty(GameController.lastSaveString) && (GameController.lastSaveString != JsonUtility.ToJson(GameController.Instance.playerDataVO)))) { _isSaving = true; ((PlayGamesPlatform)Social.Active).SavedGame.OpenWithAutomaticConflictResolution( SAVE_FILES_NAME, DataSource.ReadCacheOrNetwork, ConflictResolutionStrategy.UseLongestPlaytime, SaveCallback); } } } void SaveCallback(SavedGameRequestStatus status, ISavedGameMetadata game) { if (status == SavedGameRequestStatus.Success) { SavedGameMetadataUpdate.Builder builder = new SavedGameMetadataUpdate.Builder(); SavedGameMetadataUpdate updatedMetadata = builder.Build(); ((PlayGamesPlatform)Social.Active).SavedGame.CommitUpdate(game, updatedMetadata, ToBytes(), SaveCallback2); } else _isSaving = false; } void SaveCallback2(SavedGameRequestStatus status, ISavedGameMetadata game) { _isSaving = false; if (status == SavedGameRequestStatus.Success)
{ }
} byte[] ToBytes() { byte[] bytes = Encoding.UTF8.GetBytes(JsonUtility.ToJson(GameController.Instance.playerDataVO)); return bytes; } string FromBytes(byte[] bytes) { return Encoding.UTF8.GetString(bytes); } bool Authenticated { get { return Social.Active.localUser.authenticated; } }
} #endif
— Reply to this email directly, view it on GitHub https://github.com/playgameservices/play-games-plugin-for-unity/issues/3135#issuecomment-1120421352, or unsubscribe https://github.com/notifications/unsubscribe-auth/AYOOZDVVJHPDNWDC53DGXO3VI7ABPANCNFSM5SNOUKMQ . You are receiving this because you authored the thread.Message ID: <playgameservices/play-games-plugin-for-unity/issues/3135/1120421352@ github.com>