/// <summary> /// Loads local saves. /// </summary> /// <returns></returns> private Dictionary <string, string> LoadLocal() { if (!SaveGame.Exists(SaveId)) { Logger.Log("No saves found"); return(new Dictionary <string, string>()); } Dictionary <string, string> saves = null; Logger.Log("Loading save"); saves = SaveGame.Load(SaveId, Saves, Encode, EncodePassword, SaveGame.Serializer, SaveGame.Encoder, Encoding, SavePath); var saveMessageBuilder = new StringBuilder($"Loaded values: {Environment.NewLine}"); foreach (KeyValuePair <string, string> save in saves) { saveMessageBuilder.Append($"{save.Key}: {save.Value}"); } Logger.Log(saveMessageBuilder.ToString()); return(saves); }
public void IncrementAchievement(string achievementId, int progress) { if (!Social.localUser.authenticated) { Logger.LogError("Not logged in!"); return; } if (CurrentPlatform == RuntimePlatform.Android) { #if UNITY_ANDROID PlayGamesPlatform.Instance.IncrementAchievement(achievementId, progress, (bool success) => { if (success) { Logger.Log("Achievement incremented"); } else { Logger.LogError("Achievement failed to increment"); } }); #endif } else { Social.LoadAchievements(achievements => { if (achievements.Length > 0) { foreach (IAchievement achievement in achievements) { if (achievement.id == achievementId) { Social.ReportProgress(achievementId, achievement.percentCompleted + (double)progress, success => { if (success) { Logger.Log("Achievement incremented"); } else { Logger.LogError( "Achievement failed to increment"); } }); } } } }); } }
/// <summary> /// Checks if cloud saving was successful or not. /// </summary> /// <param name="status"></param> /// <param name="meta"></param> private void OnPlayServicesSaved(SavedGameRequestStatus status, ISavedGameMetadata meta) { switch (status) { case SavedGameRequestStatus.Success: Logger.Log("Saved to cloud"); break; default: //error Logger.LogError($"Failed to save {status}"); throw new Exception($"Failed to save. {status}"); } }
public void AddScore(long score, string leaderboardId) { if (!Social.localUser.authenticated) { Logger.LogError("Not logged in!"); return; } Social.ReportScore(score, leaderboardId, (bool success) => { if (success) { Logger.Log($"Posted new score to leaderboard {leaderboardId}"); } else { Logger.LogError($"Unable to post new score to leaderboard {leaderboardId}"); } }); }
/// <summary> /// Check if cloud save was successfully loaded. If true then cloud save file is deserialized. /// CloudLoadStatus is also updated to inform that a file is loaded. /// </summary> /// <param name="status"></param> /// <param name="data"></param> /// <param name="cloudLoadStatus"></param> /// <param name="meta"></param> private void OnPlayServicesLoaded(SavedGameRequestStatus status, byte[] data, CloudLoadStatus cloudLoadStatus, ISavedGameMetadata meta = null) { switch (status) { case SavedGameRequestStatus.Success: Logger.Log("Loaded from cloud."); if (meta != null) { string json = DeserializeByte(data); cloudLoadStatus.Loaded(meta.Filename, meta.Filename, json); } break; default: //error Logger.LogError($"Failed to load. {status}"); throw new Exception($"Failed to load. {status}"); } }
/// <summary> /// A method used for cloud saving. /// Initializes and logs in to Google Play Games on Android and KeyChain on iOS. /// Loads cloud saves after logging in and makes sure locally saved material is the same as in the cloud. /// This method is called by a service handler via the LoadResources method. /// </summary> /// <param name="onSuccess">Callback for success.</param> /// <param name="onFail">Callback for failure.</param> private void PlatformInitialization(Action onSuccess, Action onFail) { #if UNITY_ANDROID PlayGamesClientConfiguration.Builder builder = new PlayGamesClientConfiguration.Builder(); builder.EnableSavedGames(); PlayGamesPlatform.InitializeInstance(builder.Build()); PlayGamesPlatform.Activate(); #endif Social.localUser.Authenticate((bool success) => { if (success) { Logger.Log("Logged in"); #if UNITY_ANDROID //load Android cloud saves var platform = (PlayGamesPlatform)Social.Active; //fetches all Android cloud saves platform.SavedGame.FetchAllSavedGames(DataSource.ReadCacheOrNetwork, OnPlayServicesSavesFetched); #endif #if UNITY_IOS //load iOS cloud saves KeyChain.InitializeKeychain(); foreach (var saveName in Saves) { if (!Saves[saveName.Key].Equals(KeyChain.GetString(saveName.Key))) { Saves[saveName.Key] = KeyChain.GetString(saveName.Key); } } #endif onSuccess(); } else { Logger.LogError("Failed to login"); onFail(); } }); }
public void UnlockAchievement(string achievementId) { if (!Social.localUser.authenticated) { Logger.LogError("Not logged in!"); return; } if (CurrentPlatform == RuntimePlatform.Android) { Social.ReportProgress(achievementId, 100.0f, (bool success) => { if (success) { Logger.Log("Achievement unlocked"); } else { Logger.LogError("Achievement failed to unlock"); } }); } else { Social.ReportProgress(achievementId, 100.0, (bool success) => { if (success) { Logger.Log("Achievement unlocked"); } else { Logger.LogError("Achievement failed to unlock"); } }); } }
private void OnPlayServicesError(PlayServiceError err) { Logger.Log(err); throw new Exception($"Error opening Android cloud saves. {err}"); }
/// <summary> /// Failed opening an Android cloud save. /// </summary> /// <param name="err"></param> /// <param name="fileName"></param> /// <param name="cloudLoadStatus"></param> private void OnPlayServicesError(PlayServiceError err, string fileName, CloudLoadStatus cloudLoadStatus) { Logger.Log(err); cloudLoadStatus.Loaded(fileName); throw new Exception($"Error opening Android cloud saves. {err}"); }