유니티 안드로이드 게임의 데이터를 구글 플레이 서비스 클라우드에 저장하고 가져오는 스크립트 입니다. 유니티 포럼에서 본걸 조금만 수정했어요. 잘되는거 테스트 완료.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// code reference: http://answers.unity3d.com/questions/894995/how-to-saveload-with-google-play-services.html | |
// you need to import https://github.com/playgameservices/play-games-plugin-for-unity | |
using UnityEngine; | |
using System; | |
using System.Collections; | |
//gpg | |
using GooglePlayGames; | |
using GooglePlayGames.BasicApi; | |
using GooglePlayGames.BasicApi.SavedGame; | |
//for encoding | |
using System.Text; | |
//for extra save ui | |
using UnityEngine.SocialPlatforms; | |
//for text, remove | |
using UnityEngine.UI; | |
public class PlayCloudDataManager : MonoBehaviour | |
{ | |
private static PlayCloudDataManager instance; | |
public static PlayCloudDataManager Instance | |
{ | |
get | |
{ | |
if (instance == null) | |
{ | |
instance = FindObjectOfType<PlayCloudDataManager>(); | |
if (instance == null) | |
{ | |
instance = new GameObject("PlayGameCloudData").AddComponent<PlayCloudDataManager>(); | |
} | |
} | |
return instance; | |
} | |
} | |
public bool isProcessing | |
{ | |
get; | |
private set; | |
} | |
public string loadedData | |
{ | |
get; | |
private set; | |
} | |
private const string m_saveFileName = "game_save_data"; | |
public bool isAuthenticated | |
{ | |
get | |
{ | |
return Social.localUser.authenticated; | |
} | |
} | |
private void InitiatePlayGames() | |
{ | |
PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder() | |
// enables saving game progress. | |
.EnableSavedGames() | |
.Build(); | |
PlayGamesPlatform.InitializeInstance(config); | |
// recommended for debugging: | |
PlayGamesPlatform.DebugLogEnabled = false; | |
// Activate the Google Play Games platform | |
PlayGamesPlatform.Activate(); | |
} | |
private void Awake() | |
{ | |
InitiatePlayGames(); | |
} | |
public void Login() | |
{ | |
Social.localUser.Authenticate((bool success) => | |
{ | |
if (!success) | |
{ | |
Debug.Log("Fail Login"); | |
} | |
}); | |
} | |
private void ProcessCloudData(byte[] cloudData) | |
{ | |
if (cloudData == null) | |
{ | |
Debug.Log("No Data saved to the cloud"); | |
return; | |
} | |
string progress = BytesToString(cloudData); | |
loadedData = progress; | |
} | |
public void LoadFromCloud(Action<string> afterLoadAction) | |
{ | |
if (isAuthenticated && !isProcessing) | |
{ | |
StartCoroutine(LoadFromCloudRoutin(afterLoadAction)); | |
} | |
else | |
{ | |
Login(); | |
} | |
} | |
private IEnumerator LoadFromCloudRoutin(Action<string> loadAction) | |
{ | |
isProcessing = true; | |
Debug.Log("Loading game progress from the cloud."); | |
((PlayGamesPlatform)Social.Active).SavedGame.OpenWithAutomaticConflictResolution( | |
m_saveFileName, //name of file. | |
DataSource.ReadCacheOrNetwork, | |
ConflictResolutionStrategy.UseLongestPlaytime, | |
OnFileOpenToLoad); | |
while(isProcessing) | |
{ | |
yield return null; | |
} | |
loadAction.Invoke(loadedData); | |
} | |
public void SaveToCloud(string dataToSave) | |
{ | |
if (isAuthenticated) | |
{ | |
loadedData = dataToSave; | |
isProcessing = true; | |
((PlayGamesPlatform)Social.Active).SavedGame.OpenWithAutomaticConflictResolution(m_saveFileName, DataSource.ReadCacheOrNetwork, ConflictResolutionStrategy.UseLongestPlaytime, OnFileOpenToSave); | |
} | |
else | |
{ | |
Login(); | |
} | |
} | |
private void OnFileOpenToSave(SavedGameRequestStatus status, ISavedGameMetadata metaData) | |
{ | |
if (status == SavedGameRequestStatus.Success) | |
{ | |
byte[] data = StringToBytes(loadedData); | |
SavedGameMetadataUpdate.Builder builder = new SavedGameMetadataUpdate.Builder(); | |
SavedGameMetadataUpdate updatedMetadata = builder.Build(); | |
((PlayGamesPlatform)Social.Active).SavedGame.CommitUpdate(metaData, updatedMetadata, data, OnGameSave); | |
} | |
else | |
{ | |
Debug.LogWarning("Error opening Saved Game" + status); | |
} | |
} | |
private void OnFileOpenToLoad(SavedGameRequestStatus status, ISavedGameMetadata metaData) | |
{ | |
if (status == SavedGameRequestStatus.Success) | |
{ | |
((PlayGamesPlatform)Social.Active).SavedGame.ReadBinaryData(metaData, OnGameLoad); | |
} | |
else | |
{ | |
Debug.LogWarning("Error opening Saved Game" + status); | |
} | |
} | |
private void OnGameLoad(SavedGameRequestStatus status, byte[] bytes) | |
{ | |
if (status != SavedGameRequestStatus.Success) | |
{ | |
Debug.LogWarning("Error Saving" + status); | |
} | |
else | |
{ | |
ProcessCloudData(bytes); | |
} | |
isProcessing = false; | |
} | |
private void OnGameSave(SavedGameRequestStatus status, ISavedGameMetadata metaData) | |
{ | |
if (status != SavedGameRequestStatus.Success) | |
{ | |
Debug.LogWarning("Error Saving" + status); | |
} | |
isProcessing = false; | |
} | |
private byte[] StringToBytes(string stringToConvert) | |
{ | |
return Encoding.UTF8.GetBytes(stringToConvert); | |
} | |
private string BytesToString(byte[] bytes) | |
{ | |
return Encoding.UTF8.GetString(bytes); | |
} | |
} |