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.

Question How can i filter leaders from Country?

Discussion in 'Leaderboards' started by doublehitgames, Sep 11, 2023.

  1. doublehitgames


    Mar 5, 2016
    I noticed that we don't have this native option, correct?
    Therefore, how can I show players from a specific country, and/or global, in the ranking, without filtering countries? Mybe using Tiers? IDK
  2. restush96


    May 28, 2019
    There is no way to get country. But there is one method I know however, it's quite expensive. Here are the steps

    Service that we need: Leaderboard, Cloud Code, Cloud Save

    - In client side, when player sign in for first time, prompt `Select your country`
    - In client side, save player country info to Cloud Save
    - Make Cloud Code with parameter LeaderboardID
    - In Cloud Code, write code to get all user of LeaderboardID, the return user will have PlayerID. Check their API Doc
    - In Cloud Code, create class `PlayerData` to hold data PlayerID and Country
    - In Cloud Code, create array to hold PlayerData
    - In Cloud Code, foreach PlayerIDs to get `Country` variable from Cloud Save, if you found it, then create new `PlayerData` and assignee PlayerID and Country then push to array we created.
    - In Cloud Code, serialize the array of `PlayerData` to JSON then return it
    - In client side, make a C# class that contain prop PlayerID and Country
    - In client side, try call the cloud code method with param `LeaderboardID` that we created where it will return JSON, then deserialize by `JsonConvert.DeserializeObject`
    - Foreach the return value and filter which country that you want
    - Finish

    To improve performance, need to cache after we get the leaderboard value, so it will blazing fast.