Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using GooglePlayGames;
- using GooglePlayGames.BasicApi;
- using GooglePlayGames.BasicApi.SavedGame;
- using System;
- public class PlayGames : MonoBehaviour
- {
- public int playerScore;
- string leaderboardID = "";
- string achievementID = "";
- public static PlayGamesPlatform platform;
- private bool issaving = false;
- private string SAVE_NAME = "savegames";
- string savedString;//for saves,duh
- void Start()
- {
- if (platform == null)
- {
- PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder().Build();
- PlayGamesPlatform.InitializeInstance(config);
- PlayGamesPlatform.DebugLogEnabled = true;
- platform = PlayGamesPlatform.Activate();
- }
- Social.Active.localUser.Authenticate(success =>
- {
- if (success)
- {
- Debug.Log("Logged in successfully");
- }
- else
- {
- Debug.Log("Login Failed");
- }
- });
- UnlockAchievement();
- }
- public void AddScoreToLeaderboard()
- {
- if (Social.Active.localUser.authenticated)
- {
- Social.ReportScore(playerScore, leaderboardID, success => { });
- }
- }
- public void ShowLeaderboard()
- {
- if (Social.Active.localUser.authenticated)
- {
- platform.ShowLeaderboardUI();
- }
- }
- public void ShowAchievements()
- {
- if (Social.Active.localUser.authenticated)
- {
- platform.ShowAchievementsUI();
- }
- }
- public void UnlockAchievement()
- {
- if (Social.Active.localUser.authenticated)
- {
- Social.ReportProgress(achievementID, 100f, success => { });
- }
- }
- public void ChangeAchievementID(string newValue){
- achievementID = newValue;
- }
- public void ChangeLeaderBoardID(string newValue){
- leaderboardID = newValue;
- }
- public void SetScore(int newValue){
- playerScore = newValue;
- }
- //cloud save
- public void OpenSaveToCloud(bool saving)
- {
- // debugtext.text = "hello";
- if(Social.localUser.authenticated)
- {
- // debugtext.text = "hello2";
- issaving = saving;
- ((PlayGamesPlatform)Social.Active).SavedGame.OpenWithAutomaticConflictResolution
- (SAVE_NAME, GooglePlayGames.BasicApi.DataSource.ReadCacheOrNetwork,
- ConflictResolutionStrategy.UseLongestPlaytime, SavedGameOpen);
- }
- }
- private void SavedGameOpen(SavedGameRequestStatus status, ISavedGameMetadata meta)
- {
- if(status == SavedGameRequestStatus.Success)
- {
- // debugtext.text = "hello in save1";
- if (issaving)//if is saving is true we are saving our data to cloud
- {
- // debugtext.text = "hello in save2";
- byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes(GetDataToStoreinCloud());
- SavedGameMetadataUpdate update = new SavedGameMetadataUpdate.Builder().Build();
- ((PlayGamesPlatform)Social.Active).SavedGame.CommitUpdate(meta, update, data, SaveUpdate);
- }
- else//if is saving is false we are opening our saved data from cloud
- {
- ((PlayGamesPlatform)Social.Active).SavedGame.ReadBinaryData(meta, ReadDataFromCloud);
- }
- }
- }
- private void ReadDataFromCloud(SavedGameRequestStatus status, byte[] data)
- {
- if(status == SavedGameRequestStatus.Success)
- {
- string savedata = System.Text.ASCIIEncoding.ASCII.GetString(data);
- LoadDataFromCloudToOurGame(savedata);
- }
- }
- private void SaveUpdate(SavedGameRequestStatus status, ISavedGameMetadata meta)
- {
- //use this to debug whether the game is uploaded to cloud
- Debug.Log("successfully add data to cloud");
- }
- private string GetDataToStoreinCloud()// we seting the value that we are going to store the data in cloud
- {
- string Data = "";
- //data [0]
- Data += savedString;
- Data += "|";
- return Data;
- }
- private void LoadDataFromCloudToOurGame(string savedata)
- {
- string[] data = savedata.Split('|');
- Debug.Log(data[0].ToString());
- }
- public void SetSaveString(string newString){
- savedString = newString;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment