Friday, January 18, 2013

Submit Scores and Points to Game Center in Unity3D

This simple script is how to submit scores and points to the Game Center from a Unity3D Game. People use the prime31 plugin, and pay the extra money, which dumbfounds me. Just do it yourself! I've done both JS and C# implementations of the same code... Here it goes in JavaScript:



#if UNITY_IPHONE
import UnityEngine.SocialPlatforms;
#endif

//These next two methods show the leaderboard

function DoLeaderboard () {
#if UNITY_IPHONE
Social.localUser.Authenticate (ProcessAuthentication);
#endif
}

function ProcessAuthentication (success: boolean) {
#if UNITY_IPHONE
    if (success) {
        Debug.Log ("Authentication successful");
    Social.CreateLeaderboard();
Social.CreateLeaderboard().id = "YourLeaderBoardID";
Social.ShowLeaderboardUI();
    }
    else
        Debug.Log ("Failed to authenticate");
#endif
}


//these next two methods report a score to the leaderboard

function reportScoreToBoard() {
#if UNITY_IPHONE
Social.localUser.Authenticate (ReportScore);
#endif
}


function ReportScore (success: boolean) {
#if UNITY_IPHONE
    if (success) {
        
        Debug.Log ("Authentication successful");
   
    Social.CreateLeaderboard();

Social.CreateLeaderboard().id = "YourLeaderBoardID";
Social.ReportScore(PlayerPrefs.GetInt("timeElapsed"),"YourLeaderBoardID",  function(result) {
        if (result)
            Debug.Log ("Successfully reported timeElapsed, Virgin:" + PlayerPrefs.GetInt("timeElapsed"));
        else
            Debug.Log ("Failed to report timeElapsed");});

//if you want uncomment below to show leaderboard!
//Social.ShowLeaderboardUI();
    }
    else
        Debug.Log ("Failed to authenticate");
        
 #endif
}



9 comments:

  1. wow,
    this was really cool, this is one amazing article! Thank you for posting this.

    Hire iPhone Game Developer | Unity3D Game Development Company

    ReplyDelete
  2. Awesome, thanks! Easier than I thought :D

    ReplyDelete
  3. Awesome, thanks! Easier than I thought :D

    ReplyDelete
  4. Man, thanks so much for posting it! You saved my life!

    ReplyDelete
  5. That's not working as is, there is probably something to setup elswhere, any detailed steps ?

    ReplyDelete
    Replies
    1. Yeah could you give more detailed steps and also maybe show the C# code. Thanks.

      Delete
  6. Hi, thanks for your tips! it's working as well!
    But I have a question.
    Is it possible to load the highscore from gamecenter? I explain my probleme:
    When the player do a highscore ( for exemple : 99 points) and go to the game over, he send his highscore to the gamecenter.
    And in the playerprefs of the game, the highscore is saved.

    But if the player deletes the app and downloads it again, the highscore from the gamecenter is 99 points again but in my playerprefs from the game, it has been reinitalized to 0.
    So I want to do a check when the player lunches the game.
    I hope you understood my question!

    Kinds Regards,
    Xavier

    ReplyDelete
  7. Thank you very much! That is some very useful example code. You saved me quite a bit of time.

    ReplyDelete
  8. Social.CreateLeaderboard();
    Social.CreateLeaderboard().id = "YourLeaderBoardID";

    The code above is wrong, you dont want to call CreateLeaderboard() twice. It leads to duplicate entries in the leaderboards (not permanent ones, but game center gets confused for a bit.

    ReplyDelete