Submit a player's high score to a PlayFab Leaderboard in Unity using C#.
public static void Post_High_Score(int score)
{
Debug.Log("Post_High_Score");
#if UNITY_EDITOR
Debug.Log("Dont post to leaderboards in Unity Editor");
return;
#endif
var leaderboard_title = "High Scores";
try
{
if (PlayFabClientAPI.IsClientLoggedIn())
{
Debug.Log("Player Logged In");
PlayFabClientAPI.UpdatePlayerStatistics(new UpdatePlayerStatisticsRequest
{
Statistics = new List {
new StatisticUpdate {
StatisticName = leaderboard_title,
Value = score
}
}
}, result => OnStatisticsUpdated(result), FailureCallback);
}
else
{
Debug.Log("Player Not Logged In");
}
}
catch (System.Exception ex)
{
Debug.Log(ex);
}
}
static void OnStatisticsUpdated(UpdatePlayerStatisticsResult updateResult)
{
Debug.Log("Successfully submitted high score");
}
static void FailureCallback(PlayFabError error)
{
Debug.LogWarning("Something went wrong with your API call. Here's some debug information:");
Debug.LogError(error.GenerateErrorReport());
}