Leaderboards

After the player is connected, you can begin to use the leaderboards API.

Show leaderboards UI

You can show leaderboards UI using this method:

OSXGameCenterManager.showLeaderBoards ();

 

Or you can specify a Leaderboard ID if you want to show  UI of the specific leaderboard.

OSXGameCenterManager.showLeaderBoard(leaderBoardId);

 

Working with current player score.

You can report new high score using reportScore method. As soon as the score will be reported you will get

Example:

OSXGameCenterManager.OnScoreSubmited += OnScoreSubmited;
OSXGameCenterManager.reportScore(hiScore, leaderBoardId);


private void OnScoreSubmited (ISN_Result result) {
	OSXGameCenterManager.OnScoreSubmited -= OnScoreSubmited;
		if(result.IsSucceeded)  {
		Debug.Log("Score Submited");
	} else {
		Debug.Log("Score Submit Failed");
	}
}

 

 

 

Note: You should use score report, event if the user gets result lower than his top score, his top result will not be overridden. Good practise is to report the score after a player completes the level. In this case Game Center will be able to track not only top player results, but also daily and weekly charts.

 

If you want to get user scores, you should use loadCurrentPlayerScoreLocal method and subscribe to the OnPlayerScoreLoaded action.

OSXGameCenterManager.OnPlayerScoreLoaded += OnPlayerScoreLoaded;
OSXGameCenterManager.loadCurrentPlayerScore(leaderBoardId);

private void OnPlayerScoreLoaded (ISN_PlayerScoreLoadedResult result) {
    OSXGameCenterManager.OnPlayerScoreLoaded -= OnPlayerScoreLoaded;

	if(result.IsSucceeded) {
		GCScore score = result.loadedScore;
		OSXMessage.Create("Leader Board " + score.leaderboardId, "Score: " + score.score + "\n" + "Rank:" + score.rank);
	}

   Debug.Log("double score representation: " + score.GetDoubleScore());
   Debug.Log("long score representation: " + score.GetLongScore());
}

 

By default loadCurrentPlayerScoreLocal method will load ALL_TIME score from the GLOBAL score collection.  But you can always use another options:

public static void loadCurrentPlayerScore(string leaderBoradrId, GCBoardTimeSpan timeSpan = GCBoardTimeSpan.ALL_TIME, GCCollectionType collection = GCCollectionType.GLOBAL);

 

Note: It's recomended to always keep local copy of player sores using PlayerPrefs for example. In case Game Center data will be not avaliable due to broken internet connection, etc.

 

Loading other player scores

This can be useful if you want to create your own UI to display leaderboard scores in your game. For loading score list from the leaderboard you can use the loadScore method. As soon as requested scores are loaded, the OnScoresListLoaded action will be fired.

Example:

OSXGameCenterManager.OnScoresListLoaded += OnScoresListLoaded;
OSXGameCenterManager.loadScore(leaderBoardId, 1, 10, GCBoardTimeSpan.ALL_TIME, GCCollectionType.FRIENDS);

private void OnScoresListLoaded (ISN_Result res) {
	if(res.IsSucceeded) {
		Debug.Log("Scores loaded");
		loadedLeaderBoard = OSXGameCenterManager.GetLeaderBoard(leaderBoardId);
	} else  {
		Debug.Log("Failed to load scores");
	}
}

 

In the code above we trying to retrieve 10 results of the player friends starting from 1st plase. If the request succeeds, we can update our GUI with the loaded results. 

Here is example using Unity GUI:

GUI.Label(new Rect(10,  90, 100, 40), "rank", boardStyle);
GUI.Label(new Rect(100, 90, 100, 40), "score", boardStyle);
GUI.Label(new Rect(200, 90, 100, 40), "playerId", boardStyle);
GUI.Label(new Rect(400, 90, 100, 40), "name ", boardStyle);
GUI.Label(new Rect(550, 90, 100, 40), "avatar ", boardStyle);

loadedLeaderBoard = OSXGameCenterManager.GetLeaderBoard(leaderBoardId);
if(loadedLeaderBoard != null) {
	for(int i = 1; i < 10; i++) {
		GCScore score = loadedLeaderBoard.GetScore(i, GCBoardTimeSpan.ALL_TIME, diplayCollection);
		if(score != null) {
		    GUI.Label(new Rect(10,  90 + 70 * i, 100, 40), i.ToString(), boardStyle);
			GUI.Label(new Rect(100, 90 + 70 * i, 100, 40), score.GetLongScore().ToString() , boardStyle);
			GUI.Label(new Rect(200, 90 + 70 * i, 100, 40), score.playerId, boardStyle);


			OSXGameCenterPlayerTemplate player = OSXGameCenterManager.GetPlayerById(score.playerId);
			if(player != null) {
				GUI.Label(new Rect(400, 90 + 70 * i , 100, 40), player.alias, boardStyle);
				if(player.avatar != null) {
					GUI.DrawTexture(new Rect(550, 75 + 70 * i, 50, 50), player.avatar);
				} else  {
					GUI.Label(new Rect(550, 90 + 70 * i, 100, 40), "no photo ", boardStyle);
				}
			}

			if(GUI.Button(new Rect(650, 90 + 70 * i, 100, 30), "Chalange")) {
				OSXGameCenterManager.issueLeaderboardChallenge(leaderBoardId, "Your message here", score.playerId);
			}

		}

	}
}

 

Warning: When requesting score range, the start position should be > 1 and end range should  be <= 100.

OSXGameCenterManager.loadScore(leaderBoardId, 1, 10); // correct
OSXGameCenterManager.loadScore(leaderBoardId, 10, 50); // correct
OSXGameCenterManager.loadScore(leaderBoardId, 2, 20); // correct

OSXGameCenterManager.loadScore(leaderBoardId, 50, 150); // wrong
OSXGameCenterManager.loadScore(leaderBoardId, -5, 10); // wrong
OSXGameCenterManager.loadScore(leaderBoardId, 0, 101); // wrong

 

Getting leaderboard range

You can get the total scores number posted to the leaderboard as showed bellow:

string leaderBoardId =  "your.ios.leaderbord1.id";
GCLeaderboard lb =  OSXGameCenterManager.GetLeaderboard(leaderBoardId);
Debug.Log(lb.Info.MaxRange);

 

Warning: This property is invalid until a call to LoadCurrentPlayerScore or LoadScore  is completed. Afterward, it contains the total number of entries available to return to your game given the filters you applied to the query.

 

Full API use Example can be found in following example scenes:

GameCenterExamples

CustomLeaderBoardGUIExample