Skip to main content

Get Scores by Leaderboard ID

To get full scores list by LeaderboardId using the Leaderboard-Unity-SDK, follow the steps below.

Step 1: Initialize the Leaderboard Manager

1. Using Environment Variables

Automatically initialize MyriaLeaderboardSDKManager using environment variables.

Once all the required parameters are provided in the MyriaLeaderboardConfig object in the inspector, the MyriaLeaderboardSDKManager will automatically initialize based on the environment variables.

Import sample:

unity-install-step-10

2. Using the Init Function in Unity

To initialize the MyriaLeaderboardSDKManager, simply call the MyriaLeaderboardSDKManager.Init function in Unity. This will set up the SDK with the required configurations.

Sample code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MyriaLeaderboard;
using MyriaLeaderboard.Requests;
using System;

public class GamẹObjectTest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

MyriaLeaderboardSDKManager.Init("developerApiKey",
"publicRSAKey",
MyriaLeaderboard.MyriaLeaderboardEnums.MyriaEnvironment.Staging,
"1.0.0",
"https://game-server-url.com",
MyriaLeaderboardConfig.DebugLevel.All
);
}
}

MyriaLeaderboardSDKManager.Init(...) Parameters:

  • developerApiKey (string): The Developer API key provided by Myria Admin, required for authentication.
  • publicRSAKey (string): The RSA public key generated by your local server, used for encryption.
  • env (enum): Specify the environment for testing using the MyriaLeaderboard.MyriaLeaderboardEnums.MyriaEnvironmentenum. Recommend using Staging for Test environment and Prod for Live environment.
  • version (string): The version of the SDK you wish to specify.
  • gameServerUrl (string): The URL of your game server, either generated by Myria's template or your existing server.
  • debugLevelMode (string): Sets the debug level mode to control log visibility for troubleshooting in your game.

Step 2: Define Get List Score in the Leaderboard Parameters

The score list in the leaderboard are paginated, so you’ll need to specify the page number and the number of leaderboards to display per page.

Sample Code Scripts:

void Start()
{
MyriaLeaderboardSDKManager.GetScoreList("Leaderboard_id", page, item_per_pages, (response) =>
{
Debug.Log(response.data.items);
}, null, null);
}
  • leaderboardId (string): The unique identifier of the leaderboard to query all associated scores
  • page (string): The page number to retrieve the leaderboard data from.
  • item_per_pages (string): The number of leaderboard entries to display per page.

Step 3: Retrieve list of scores in the Leaderboard

Once you have defined the parameters, you can get the list scores of leaderboards by calling the GetListLeaderboard method on the MyriaLeaderboardSDKManager instance.

Sample Code in Editor:

unity_sdk_get_scores_leaderboard

Sample Code Scripts:

void Start()
{
MyriaLeaderboardSDKManager.GetScoreList("111", 1, 10, (response) =>
{
Debug.Log(response.data.items);
}, null, null);
}

Output Details:

The method returns a GetScoreDataResponse object containing the scores along with pagination metadata.

  • items (GetListScoreItemResponse[]): An array of score objects.
  • meta (object): Metadata about the pagination.
  • totalItems (number): The total number of items available.
  • itemCount (number): The number of items on the current page.
  • itemsPerPage (number): The number of items per page.
  • totalPages (number): The total number of pages available.
  • currentPage (number): The current page number.

Score Response Object (GetListScoreItemResponse)

  • id (number): The unique ID of the score entry.
  • userId (string): The ID of the user associated with the score.
  • leaderboardId (number): The ID of the leaderboard to which the score belongs.
  • period (number): The period in which the score was recorded.
  • score (number): The score value.
  • createdAt (string): The timestamp when the score was created.
  • updatedAt (string): The timestamp when the score was last updated.
  • rank (number): The rank of the user based on the score.

Summary

By following these steps, you can successfully get a score list in a leaderboard using the Leaderboard-Unity-SDK. This process involves initializing the LeaderboardManager, defining the score parameters, and invoking the postScore method to submit the score.

For more details on the available options and additional features, refer to the full documentation.