Guest User

PlayGames

a guest
Feb 12th, 2022
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.63 KB | None | 0 0
  1.  using UnityEngine;
  2.  using GooglePlayGames;
  3.  using GooglePlayGames.BasicApi;
  4.  using GooglePlayGames.BasicApi.SavedGame;
  5.  using System;
  6.  public class PlayGames : MonoBehaviour
  7.  {
  8.      public int playerScore;
  9.      string leaderboardID = "";
  10.      string achievementID = "";
  11.      public static PlayGamesPlatform platform;
  12.      private bool issaving = false;
  13.      private string SAVE_NAME = "savegames";
  14.      string savedString;//for saves,duh
  15.  
  16.  
  17.      void Start()
  18.      {
  19.          if (platform == null)
  20.          {
  21.              PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder().Build();
  22.              PlayGamesPlatform.InitializeInstance(config);
  23.              PlayGamesPlatform.DebugLogEnabled = true;
  24.              platform = PlayGamesPlatform.Activate();
  25.          }
  26.  
  27.          Social.Active.localUser.Authenticate(success =>
  28.          {
  29.              if (success)
  30.              {
  31.                  Debug.Log("Logged in successfully");
  32.              }
  33.              else
  34.              {
  35.                  Debug.Log("Login Failed");
  36.              }
  37.          });
  38.          UnlockAchievement();
  39.      }
  40.  
  41.      public void AddScoreToLeaderboard()
  42.      {
  43.          if (Social.Active.localUser.authenticated)
  44.          {
  45.              Social.ReportScore(playerScore, leaderboardID, success => { });
  46.          }
  47.      }
  48.  
  49.      public void ShowLeaderboard()
  50.      {
  51.          if (Social.Active.localUser.authenticated)
  52.          {
  53.              platform.ShowLeaderboardUI();
  54.          }
  55.      }
  56.  
  57.      public void ShowAchievements()
  58.      {
  59.          if (Social.Active.localUser.authenticated)
  60.          {
  61.              platform.ShowAchievementsUI();
  62.          }
  63.      }
  64.  
  65.      public void UnlockAchievement()
  66.      {
  67.          if (Social.Active.localUser.authenticated)
  68.          {
  69.              Social.ReportProgress(achievementID, 100f, success => { });
  70.          }
  71.      }
  72.      public void ChangeAchievementID(string newValue){
  73.          achievementID = newValue;
  74.      }
  75.      public void ChangeLeaderBoardID(string newValue){
  76.          leaderboardID = newValue;
  77.      }
  78.      public void SetScore(int newValue){
  79.          playerScore = newValue;
  80.      }
  81.      //cloud save
  82.       public void OpenSaveToCloud(bool saving)
  83.      {
  84.         // debugtext.text = "hello";
  85.          if(Social.localUser.authenticated)
  86.          {
  87.            //  debugtext.text = "hello2";
  88.              issaving = saving;
  89.              ((PlayGamesPlatform)Social.Active).SavedGame.OpenWithAutomaticConflictResolution
  90.                  (SAVE_NAME, GooglePlayGames.BasicApi.DataSource.ReadCacheOrNetwork,
  91.                  ConflictResolutionStrategy.UseLongestPlaytime, SavedGameOpen);
  92.  
  93.          
  94.      }
  95.      }
  96.  
  97.      private void SavedGameOpen(SavedGameRequestStatus status, ISavedGameMetadata meta)
  98.      {
  99.          if(status == SavedGameRequestStatus.Success)
  100.          {
  101.             // debugtext.text = "hello in save1";
  102.              if (issaving)//if is saving is true we are saving our data to cloud
  103.              {
  104.                 // debugtext.text = "hello in save2";
  105.                  byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes(GetDataToStoreinCloud());
  106.                  SavedGameMetadataUpdate update = new SavedGameMetadataUpdate.Builder().Build();
  107.                  ((PlayGamesPlatform)Social.Active).SavedGame.CommitUpdate(meta, update, data, SaveUpdate);
  108.              }
  109.              else//if is saving is false we are opening our saved data from cloud
  110.              {
  111.                  ((PlayGamesPlatform)Social.Active).SavedGame.ReadBinaryData(meta, ReadDataFromCloud);
  112.              }
  113.          }
  114.      }
  115.  
  116.      private void ReadDataFromCloud(SavedGameRequestStatus status, byte[] data)
  117.      {
  118.          if(status == SavedGameRequestStatus.Success)
  119.          {
  120.              string savedata = System.Text.ASCIIEncoding.ASCII.GetString(data);
  121.              LoadDataFromCloudToOurGame(savedata);
  122.          }
  123.      }
  124.      private void SaveUpdate(SavedGameRequestStatus status, ISavedGameMetadata meta)
  125.      {
  126.          //use this to debug whether the game is uploaded to cloud
  127.          Debug.Log("successfully add data to cloud");
  128.      }
  129.      private string GetDataToStoreinCloud()//  we seting the value that we are going to store the data in cloud
  130.      {
  131.          string Data = "";
  132.      //data [0]
  133.          Data += savedString;
  134.          Data += "|";
  135.          return Data;
  136.      }
  137.      private void LoadDataFromCloudToOurGame(string savedata)
  138.      {
  139.          string[] data = savedata.Split('|');
  140.          Debug.Log(data[0].ToString());
  141.      }
  142.      public void SetSaveString(string newString){
  143.          savedString = newString;
  144.      }
  145.      
  146.  }
  147.  
Advertisement
Add Comment
Please, Sign In to add comment