Created
April 13, 2023 15:31
-
-
Save ardaerbaharli/293e03f1442fb3082a68e95cc4e17afa to your computer and use it in GitHub Desktop.
Social platforms solution for both android and iOS
This file contains hidden or 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
| using UnityEngine; | |
| namespace SocialPlatforms | |
| { | |
| public class Achievement | |
| { | |
| public AchievementType Type; | |
| public string ID; | |
| public bool Completed; | |
| public int TotalSteps; | |
| public int CurrentSteps; | |
| public bool IsIncremental; | |
| public Achievement(AchievementType type, bool isIncremental = false, int totalSteps = 1) | |
| { | |
| Type = type; | |
| ID = AchievementID.GetIDRespectfulToPlatform(type); | |
| IsIncremental = isIncremental; | |
| TotalSteps = totalSteps; | |
| CurrentSteps = PlayerPrefs.GetInt(Type + "_Progress", 0); | |
| Completed = CurrentSteps >= TotalSteps; | |
| } | |
| public void ReportProgress(int steps = 1) | |
| { | |
| if (Completed) return; | |
| CurrentSteps += steps; | |
| PlayerPrefs.SetInt(Type + "_Progress", CurrentSteps); | |
| if (CurrentSteps >= TotalSteps) | |
| { | |
| SetCompleted(); | |
| } | |
| } | |
| public void SetCompleted() | |
| { | |
| if (Completed) return; | |
| CurrentSteps = TotalSteps; | |
| Completed = true; | |
| AchievementsManager.Instance.ReportAchievement(ID); | |
| } | |
| } | |
| } |
This file contains hidden or 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
| using System.Collections.Generic; | |
| namespace SocialPlatforms | |
| { | |
| public static class AchievementID | |
| { | |
| private static Dictionary<AchievementType, string> IOSIDS = new() | |
| { | |
| {AchievementType.Lucky13, "INSERT IOS ID"}, | |
| {AchievementType.CallingTheDevil, "INSERT IOS ID"}, | |
| }; | |
| private static Dictionary<AchievementType, string> AndroidIDS = new() | |
| { | |
| {AchievementType.Lucky13, "INSERT ANDROID ID"}, | |
| {AchievementType.CallingTheDevil, "INSERT ANDROID ID"}, | |
| }; | |
| public static string GetIDRespectfulToPlatform(AchievementType type) | |
| { | |
| #if UNITY_ANDROID | |
| return AndroidIDS[type]; | |
| #elif UNITY_IOS | |
| return IOSIDS[type]; | |
| #endif | |
| return null; | |
| } | |
| public static string GetAchievementID(AchievementType type) | |
| { | |
| return GetIDRespectfulToPlatform(type); | |
| } | |
| } | |
| } |
This file contains hidden or 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
| using System.Collections.Generic; | |
| using System.Linq; | |
| using UnityEngine; | |
| using UnityEngine.SocialPlatforms; | |
| #if UNITY_ANDROID | |
| using GooglePlayGames; | |
| #elif UNITY_IOS | |
| using UnityEngine.SocialPlatforms.GameCenter; | |
| #endif | |
| namespace SocialPlatforms | |
| { | |
| public class AchievementsManager : MonoBehaviour | |
| { | |
| public static AchievementsManager Instance; | |
| private static Achievement Lucky13 => new(AchievementType.Lucky13); | |
| private static Achievement CallingTheDevil => new(AchievementType.CallingTheDevil); | |
| private static List<Achievement> Achievements => new() | |
| { | |
| Lucky13, | |
| CallingTheDevil, | |
| }; | |
| private void Awake() | |
| { | |
| Instance = this; | |
| } | |
| private void Start() | |
| { | |
| SocialPlatformManager.Instance.OnLoginSuccess += OnLoginSuccess; | |
| } | |
| private void OnLoginSuccess() | |
| { | |
| LoadAchievements(); | |
| } | |
| private void LoadAchievements() | |
| { | |
| Social.LoadAchievements(LoadAchievementsCallBack); | |
| } | |
| private void LoadAchievementsCallBack(IAchievement[] achievements) | |
| { | |
| if (achievements.Length == 0) | |
| print("Error: no achievements found"); | |
| else | |
| { | |
| foreach (var achievement in achievements) | |
| { | |
| var a = Achievements.First(x => x.ID == achievement.id); | |
| a.Completed = achievement.completed; | |
| } | |
| } | |
| } | |
| public Achievement GetAchievement(AchievementType type) | |
| { | |
| return Achievements.First(x => x.ID == AchievementID.GetIDRespectfulToPlatform(type)); | |
| } | |
| public void ReportAchievement(string achievementID) | |
| { | |
| if (Social.localUser.authenticated == false) | |
| { | |
| SocialPlatformManager.Instance.AuthenticateSocialPlatform(); | |
| return; | |
| } | |
| #if UNITY_ANDROID | |
| PlayGamesPlatform.Instance.ReportProgress(achievementID, 100, AchievementReportCallBack); | |
| #elif UNITY_IOS | |
| Social.ReportProgress(achievementID, 100, AchievementReportCallBack); | |
| #endif | |
| } | |
| private void AchievementReportCallBack(bool result) | |
| { | |
| if (result) | |
| { | |
| print("Successfully reported achievement "); | |
| LoadAchievements(); | |
| } | |
| else | |
| { | |
| print("Failed to report achievement "); | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or 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
| namespace SocialPlatforms | |
| { | |
| public enum AchievementType | |
| { | |
| Lucky13, | |
| CallingTheDevil, | |
| } | |
| } |
This file contains hidden or 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
| using System.Collections.Generic; | |
| #if UNITY_ANDROID | |
| using GooglePlayGames; | |
| #endif | |
| using UnityEngine; | |
| namespace SocialPlatforms | |
| { | |
| public class LeaderboardsManager : MonoBehaviour | |
| { | |
| public static LeaderboardsManager Instance; | |
| private Dictionary<LeaderboardType, string> AndroidIDS = new() | |
| { | |
| {LeaderboardType.TopScore, "INSERT ANDROID ID"}, | |
| {LeaderboardType.TimeRunTopScore, "INSERT ANDROID ID"}, | |
| }; | |
| private Dictionary<LeaderboardType, string> IOSIDS = new() | |
| { | |
| {LeaderboardType.TopScore, "INSERT IOS ID"}, | |
| {LeaderboardType.TimeRunTopScore, "INSERT IOS ID"}, | |
| }; | |
| private void Awake() | |
| { | |
| Instance = this; | |
| } | |
| private void Start() | |
| { | |
| SocialPlatformManager.Instance.OnLoginSuccess += OnLoginSuccess; | |
| } | |
| private void OnLoginSuccess() | |
| { | |
| } | |
| public void ReportScore(LeaderboardType leaderboardType, long score) | |
| { | |
| if (Social.localUser.authenticated == false) | |
| { | |
| SocialPlatformManager.Instance.AuthenticateSocialPlatform(); | |
| return; | |
| } | |
| #if UNITY_IOS | |
| Social.ReportScore(score, GetLeaderboardID(leaderboardType), success => | |
| { | |
| if (success) | |
| { | |
| Debug.Log("Score submitted"); | |
| } | |
| else | |
| { | |
| Debug.Log("Failed to submit score"); | |
| } | |
| }); | |
| #elif UNITY_ANDROID | |
| PlayGamesPlatform.Instance.ReportScore(score, GetLeaderboardID(leaderboardType), success => | |
| { | |
| if (success) | |
| { | |
| Debug.Log("Score submitted"); | |
| } | |
| else | |
| { | |
| Debug.Log("Failed to submit score"); | |
| } | |
| }); | |
| #endif | |
| } | |
| public void ShowLeaderboard() | |
| { | |
| Social.ShowLeaderboardUI(); | |
| } | |
| private string GetLeaderboardID(LeaderboardType type) | |
| { | |
| #if UNITY_ANDROID | |
| return AndroidIDS[type]; | |
| #elif UNITY_IOS | |
| return IOSIDS[type]; | |
| #endif | |
| return null; | |
| } | |
| } | |
| } |
This file contains hidden or 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
| namespace SocialPlatforms | |
| { | |
| public enum LeaderboardType | |
| { | |
| TopScore, | |
| TimeRunTopScore, | |
| TopSpeed | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment