/// <summary> /// Creates the new save. Save returned in callback is closed!. Open it before use. /// </summary> /// <param name="save">Save.</param> /// <param name="saveCreatedCallback">Invoked when save has been created.</param> private static void CreateNewSave(ISavedGameMetadata save, Action<ISavedGameMetadata> saveCreatedCallback) { ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame; SavedGameMetadataUpdate.Builder builder = new SavedGameMetadataUpdate.Builder (); builder = builder .WithUpdatedPlayedTime(save.TotalTimePlayed.Add(new TimeSpan(0, 0, (int) Time.realtimeSinceStartup))) .WithUpdatedDescription("Saved at " + DateTime.Now); SavedGameMetadataUpdate updatedMetadata = builder.Build(); SaveDataBundle newBundle = new SaveDataBundle(new StoredPlayerData()); savedGameClient.CommitUpdate( save, updatedMetadata, SaveDataBundle.ToByteArray(newBundle), (SavedGameRequestStatus status,ISavedGameMetadata game) => { if (status == SavedGameRequestStatus.Success) { m_saveBundleMetadata = game; if (saveCreatedCallback != null) saveCreatedCallback(game); } Debug.Log("Creating new save finished with status :" + status.ToString()); } ); }
public static byte[] ToByteArray(SaveDataBundle bundle) { var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); using (var stream = new System.IO.MemoryStream()) { formatter.Serialize(stream, bundle); return stream.ToArray(); } }
public void SaveGame(SaveDataBundle file, Action<bool> callback) { CommitSaveToCloud(file, "undefined", callback); }
/// <summary> /// Commits the save to cloud. /// </summary> /// <param name="file">Actual save file. This will replace static reference to current save file</param> /// <param name="fileName">File name. Used only when saving for first time</param> /// <param name="callback">Invoked after commit (true = success)</param> private static void CommitSaveToCloud(SaveDataBundle file, string fileName, System.Action<bool> callback) { ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame; savedGameClient.OpenWithAutomaticConflictResolution( m_saveBundleMetadata.Filename == string.Empty ? fileName : m_saveBundleMetadata.Filename, DataSource.ReadCacheOrNetwork, ConflictResolutionStrategy.UseLongestPlaytime, (SavedGameRequestStatus reqStatus, ISavedGameMetadata openedGame) => { if(reqStatus == SavedGameRequestStatus.Success) { // adding real time since startup so we can determine longes playtime and resolve future conflicts easilly m_saveBundleMetadata = openedGame; SavedGameMetadataUpdate.Builder builder = new SavedGameMetadataUpdate.Builder (); builder = builder .WithUpdatedPlayedTime(timePlayed) .WithUpdatedDescription("Saved game at " + DateTime.Now); if (bannerTexture != null) { builder = builder.WithUpdatedPngCoverImage(bannerTexture.EncodeToPNG()); } //m_saveBundleMetadata.TotalTimePlayed.Add (new TimeSpan (0, 0, (int)Time.realtimeSinceStartup)) SavedGameMetadataUpdate updatedMetadata = builder.Build(); savedGameClient.CommitUpdate( m_saveBundleMetadata, updatedMetadata, SaveDataBundle.ToByteArray(file), (SavedGameRequestStatus status, ISavedGameMetadata game) => { Debug.Log("SGI CommitUpdate callback invoked with status " + status + ", proceeding..."); if (status == SavedGameRequestStatus.Success) { m_saveBundleMetadata = game; m_currentSaveBundle = file; } if (callback != null) callback.Invoke(status == SavedGameRequestStatus.Success); } ); } } ); }
static void LoadGame(SelectUIStatus status, ISavedGameMetadata game) { if (status == SelectUIStatus.SavedGameSelected) { OpenSavedGame(game, (ISavedGameMetadata openedGame) => { if(game.Description == null || game.Description == string.Empty) { // game has not been saved on cloud before, create new file CreateNewSave(openedGame, (ISavedGameMetadata newlySavedGame) => { LoadGame(SelectUIStatus.SavedGameSelected, newlySavedGame); }); return; } // Pull the time played if (game.TotalTimePlayed != null) timePlayed = game.TotalTimePlayed; // save should be opened now Debug.Log ("Loading save from: " + openedGame.Filename + "\n" + openedGame.Description + "\nOpened = " + openedGame.IsOpen.ToString ()); m_saveBundleMetadata = openedGame; ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame; savedGameClient.ReadBinaryData(openedGame, (SavedGameRequestStatus reqStatus, byte[] data) => { Debug.Log("Reading save finished with status: " + reqStatus.ToString()); if (reqStatus == SavedGameRequestStatus.Success) { SaveDataBundle bundle = SaveDataBundle.FromByteArray(data); m_currentSaveBundle = bundle; if (OnSaveLoaded != null) { OnSaveLoaded.Invoke (bundle); OnSaveLoaded = null; } } }); }); } }