Blog

13 Jan 2021

Cross platform Leaderboard API - What is it?

Our cross platform leaderboard API package stores and fetches scores from our server into your app. With 3 Lines of code are you ready to go. 


Support

Join our discord server or use our contact form.


How to get started with the Leaderboard API Package in Unity.

Just download our asset from the Unity Asset Store or directly here (download Unity Leaderboard Component) and import it into your Unityproject.

Import the prefab named “LB_Controller” in your MainScene. It is a singleton-instance and will not be destroyed if you load another scene.

Go to ​https://apps.mw-systems.com/register and register for free. Login and follow the instructions to create your leaderboard. On the overview of your leaderbaords you can see your API-KEY.



Copy and paste the API-KEY into the “LB-Controller” - Script. Also enter your board ID here. 



Your setup is done!


Upload a new Score

Anywhere in your own scripts call our method to upload the users score.


LB_Controller.instance.StoreScore(1283, "Hoang_532”); // parameters -> score, username

Parameters: 

  • score -> the score of the latest game

  • username -> the username, displayed on your leaderboard



  1. Receive Scores

    There are 2 simple options to receive your leaderboard-entries. 


    First you have to download your scores: 

    After the download your entries are cached locally. 


    LB_Controller.instance.ReloadLeaderboard();

    1st method: You can access your entries via:

    LB_Entry[] entries = LB_Controller.instance.Entries();


    2nd method: The smart way is to listen for updated scores:

    using UnityEngine;

    public class DemoScriptUpload : MonoBehaviour
    {

        // Start is called before the first frame update
        void Start() {
            LB_Controller.OnUpdatedScores += OnLeaderboardUpdated;
            DownloadScores();
        }

        private void DownloadScores() {
            LB_Controller.instance.ReloadLeaderboard(1); // parameter -> board-id
        }

        private void OnLeaderboardUpdated(LB_Entry[] entries) {
            foreach (LB_Entry entry in entries) {
                // here you can fill your List on your UI
                Debug.Log("Rank: " + entry.rank + "; Name: " + entry.name + "; Points: " + entry.points);
            }
        }

        private void OnDestroy() {
            LB_Controller.OnUpdatedScores -= OnLeaderboardUpdated;
        }
    }


    Rank for User

    Retrieve a rank for a user is also simple. 

    Where ever you need to know the rank for an user just call this function: 


    int rank = LB_Controller.instance.GetRankForUser("Markus_0815"); //pass the username as string

    It will return the rank of the requested user as an integer. If no user is found it will return 0. 

    Change Username

    To change a username implement this functions. 

    using UnityEngine;

    public class DemoChangename : MonoBehaviour
    {

        // Start is called before the first frame update
        void Start() {
          LB_Controller.instance.ChangeUsername("Mia_0815", "Mia_0816"); // parameters -> oldUsername, newUsername LB_Controller.OnUsernameChangedFinished += OnUsernameChaged;
        }

        private void OnUsernameChaged(LB_ChangeUsernameResult result, LB_Entry[] entries) { switch (result) { case LB_ChangeUsernameResult.OK: // reload your board and / or show a successmessage OnLeaderboardUpdated(entries); break; case LB_ChangeUsernameResult.USER_NOT_FOUND: //show an error to the user break; case LB_ChangeUsernameResult.USERNAME_ALREADY_TAKEN: //show an error to the user break; default: // do your fallback stuff here break; } }
    private void OnDestroy() { LB_Controller.OnUsernameChangedFinished -= OnUsernameChaged; } }