Skip to content

Instantly share code, notes, and snippets.

@koster
Last active June 7, 2024 09:39
Show Gist options
  • Save koster/ff73ff7274801c87206cb9083d6530a4 to your computer and use it in GitHub Desktop.
Save koster/ff73ff7274801c87206cb9083d6530a4 to your computer and use it in GitHub Desktop.
Example implementation of a leaderboard manager using Playfab
using System.Collections;
using System.Collections.Generic;
using PlayFab;
using PlayFab.ClientModels;
using UnityEngine;
public class PlayfabLeaderboardManager : MonoBehaviour
{
private bool isConnected = false;
void Start()
{
LoginToPlayFab();
}
private void LoginToPlayFab()
{
PlayFabSettings.staticSettings.TitleId = "YourTitleId"; // Replace with your actual Title ID
var request = new LoginWithCustomIDRequest { CustomId = username or some other type of id, CreateAccount = true };
PlayFabClientAPI.LoginWithCustomID(request, OnLoginSuccess, OnLoginFailure);
}
private void OnLoginSuccess(LoginResult result)
{
Debug.Log("Successfully connected to PlayFab.");
isConnected = true;
GetLeaderboardFromPlayFab(); // Fetch leaderboard immediately after login
}
private void OnLoginFailure(PlayFabError error)
{
Debug.LogError("Error connecting to PlayFab: " + error.GenerateErrorReport());
}
public void SubmitScore(int score)
{
if (!isConnected)
{
Debug.LogWarning("Not connected to PlayFab, attempting to connect before submitting score...");
StartCoroutine(WaitForConnection(() => SubmitScoreToLeaderboard(score)));
}
else
{
SubmitScoreToLeaderboard(score);
}
}
private IEnumerator WaitForConnection(System.Action onConnected)
{
while (!isConnected)
{
yield return new WaitForSeconds(1);
}
onConnected?.Invoke();
}
private void SubmitScoreToLeaderboard(int score)
{
var request = new UpdatePlayerStatisticsRequest
{
Statistics = new List<StatisticUpdate>
{
new StatisticUpdate { StatisticName = "HighScores", Value = score }
}
};
PlayFabClientAPI.UpdatePlayerStatistics(request, OnScoreSubmitSuccess, OnScoreSubmitFailure);
}
private void OnScoreSubmitSuccess(UpdatePlayerStatisticsResult result)
{
Debug.Log("Score submitted successfully!");
}
private void OnScoreSubmitFailure(PlayFabError error)
{
Debug.LogError("Failed to submit score: " + error.GenerateErrorReport());
}
public void SetUsername(string username)
{
var request = new UpdateUserTitleDisplayNameRequest { DisplayName = username };
PlayFabClientAPI.UpdateUserTitleDisplayName(request, OnUsernameSetSuccess, OnUsernameSetFailure);
}
private void OnUsernameSetSuccess(UpdateUserTitleDisplayNameResult result)
{
Debug.Log("Username updated successfully to: " + result.DisplayName);
}
private void OnUsernameSetFailure(PlayFabError error)
{
Debug.LogError("Failed to update username: " + error.GenerateErrorReport());
}
public void GetLeaderboard()
{
if (!isConnected)
{
Debug.LogWarning("Not connected to PlayFab, attempting to connect before retrieving leaderboard...");
StartCoroutine(WaitForConnection(GetLeaderboardFromPlayFab));
}
else
{
GetLeaderboardFromPlayFab();
}
}
private void GetLeaderboardFromPlayFab()
{
var request = new GetLeaderboardAroundPlayerRequest
{
StatisticName = "HighScores",
MaxResultsCount = 10
};
PlayFabClientAPI.GetLeaderboardAroundPlayer(request, OnLeaderboardGetSuccess, OnLeaderboardGetFailure);
}
private void OnLeaderboardGetSuccess(GetLeaderboardAroundPlayerResult result)
{
foreach (var item in result.Leaderboard)
{
Debug.Log($"Rank: {item.Position} | ID: {item.PlayFabId} | Score: {item.StatValue}");
}
}
private void OnLeaderboardGetFailure(PlayFabError error)
{
Debug.LogError("Failed to retrieve leaderboard: " + error.GenerateErrorReport());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment