锦标赛和排行榜快速入门
本快速入门介绍如何获得跟踪玩家高分的统计信息,以及如何获得最高分的排行榜。 这可用于全局排行榜或与 可重置统计信息 一起用于重置特定事件或锦标赛。
先决条件
玩家已登录 PlayFab。
步骤 1 - 创建统计信息和关联排行榜
在 Game Manager 中:
- 转到左侧菜单中的 排行榜。
- 选择“新建排行榜”。
- 在 统计信息名称 字段中,添加名为 HighScore 的 排行榜。
- 使用提供的下拉菜单,将 重置频率 字段设置为 手动。
- 移动到 聚合方法 字段,然后从提供的下拉菜单中选择“最高值”(始终使用最高值)。
第 2 步 - 使用玩家的高分更新统计信息
可以从客户端使用 UpdatePlayerStatistics 前,必须在 API 功能 中启用它。
- 从游戏的设置菜单中选择关闭“游戏设置”。
- 选择“API 功能”选项卡。
- 选中“允许客户端发布玩家统计信息 框。 注意:一般情况下,此选项不应用于实时游戏,因为它对客户端的授权高于所提交的值。 这仅适用于不担心玩家可能在统计信息中作弊的情况。 如果需要保护统计信息,则应仅通过服务器授权操作(如云脚本或自定义游戏服务器)进行更新。
- 单击屏幕底部的“保存”按钮。
C# 代码示例 - SubmitScore
在此代码示例中,将有一个在游戏结束时调用的 SubmitScore
函数。
public void SubmitScore(int playerScore) {
PlayFabClientAPI.UpdatePlayerStatistics(new UpdatePlayerStatisticsRequest {
Statistics = new List<StatisticUpdate> {
new StatisticUpdate {
StatisticName = "HighScore",
Value = playerScore
}
}
}, result=> OnStatisticsUpdated(result), FailureCallback);
}
private void OnStatisticsUpdated(UpdatePlayerStatisticsResult updateResult) {
Debug.Log("Successfully submitted high score");
}
private void FailureCallback(PlayFabError error){
Debug.LogWarning("Something went wrong with your API call. Here's some debug information:");
Debug.LogError(error.GenerateErrorReport());
}
第 3 步 - 请求高分排行榜
为了获取玩过该游戏的所有玩家的最高分排行榜,将对 GetLeaderboard 进行调用。
C# 代码示例 - RequestLeaderboard
在这个代码示例中,将有一个 RequestLeaderboard
函数,该函数将用于获取排行榜,并将结果传递给 DisplayLeaderboard
函数,并由后者写入游戏中显示高分的体验。
//Get the players with the top 10 high scores in the game
public void RequestLeaderboard() {
PlayFabClientAPI.GetLeaderboard(new GetLeaderboardRequest {
StatisticName = "HighScore",
StartPosition = 0,
MaxResultsCount = 10
}, result=> DisplayLeaderboard(result), FailureCallback);
}
private void FailureCallback(PlayFabError error){
Debug.LogWarning("Something went wrong with your API call. Here's some debug information:");
Debug.LogError(error.GenerateErrorReport());
}