Unity中Google Play Services排行榜用户ID映射及当前用户分数定位问题
Unity Google Play排行榜匹配当前用户分数问题
问题描述
在Unity中使用Google Play Services插件开发排行榜时,调用PlayGamesPlatform.Instance.LoadScores()加载数据后,发现data.PlayerScore.userID(纯数字格式)与data.Scores列表中分数的userID(带a_前缀格式)不一致。已知排行榜中仅有当前用户的分数,需要找到列表中对应当前用户的分数。
代码示例
PlayGamesPlatform.Instance.LoadScores( type, start, count, LeaderboardCollection.Public, timespan, data => { Debug.Log("GOOGLE USERID " + PlayGamesPlatform.Instance.GetUserId()); // GOOGLE USERID 116254439162396242728 Debug.Log("GOOGLELOCAL USERID " + PlayGamesPlatform.Instance.localUser.id); // GOOGLELOCAL USERID 116254439162396242728 Debug.Log("PLAYERSCORE USERID " + data.PlayerScore.userID); // PLAYERSCORE USERID 116254439162396242728 foreach (var score in data.Scores) { Debug.Log("SCORE USERID " + score.userID); // SCORE USERID a_2248181595467244665 } });
解决方案
方法1:直接使用PlayerScore
data.PlayerScore本身就是当前用户的分数数据(日志中其userID与当前用户ID完全一致),且排行榜中仅有你的分数,直接使用该对象即可,无需遍历列表:
// 直接获取当前用户分数 long currentUserScore = data.PlayerScore.value; // 自定义展示逻辑 Debug.Log("当前用户分数:" + currentUserScore);
方法2:从Scores列表中匹配
如果必须从data.Scores列表中筛选,不要使用score.userID(这是带前缀的公开用户ID),而是通过score.user.id(与localUser.id一致的内部玩家ID)进行匹配:
PlayGamesPlatform.Instance.LoadScores( type, start, count, LeaderboardCollection.Public, timespan, data => { string currentUserId = PlayGamesPlatform.Instance.localUser.id; foreach (var score in data.Scores) { // 用内部玩家ID匹配 if (score.user.id == currentUserId) { Debug.Log("找到当前用户分数:" + score.value); // 执行自定义展示逻辑 break; } } });
原因说明
Google Play Services插件返回的Score对象包含两种用户ID:
score.userID:公开用户标识符,用于对外展示,格式带前缀(如a_xxxx)score.user.id:内部玩家ID,与PlayGamesPlatform.Instance.localUser.id格式一致(纯数字),是用于身份匹配的正确字段
内容的提问来源于stack exchange,提问作者Kretess
相关产品推荐
相关产品推荐

