Tuesday, October 2, 2012

Using Social APIs from Unity (Leaderboard)

To submit a high score from your app to the Game Center leaderboard is easy with the Social API:
IN C# put this in your score dialog or .cs code:


void DoLeaderboard () {
    
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);
    
Social.CreateLeaderboard();
if (Application.loadedLevel == 18)
{
Social.CreateLeaderboard().id = "score18";
ReportScore(TotalScore + TotalScore2,"score18");
shown = true;
else if (Application.loadedLevel == 9)
{
Social.CreateLeaderboard().id = "score9";
ReportScore(TotalScore,"score9");
shown = true;
}
Social.ShowLeaderboardUI();
}
    else
        Debug.Log ("Authentication failed");
} );
}

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");
    });
}

No comments:

Post a Comment