Skip to content

Instantly share code, notes, and snippets.

@arif-pandu
Created November 9, 2023 17:05
Show Gist options
  • Save arif-pandu/258d5a94289e433ee3c5e439f9866fe4 to your computer and use it in GitHub Desktop.
Save arif-pandu/258d5a94289e433ee3c5e439f9866fe4 to your computer and use it in GitHub Desktop.
using System;
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using GooglePlayGames.BasicApi.SavedGame;
using UnityEngine;
public class GooglePlayUserDataManager : MonoBehaviour
{
[SerializeField] public UITest theUI;
private bool isSaving;
private string filename = "FileNameData";
#region SignIn
public void SignInToGPGS()
{
PlayGamesPlatform.Activate();
PlayGamesPlatform.Instance.Authenticate((SignInStatus result) =>
{
theUI.statusText.text = "Authenticating...";
if (result == SignInStatus.Success)
{
theUI.statusText.text = "SUCCESS LOGGED IN AS : " + PlayGamesPlatform.Instance.GetUserDisplayName();
}
else
{
theUI.statusText.text = "FAILED LOGIN";
}
});
}
#endregion
#region Opening Saved Game
public void OpenSavedGame(bool saving)
{
if (Social.localUser.authenticated)
{
isSaving = saving;
ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
savedGameClient.OpenWithAutomaticConflictResolution(filename, DataSource.ReadCacheOrNetwork, ConflictResolutionStrategy.UseLongestPlaytime, SavedGameOpened);
}
}
public void SavedGameOpened(SavedGameRequestStatus status, ISavedGameMetadata meta)
{
if (status == SavedGameRequestStatus.Success)
{
if (isSaving) // Are Saving
{
// Convert data to byte
byte[] myData = System.Text.ASCIIEncoding.ASCII.GetBytes(GetSaveString());
// update our metadata
SavedGameMetadataUpdate updateForMetadata = new SavedGameMetadataUpdate.Builder().WithUpdatedDescription("Updated at" + DateTime.Now.ToString()).Build();
// commit
((PlayGamesPlatform)Social.Active).SavedGame.CommitUpdate(meta, updateForMetadata, myData, SaveCallBack);
}
else // Are Loading
{
((PlayGamesPlatform)Social.Active).SavedGame.ReadBinaryData(meta, LoadCallBack);
}
}
}
public string GetSaveString()
{
string dataToSave = "";
// CONVERT LOCAL DATA TO CLOUD HERE
dataToSave = "INI DATA YANG DISIMPEN";
return dataToSave;
}
private void SaveCallBack(SavedGameRequestStatus status, ISavedGameMetadata meta)
{
if (status == SavedGameRequestStatus.Success)
{
theUI.logText.text = "Success Save Data to Cloud";
}
else
{
theUI.logText.text = "Failed to Save to Cloud";
}
}
private void LoadCallBack(SavedGameRequestStatus status, byte[] data)
{
if (status == SavedGameRequestStatus.Success)
{
string loadedData = System.Text.ASCIIEncoding.ASCII.GetString(data);
// PARSING CLOUD DATA TO LOCAL HERE
theUI.valueText.text = loadedData;
}
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment