Hi,
I used this guide to implement a system for save and load my data:
https://unity3d.com/learn/tutorials/topics/scripting/persistence-saving-and-loading-data
Now, I want to implement the save in the cloud with the google api, but I can not find an example code to save and load the data.
I tried to follow the guide but I did not understand much.
Can someone post an example code using, for example, an “int coin” variable and a “bool ulevel” variable?
This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using GooglePlayGames.BasicApi.SavedGame;
public class SaveLoad : MonoBehaviour {
public void Save()
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + "/playerInfo.dat");
PlayerData data = new PlayerData();
data.coin = Data.coin;
data.ulevel = Data.ulevel;
bf.Serialize (file, data);
file.Close ();
}
public void Load()
{
if (File.Exists (Application.persistentDataPath + "/playerInfo.dat"))
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize (file);
file.Close ();
Data.coin = data.coin;
Data.ulevel = data.ulevel;
}
}
public void Delete()
{
if (File.Exists (Application.persistentDataPath + "/playerInfo.dat"))
{
File.Delete (Application.persistentDataPath + "/playerInfo.dat");
}
}
[Serializable]
public class PlayerData
{
public int coin;
public bool ulevel;}
This is the code of Google api:
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using UnityEngine.SocialPlatforms;
using UnityEngine;
//gpg
using GooglePlayGames.BasicApi.SavedGame;
//for encoding
using System.Text;
//for text, remove
using UnityEngine.UI;
using System;
public class GameManagerGoogle : MonoBehaviour {
PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
// enables saving game progress.
.EnableSavedGames()
// registers a callback to handle game invitations received while the game is not running.
//.WithInvitationDelegate(<callback method>)
// registers a callback for turn based match notifications received while the
// game is not running.
//.WithMatchDelegate(<callback method>)
// requests the email address of the player be available.
// Will bring up a prompt for consent.
//.RequestEmail()
// requests a server auth code be generated so it can be passed to an
// associated back end server application and exchanged for an OAuth token.
//.RequestServerAuthCode(false)
// requests an ID token be generated. This OAuth token can be used to
// identify the player to other services such as Firebase.
//.RequestIdToken()
.Build();
void Start()
{
PlayGamesPlatform.InitializeInstance (config);
// recommended for debugging:
PlayGamesPlatform.DebugLogEnabled = true;
// Activate the Google Play Games platform
PlayGamesPlatform.Activate ();
}
public void SignIn()
{
// authenticate user:
Social.localUser.Authenticate((bool success) => {
// handle success or failure
if (success)
{
Debug.Log("You've successfully logged in");
}
else
{
Debug.Log("Login failed for some reason");
}
});
}
public void SignOut()
{
// sign out
PlayGamesPlatform.Instance.SignOut();
}
public void ShowAchievementsUI()
{
// show achievements UI
Social.ShowAchievementsUI();
}
public void ShowLeaderboardUI()
{
// show leaderboard UI
Social.ShowLeaderboardUI();
}
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.
Debug.Log("1");
} else {
// handle error
Debug.Log("2");
}
}
void SaveGame (ISavedGameMetadata game, byte[] savedData, TimeSpan totalPlaytime) {
ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
SavedGameMetadataUpdate.Builder builder = new SavedGameMetadataUpdate.Builder();
builder = builder
.WithUpdatedPlayedTime(totalPlaytime)
.WithUpdatedDescription("Saved game at " + DateTime.Now);
SavedGameMetadataUpdate updatedMetadata = builder.Build();
savedGameClient.CommitUpdate(game, updatedMetadata, savedData, OnSavedGameWritten);
}
public void OnSavedGameWritten (SavedGameRequestStatus status, ISavedGameMetadata game) {
if (status == SavedGameRequestStatus.Success) {
// handle reading or writing of saved game.
Debug.Log("3");
} else {
// handle error
Debug.Log("4");
}
}
void LoadGameData (ISavedGameMetadata game) {
ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
savedGameClient.ReadBinaryData(game, OnSavedGameDataRead);
}
public void OnSavedGameDataRead (SavedGameRequestStatus status, byte[] data) {
if (status == SavedGameRequestStatus.Success) {
// handle processing the byte array data
Debug.Log("5");
} else {
// handle error
Debug.Log("6");
}
}
}
How should I modify it?