Skip to content

Instantly share code, notes, and snippets.

@ardaerbaharli
Created April 13, 2023 15:31
Show Gist options
  • Select an option

  • Save ardaerbaharli/293e03f1442fb3082a68e95cc4e17afa to your computer and use it in GitHub Desktop.

Select an option

Save ardaerbaharli/293e03f1442fb3082a68e95cc4e17afa to your computer and use it in GitHub Desktop.
Social platforms solution for both android and iOS
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);
}
}
}
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);
}
}
}
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 ");
}
}
}
}
namespace SocialPlatforms
{
public enum AchievementType
{
Lucky13,
CallingTheDevil,
}
}
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;
}
}
}
namespace SocialPlatforms
{
public enum LeaderboardType
{
TopScore,
TimeRunTopScore,
TopSpeed
}
}
using System;
#if UNITY_ANDROID
using GooglePlayGames.BasicApi;
using GooglePlayGames;
#elif UNITY_IOS
using UnityEngine.SocialPlatforms.GameCenter;
#endif
using UnityEngine;
namespace SocialPlatforms
{
public class SocialPlatformManager : MonoBehaviour
{
public static string UserId { get; private set; }
public Action OnLoginSuccess;
private bool SuccessfullLogin;
public static SocialPlatformManager Instance;
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
return;
}
AuthenticateSocialPlatform();
}
public void AuthenticateSocialPlatform()
{
if (SuccessfullLogin) return;
#if UNITY_EDITOR
print("Social platforms are not supported in the editor.");
return;
#elif UNITY_ANDROID
PlayGamesPlatform.Activate();
PlayGamesPlatform.Instance.Authenticate((signInStatus) =>
{
var status = false || signInStatus == SignInStatus.Success;
AuthenticateSocialCallBack(status);
});
#elif UNITY_IOS
Social.localUser.Authenticate(AuthenticateSocialCallBack);
GameCenterPlatform.ShowDefaultAchievementCompletionBanner(true);
#endif
}
private void AuthenticateSocialCallBack(bool success)
{
if (success)
{
print("Successfully logged into social platform");
// Make request to get loaded achievements and register a callback for processing them
UserId = Social.localUser.id;
print("Social platform UserID: " + Social.localUser.id);
print("Social platform UserName: " + Social.localUser.userName);
SuccessfullLogin = true;
OnLoginSuccess?.Invoke();
}
else
{
print("Failed to log into social platform");
}
}
public void ShowLeaderboards()
{
Debug.Log("ShowAchievement");
#if UNITY_EDITOR
Debug.LogError("Cannot show Leaderboard UI in Editor");
#elif UNITY_IOS
Social.ShowLeaderboardUI();
#elif UNITY_ANDROID
PlayGamesPlatform.Instance.ShowLeaderboardUI();
#endif
}
public void ShowAchievements()
{
Debug.Log("ShowAchievement");
#if UNITY_EDITOR
Debug.LogError("Cannot show achievement UI in Editor");
#elif UNITY_IOS
Social.ShowAchievementsUI ();
#elif UNITY_ANDROID
PlayGamesPlatform.Instance.ShowAchievementsUI();
#endif
}
public void ResetAllAchievements(Action<bool> result)
{
result?.Invoke(false);
}
public void Destroy()
{
Destroy(this.gameObject);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment