LootLocker本地玩家排名获取与显示问题求助
问题说明
游戏已成功展示全球排行榜(GlobalRank)的排名信息,但需要补充两个功能:
- 显示本地玩家在全球排行榜中的对应名次
- 在屏幕左下角展示本地玩家的排名
当前通过LootLocker获取排行榜数据时,无法正确获取本地玩家的排名,相关代码如下:
IEnumerator ShowScores() { yield return new WaitForSeconds(2); LootLockerSDKManager.GetScoreList(ID, maxScores, (response) => { if (response.success) { LootLockerLeaderboardMember[] scores = response.items; for (int i = 0; i < scores.Length; i++) { playerNames[i].text = (scores[i].member_id +""); playerScores[i].text = (scores[i].score +""); playerRank[i].text = (scores[i].rank + ""); //Rank of the localPlayer Rank.text = (scores["here_Should_Be_This_Player_ID"].rank + ""); LootLockerSDKManager.GetPlayerName // Entries[i].text = (scores[i].rank + ". " + scores[i].score + ". " + scores[i].member_id); } if (scores.Length < maxScores) { for (int i = scores.Length; i < maxScores; i++) { // Entries[i].text = (i + 1).ToString() + ". none"; } } } else { } }); }
解决办法
1. 先获取本地玩家的ID
在调用排行榜接口前,确保你已经拿到本地登录玩家的ID。LootLocker在玩家登录成功后会返回player_id,你可以在登录逻辑中将这个ID存入全局变量(比如localPlayerId)。
2. 遍历排行榜数据匹配本地玩家
获取到排行榜数组后,循环遍历每个条目,对比member_id与本地玩家ID,找到后更新左下角的Rank文本。如果遍历后未找到(说明玩家排名超出当前获取的排行榜范围),则调用LootLocker的单独接口获取玩家排名。
修改后的代码
// 全局变量存储本地玩家ID string localPlayerId; IEnumerator ShowScores() { yield return new WaitForSeconds(2); LootLockerSDKManager.GetScoreList(ID, maxScores, (response) => { if (response.success) { LootLockerLeaderboardMember[] scores = response.items; // 初始化本地玩家排名为未找到状态 Rank.text = "未上榜"; for (int i = 0; i < scores.Length; i++) { playerNames[i].text = scores[i].member_id; playerScores[i].text = scores[i].score.ToString(); playerRank[i].text = scores[i].rank.ToString(); // 匹配本地玩家ID,更新排名 if (scores[i].member_id == localPlayerId) { Rank.text = scores[i].rank.ToString(); } } // 处理排行榜条目不足的情况 if (scores.Length < maxScores) { for (int i = scores.Length; i < maxScores; i++) { playerNames[i].text = "暂无"; playerScores[i].text = "0"; playerRank[i].text = (i + 1).ToString(); } } // 若遍历未找到本地玩家,调用单独接口获取排名 if (Rank.text == "未上榜") { LootLockerSDKManager.GetMemberRank(ID, localPlayerId, (rankResponse) => { if (rankResponse.success) { Rank.text = rankResponse.rank.ToString(); } else { Rank.text = "无排名"; } }); } } else { Debug.Log("获取排行榜失败: " + response.Error); } }); }
关键说明
- 原代码中用
scores["here_Should_Be_This_Player_ID"]是错误的,因为scores是数组类型,只能通过索引访问,无法使用字符串键值。 - 玩家登录时需要将返回的ID存入
localPlayerId,示例登录逻辑:LootLockerSDKManager.StartGuestSession((response) => { if (response.success) { localPlayerId = response.player_id.ToString(); // 登录成功后调用排行榜展示逻辑 StartCoroutine(ShowScores()); } });
内容的提问来源于stack exchange,提问作者Mr.Schtelbe
相关产品推荐
相关产品推荐

