Skip to main content

Post Scores

To post a score to a leaderboard for players and users 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 PostScore data in the Leaderboard Parameters

The Post Scores method allows you to submit a list of score entries for multiple players to the leaderboard in a single request.

Sample Code Scripts:

void Start()
{
var item = new ItemPostScore
{
score = scoreValue, userId = "userId",
displayName = "userDisplayName", username = "userName"
};
PostScoreParams paramPostScore = new PostScoreParams { items = new ItemPostScore[] { item } };

MyriaLeaderboardSDKManager.PostScore("leaderboardID", paramPostScore, (response) =>
{
Debug.Log("response.score" + response.data);
});
}

Step 3: Post the Score

Once you have defined the score parameters, you can post the score by calling the PostScore method on the MyriaLeaderboardSDKManager instance.

Sample Code in Editor:

unity_sdk_post_scores

Sample Code Scripts:

    void Start()
{
var item = new ItemPostScore
{
score = 10, userId = "12", displayName = "TestPlayer",
username = "TestPlayer"
};
PostScoreParams paramPostScore = new PostScoreParams { items = new ItemPostScore[] { item } };

MyriaLeaderboardSDKManager.PostScore("111", paramPostScore, (response) =>
{
Debug.Log("response.score" + response.data);
});
}

Output Details:

The response from the postScore method will include the following fields:

PostScoreResponseData Class:

  • success: PostScoreDataResponse[] Contains an array of scores that were successfully posted to the leaderboard.

  • failed: PostScoreDataResponse[] Contains an array of scores that failed to post to the leaderboard.

PostScoreDataResponse Class:

  • id: number The unique identifier for the posted score.

  • score: number The score that was posted.

  • userId: string The unique identifier of the user.

  • username: string The username of the user.

  • leaderboardId: number The identifier of the leaderboard to which the score was posted.

  • period: number The period during which the score was posted.

  • updatedAt: string The timestamp when the score was last updated.

  • createdAt: string The timestamp when the score was created.

Summary

By following these steps, you can successfully post a score to 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.