How to Use iOS GameCenter Leaderboards in Unity3d

You need to setup your app on the appstore connect first with the appropriate leaderboard. And you can only test this code on the iOS device and not in the unity3d player.


using UnityEngine.SocialPlatforms;

  private ILeaderboard leaderboard;
  private string leaderboard_id = "yourleaderboardid"; // setup on appstore connect

 void Start()
 {

    // Authenticate user first
        Social.localUser.Authenticate(success => {
            if (success)
            {
                Debug.Log("Authentication successful");
                string userInfo = "Username: " + Social.localUser.userName +
                    "\nUser ID: " + Social.localUser.id +
                    "\nIsUnderage: " + Social.localUser.underage;
                Debug.Log(userInfo);
            }
            else
                Debug.Log("Authentication failed");
        });

  // create social leaderboard
        leaderboard  = Social.CreateLeaderboard();
        leaderboard.id = leaderboard_id;
        leaderboard.LoadScores(result =>
        {
            Debug.Log("Received " + leaderboard.scores.Length + " scores");
            foreach (IScore score in leaderboard.scores)
                Debug.Log(score);
        });
  }

  void ReportScore(long score, string leaderboardID)
  {
        Debug.Log("Reporting score " + score + " on leaderboard " + leaderboardID);
        Social.ReportScore(score, leaderboardID, success => {
            Debug.Log(success ? "Reported score successfully" : "Failed to report score");
        });
  }

 void OpenLeaderboard()
 {
        Social.ShowLeaderboardUI();
 }