You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.6 KiB
C#
83 lines
2.6 KiB
C#
using System.Collections.Generic;
|
|
using PlayFab;
|
|
using PlayFab.ClientModels;
|
|
using UnityEngine;
|
|
|
|
public class PlayFabLeaderboards : MonoBehaviour
|
|
{
|
|
public static string DisplayName;
|
|
public void UpdateLevelsCompleted(int levelsCompleted)
|
|
{
|
|
var request = new UpdatePlayerStatisticsRequest
|
|
{
|
|
Statistics = new List<StatisticUpdate>
|
|
{
|
|
new StatisticUpdate
|
|
{
|
|
StatisticName = GameConstants.LevelCompletedStatsKey,
|
|
Value = levelsCompleted
|
|
}
|
|
}
|
|
};
|
|
PlayFabClientAPI.UpdatePlayerStatistics(request, OnStatisticsUpdateSuccess, OnStatisticsUpdateFailure);
|
|
}
|
|
|
|
private void OnStatisticsUpdateSuccess(UpdatePlayerStatisticsResult result)
|
|
{
|
|
Debug.Log("Successfully updated player statistics!");
|
|
}
|
|
|
|
private void OnStatisticsUpdateFailure(PlayFabError error)
|
|
{
|
|
Debug.LogError("Failed to update player statistics: " + error.GenerateErrorReport());
|
|
}
|
|
|
|
[ContextMenu("GetLeaderboards")]
|
|
public void GetLeaderboard()
|
|
{
|
|
var request = new GetLeaderboardRequest
|
|
{
|
|
StatisticName = GameConstants.LevelCompletedStatsKey,
|
|
StartPosition = 0, // Start at the top of the leaderboard
|
|
MaxResultsCount = 10 // Get top 10 players
|
|
};
|
|
|
|
PlayFabClientAPI.GetLeaderboard(request, OnGetLeaderboardSuccess, OnGetLeaderboardFailure);
|
|
}
|
|
|
|
private void OnGetLeaderboardSuccess(GetLeaderboardResult result)
|
|
{
|
|
foreach (var entry in result.Leaderboard)
|
|
{
|
|
Debug.Log($"Player: {entry.PlayFabId}, Position: {entry.Position}, Levels Completed: {entry.StatValue}");
|
|
// You can display this data in your game UI
|
|
}
|
|
}
|
|
|
|
private void OnGetLeaderboardFailure(PlayFabError error)
|
|
{
|
|
Debug.LogError("Failed to get leaderboard: " + error.GenerateErrorReport());
|
|
}
|
|
|
|
public void SetDisplayName(string displayName)
|
|
{
|
|
var request = new UpdateUserTitleDisplayNameRequest
|
|
{
|
|
DisplayName = displayName
|
|
};
|
|
|
|
PlayFabClientAPI.UpdateUserTitleDisplayName(request, OnDisplayNameUpdateSuccess, OnDisplayNameUpdateFailure);
|
|
DisplayName = displayName;
|
|
}
|
|
|
|
private void OnDisplayNameUpdateSuccess(UpdateUserTitleDisplayNameResult result)
|
|
{
|
|
Debug.Log("Display name set successfully: " + result.DisplayName);
|
|
}
|
|
|
|
private void OnDisplayNameUpdateFailure(PlayFabError error)
|
|
{
|
|
Debug.LogError("Failed to set display name: " + error.GenerateErrorReport());
|
|
}
|
|
|
|
} |