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 { 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()); } }