135 lines
3.6 KiB
C#
135 lines
3.6 KiB
C#
using UnityEngine;
|
|
using GooglePlayGames;
|
|
using GooglePlayGames.BasicApi;
|
|
using GooglePlayGames.BasicApi.SavedGame;
|
|
using System;
|
|
using TMPro;
|
|
|
|
|
|
public class PlayGamesManager_old : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
TMP_Text nicknameText;
|
|
|
|
private score score;
|
|
|
|
[SerializeField]
|
|
private shopManager shopManager;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
this.score = GetComponent<score>();
|
|
|
|
SignIn();
|
|
|
|
}
|
|
|
|
public void SignIn()
|
|
{
|
|
PlayGamesPlatform.Activate();
|
|
PlayGamesPlatform.Instance.Authenticate(ProcessAuthentication);
|
|
|
|
OpenSave(false);
|
|
}
|
|
|
|
internal void ProcessAuthentication(SignInStatus status)
|
|
{
|
|
if (status == SignInStatus.Success)
|
|
{
|
|
// Continue with Play Games Services
|
|
|
|
string name = PlayGamesPlatform.Instance.GetUserDisplayName();
|
|
|
|
nicknameText.text = name;
|
|
}
|
|
else
|
|
{
|
|
// Disable your integration with Play Games Services or show a login button
|
|
// to ask users to sign-in. Clicking it should call
|
|
// PlayGamesPlatform.Instance.ManuallyAuthenticate(ProcessAuthentication).
|
|
}
|
|
}
|
|
|
|
#region SavedGames
|
|
|
|
private bool isSaving;
|
|
|
|
public void OpenSave(bool saving)
|
|
{
|
|
if(Social.localUser.authenticated)
|
|
{
|
|
isSaving = saving;
|
|
((PlayGamesPlatform)Social.Active).SavedGame.OpenWithAutomaticConflictResolution("Save", DataSource.ReadCacheOrNetwork, ConflictResolutionStrategy.UseLongestPlaytime, SavedGameOpen);
|
|
}
|
|
}
|
|
|
|
private void SavedGameOpen(SavedGameRequestStatus status, ISavedGameMetadata metadata)
|
|
{
|
|
if(status == SavedGameRequestStatus.Success)
|
|
{
|
|
if(isSaving) // Saving
|
|
{
|
|
byte[] myData = System.Text.ASCIIEncoding.ASCII.GetBytes(GetSaveString());
|
|
|
|
SavedGameMetadataUpdate updateForMetadata = new SavedGameMetadataUpdate.Builder().WithUpdatedDescription("I have played my game at: " + DateTime.Now.ToString()).Build();
|
|
|
|
((PlayGamesPlatform)Social.Active).SavedGame.CommitUpdate(metadata, updateForMetadata, myData, SaveCallback);
|
|
}
|
|
else // Loading
|
|
{
|
|
((PlayGamesPlatform)Social.Active).SavedGame.ReadBinaryData(metadata, LoadCallBack);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void LoadCallBack(SavedGameRequestStatus status, byte[] data)
|
|
{
|
|
if(status == SavedGameRequestStatus.Success)
|
|
{
|
|
string loadedData = System.Text.ASCIIEncoding.ASCII.GetString(data);
|
|
|
|
LoadSavedString(loadedData);
|
|
}
|
|
}
|
|
|
|
private void LoadSavedString(string loadedData)
|
|
{
|
|
string[] cloudStringArr = loadedData.Split('|');
|
|
|
|
int highscore = int.Parse(cloudStringArr[0]);
|
|
|
|
int coins = int.Parse(cloudStringArr[1]);
|
|
|
|
string ownedPlanes = cloudStringArr[2];
|
|
|
|
score.SetHighscore(highscore);
|
|
|
|
shopManager.coins = coins;
|
|
|
|
shopManager.ConvertOwnedPlanes(ownedPlanes);
|
|
}
|
|
|
|
private string GetSaveString()
|
|
{
|
|
string dataToSave = "";
|
|
|
|
dataToSave += ((int)score.highscoreAmount).ToString() + "|" + shopManager.coins.ToString() + "|" + shopManager.GetOwnedPlanesString();
|
|
|
|
return dataToSave;
|
|
}
|
|
|
|
private void SaveCallback(SavedGameRequestStatus status, ISavedGameMetadata metadata)
|
|
{
|
|
if(status == SavedGameRequestStatus.Success)
|
|
{
|
|
Debug.Log("Successfully saved to the cloud");
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("Failed to save to cloud");
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
} |