public void ShowIntegratedAchievements() { if (!Social.localUser.authenticated) { Logger.LogError("Not logged in!"); return; } Social.ShowAchievementsUI(); }
public void ShowIntegratedLeaderboard() { if (!Social.localUser.authenticated) { Logger.LogError("Not logged in!"); return; } Social.ShowLeaderboardUI(); }
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}"); } }
/// <summary> /// After all Android cloud saves are fetched this method opens each save file. /// Google Play requires that each cloud save is "opened" before actually loading or saving it. /// </summary> /// <param name="status">Saved game request status</param> /// <param name="savesList">All fetched saves</param> private void OnPlayServicesSavesFetched(SavedGameRequestStatus status, List <ISavedGameMetadata> savesList) { var cloudLoadStatus = new CloudLoadStatus(savesList, OnAllSavesLoaded); if (status == SavedGameRequestStatus.Success) { foreach (var save in savesList) { var fileName = save.Filename; OpenAndroidCloudSave(save.Filename, (requestStatus, metadata) => OnPlayServicesLoadResponse(requestStatus, metadata, cloudLoadStatus), error => OnPlayServicesError(error, fileName, cloudLoadStatus)); } } else { Logger.LogError($"Failed to fetch all cloud saved games {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"); } }); } }