Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Is there a way to show a user's rank as a percentage?

Discussion in 'Leaderboards' started by DragonGalaxy, May 8, 2023.

  1. DragonGalaxy

    DragonGalaxy

    Joined:
    Jul 28, 2016
    Posts:
    4
    I want to show the user's current rank as a percentage with the formula (user's current rank/all users). Are there APIs that will make this possible?
     
  2. EmilF_Unity

    EmilF_Unity

    Unity Technologies

    Joined:
    Jun 10, 2022
    Posts:
    16
    Hi DragonGalaxy,

    thanks for looking into leaderboards! There is currently no single API for that exact information but the following two calls should do the trick:
    1. First retrieving a page of scores via the
      GetScoresAsync
      method in the SDK or else from the API directly (docs can be found here) will provide the total number of scores (aka the number of users in the leaderboard) as a field on the return object
    2. Then fetching the user's current rank via
      GetPlayerScoreAsync
      in the SDK or the API (docs)
    That would provide the inputs for the percentage formula. If you roughly know where the user currently is on the leaderboard, you might be able to use the limit and offset parameters on the first call to include the user in the returned page and skip the second call.

    Alternatively if the percentage does not need to be exact, you could use percentage-based tiers. For example by creating tiers in 5 or 10% intervals, and then the appropriate tier label will be returned along with the rank when the user score is fetched.

    Hope this helps, and let me know if there are any other questions :)
     
    ertbaran and IainUnity3D like this.
  3. DragonGalaxy

    DragonGalaxy

    Joined:
    Jul 28, 2016
    Posts:
    4
    GetScores method has a maximum limit of 1000. what if there are 100,000 users recorded on the leaderboard? Is there any way to know the number of users recorded on the leaderboard?
     
  4. EmilF_Unity

    EmilF_Unity

    Unity Technologies

    Joined:
    Jun 10, 2022
    Posts:
    16
    Hi DragonGalaxy,

    there is actually no need to fetch all the scores since the GetScores response object includes a dedicated
    total
    field which provides the total number of scores on the leaderboard. Fetching a single score by specifying the
    ?limit=1
    query parameter would be enough to provide that information.