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
}
wow,
ReplyDeletethis was really cool, this is one amazing article! Thank you for posting this.
Hire iPhone Game Developer | Unity3D Game Development Company
Awesome, thanks! Easier than I thought :D
ReplyDeleteAwesome, thanks! Easier than I thought :D
ReplyDeleteMan, thanks so much for posting it! You saved my life!
ReplyDeleteThat's not working as is, there is probably something to setup elswhere, any detailed steps ?
ReplyDeleteYeah could you give more detailed steps and also maybe show the C# code. Thanks.
DeleteHi, thanks for your tips! it's working as well!
ReplyDeleteBut 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
Thank you very much! That is some very useful example code. You saved me quite a bit of time.
ReplyDeleteSocial.CreateLeaderboard();
ReplyDeleteSocial.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.