Skip to main content

Get Leaderboards

To get leaderboard list for developer 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 Leaderboard Parameters

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

Method:

void Start()
{
MyriaLeaderboardSDKManager.GetListLeaderboard(page, item_per_pages, (response) =>
{
Debug.Log(response.data);
});`
}
  • 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 Leaderboards

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

Sample Code in Editor:

unity_sdk_get_leaderboards

Sample Code Scripts:

void Start()
{
MyriaLeaderboardSDKManager.GetListLeaderboard(1, 20, (response) =>
{
Debug.Log(response.data.meta.itemsPerPage);
});
}

Output Details:

The response from getLeaderboards is an APIResponseType object containing pagination data and the list of leaderboards:

export interface APIResponseType<T> {
status: string;
data: T;
errors?: any;
}

export interface CommonPaginateDataTypes<T> {
items: T;
meta: {
totalItems: number;
itemCount: number;
itemsPerPage: number;
totalPages: number;
currentPage: number;
};
}

export interface LeaderboardData {
name: string;
description: string;
updateScoreStrategy: string;
gameName: string;
totalPeriods?: number;
livePeriodInDays: number;
availableAt: string;
enableMetadata?: boolean;
developerId?: string;
status: string;
currentPeriod: number;
dataPersistenceInDays: number;
expireAt: string;
updatedAt?: string;
createdAt?: string;
id: number;
enablePeriodRefresh?: boolean;
}

Example Output:

{
"status": "success",
"data": {
"items": [
{
"name": "Leaderboard 1",
"description": "Test leaderboard 1",
"updateScoreStrategy": "ACCUMULATE",
"gameName": "Test Game",
"livePeriodInDays": 30,
"availableAt": "2024-01-01T00:00:00Z",
"status": "active",
"currentPeriod": 1,
"dataPersistenceInDays": 365,
"expireAt": "2025-01-01T00:00:00Z",
"id": 1
}
],
"meta": {
"totalItems": 1,
"itemCount": 1,
"itemsPerPage": 10,
"totalPages": 1,
"currentPage": 1
}
}
}

Summary

By following these steps, you can successfully get the leaderboard list 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.