Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Tutorial: GameCenter Leaderboards in 5 minutes! (for intermediate/advanced scripters)

Discussion in 'iOS and tvOS' started by zombie_psy, Apr 6, 2014.

  1. zombie_psy

    zombie_psy

    Joined:
    Oct 9, 2012
    Posts:
    62
    This is assuming you already have your GameCenter activated for your game at developer.apple.com, and never did this, or first time trying out Unity's Social API and could use a quick 5 minute explanation of how to get this thing working. As of this writing, Unity's Social API code is incomplete and every user will have to do additional searching to find out how to get the callback working. I'll have it explained out here for you guys instead.

    1) From Unity's Social API, copy and paste this code into a empty gameObject that is part of your loading section:

    Code (csharp):
    1. import UnityEngine.SocialPlatforms;
    2.  
    3. function Start () {
    4.     // Authenticate and register a ProcessAuthentication callback
    5.     // This call needs to be made before we can proceed to other calls in the Social API
    6.     Social.localUser.Authenticate (ProcessAuthentication);
    7. }
    8.  
    9. // This function gets called when Authenticate completes
    10. // Note that if the operation is successful, Social.localUser will contain data from the server.
    11. function ProcessAuthentication (success: boolean) {
    12.     if (success) {
    13.         Debug.Log ("Authenticated, checking achievements");
    14.  
    15.         // Request loaded achievements, and register a callback for processing them
    16.         Social.LoadAchievements (ProcessLoadedAchievements);
    17.     }
    18.     else
    19.         Debug.Log ("Failed to authenticate");
    20. }
    21.  
    22. // This function gets called when the LoadAchievement call completes
    23. function ProcessLoadedAchievements (achievements: IAchievement[]) {
    24.     if (achievements.Length == 0)
    25.         Debug.Log ("Error: no achievements found");
    26.     else
    27.         Debug.Log ("Got " + achievements.Length + " achievements");
    28.  
    29.     // You can also call into the functions like this
    30.     Social.ReportProgress ("Achievement01", 100.0, function(result) {
    31.         if (result)
    32.             Debug.Log ("Successfully reported achievement progress");
    33.         else
    34.             Debug.Log ("Failed to report achievement");
    35.     });
    36. }
    This is straight from the Social API's page. Check that page for the C# version. This is what load's the actual Gamecenter at the start of your game. From here, you will need to link a button to RetrieveHighScores, and calls to PostScores wherever necessary.

    2) Make your RetrieveHighScores button. To do that, first put:

    import UnityEngine.SocialPlatforms;

    on the top of the script file where your button to retrieve high scores will be. Then, simply have this line of code for the button:

    Code (csharp):
    1. Social.ShowLeaderboardUI();
    3) The final step, posting scores. Basically same as step two, but one extra mandatory error-proofing step:

    Code (csharp):
    1. import UnityEngine.SocialPlatforms; // at the top of the script file where scores will be posting to GC
    2.  
    3. var scoreLong : long = highScore; // Gamecenter requires a long variable
    4. Social.ReportScore(scoreLong,"yourLeaderboardIDinQuotes",HighScoreCheck);
    5.  
    6. static function HighScoreCheck(result : boolean) {
    7.     if(result)
    8.         Debug.Log("score submission successful");
    9.     else
    10.         Debug.Log("score submission failed");
    11. }

    And that's it! Just repeat the ReportScore and the additional error-checking function for every variable you want reported to GameCenter. This is just the leaderboards, but once you get the hang of this, adding Achievements or anything else should be a matter of changing up the variables through the Social scripting reference.
     
  2. zigglr

    zigglr

    Joined:
    Sep 28, 2015
    Posts:
    82
    Great thanks. Step three submits the player's score to the leaderboard, right? Also when can we get a leaderboard ID? Do we have to submit our app to the app store first? Thanks
     
  3. DanielaM

    DanielaM

    Joined:
    Sep 22, 2014
    Posts:
    1
    thanks
     
  4. vexe

    vexe

    Joined:
    May 18, 2013
    Posts:
    644
    Is there a way to get an authentication failure message besides that 'success' boolean?
     
  5. LucasKlein16

    LucasKlein16

    Joined:
    Nov 15, 2016
    Posts:
    1
    thank you!
     
  6. zero_null

    zero_null

    Joined:
    Mar 11, 2014
    Posts:
    159
    Will this work today ?
    Like after 3 plus years ?
     
  7. NinjaJedi

    NinjaJedi

    Joined:
    Jan 15, 2016
    Posts:
    1
    Yes it works perfectly. Thanks for the post!
     
  8. kosted

    kosted

    Joined:
    Mar 14, 2015
    Posts:
    104
    if you want to retrieve the current user leaderboard's score
    Code (CSharp):
    1.         Social.LoadScores("bestplayersvictory", scores => {
    2.             if (scores.Length > 0)
    3.             {
    4.                 Debug.Log("Got " + scores.Length + " scores");
    5.  
    6.                //idUser is retrieve after authentication
    7.                 foreach (IScore score in scores) {
    8.                     if(score.userID == idUser) {
    9.                         Debug.Log(score.value);
    10.                         PlayerPrefs.SetInt("nombreVictoire", Int32.Parse(score.value));
    11.                     }
    12.                 }
    13.             }
    14.  
    15.         });
     
    NWagter likes this.
  9. Lajo

    Lajo

    Joined:
    Dec 23, 2016
    Posts:
    24
    But can you post C# script. I copied this and it show a number of errors????
     
  10. NonPlayerCorey

    NonPlayerCorey

    Joined:
    Aug 13, 2015
    Posts:
    14
    In case you're still wondering, you're probably trying to use c# when the code is probably in javascript :)

    Code (CSharp):
    1.  
    2.     // This function gets called when Authenticate completes
    3.     // Note that if the operation is successful, Social.localUser will contain data from the server.
    4.     private void ProcessAuthentication(bool success)
    5.     {
    6.         if (success)
    7.         {
    8.             Debug.Log("Authenticated, checking achievements");
    9.  
    10.             // Request loaded achievements, and register a callback for processing them
    11.             Social.LoadAchievements(ProcessLoadedAchievements);
    12.         }
    13.         else
    14.             Debug.Log("Failed to authenticate");
    15.     }
    16.  
    17.     // This function gets called when the LoadAchievement call completes
    18.     private void ProcessLoadedAchievements(IAchievement[] achievements )
    19.     {
    20.         if (achievements.Length == 0)
    21.             Debug.Log("Error: no achievements found");
    22.         else
    23.             Debug.Log("Got " + achievements.Length + " achievements");
    24.  
    25.  
    26.         // You can also call into the functions like this
    27.         Social.ReportProgress("Achievement01", 100.0, Report);
    28.  
    29.     }
    30.  
    31.     private void Report(bool result)
    32.     {
    33.         if (result)
    34.             Debug.Log("Successfully reported achievement progress");
    35.         else
    36.             Debug.Log("Failed to report achievement");
    37.     }
     
    Last edited: Feb 9, 2018