![](/img/trans.png)
[英]How to load scores for current user from multiple leaderboard using Google Play Games services?
[英]Unity Android game Can sign in using Google Play Games Plugin, but unable to load scores from the leaderboard
在输入此问题之前,我进行了很多搜索(在此问题上停留了2周:'(),我的目标只是Unity项目中的Google页首横幅功能。我按照以下步骤操作:
1-集成的Google Play游戏插件,如以下网址所述: https : //github.com/playgameservices/play-games-plugin-for-unity
我使用的密钥库与生成SHA1代码的密钥库相同。 我在播放器设置及其密码中输入了它。
我的软件包名称在所有平台上都是一致的(在Google api控制台,google play开发者控制台,在任何地方都统一)
我在控制台中将测试用户输入到测试用户。
2-我在启动画面场景中的脚本之一中输入了以下验证代码:
void Start () {
#if UNITY_ANDROID
ConfigureGooglePlay ();
SignIn ();
#endif
}
void ConfigureGooglePlay(){
PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
.Build();
PlayGamesPlatform.InitializeInstance(config);
PlayGamesPlatform.DebugLogEnabled = true;
PlayGamesPlatform.Activate();
}
void SignIn () {
Social.localUser.Authenticate (success => {
if (success) {
Debug.Log ("Authentication successful");
string userInfo = "Username: " + Social.localUser.userName +
"\nUser ID: " + Social.localUser.id +
"\nIsUnderage: " + Social.localUser.underage;
Debug.Log (userInfo);
// DisplayGPScore();
} else
Debug.Log ("Authentication failed");
});
}
3-在游戏场景中,在游戏结束时,我使用ReportScore功能将用户的高分发布到排行榜
private void OnGameOver(){
...
PostScore(_gm.highScore);
..
DisplayGPScore ();
}
internal void LoadUsersAndDisplay(ILeaderboard lb)
{
// get the user ids
List<string> userIds = new List<string>();
foreach(IScore score in lb.scores) {
userIds.Add(score.userID);
}
// load the profiles and display (or in this case, log)
Social.LoadUsers(userIds.ToArray(), (users) =>
{
string status = "Leaderboard loading: " + lb.title + " count = " +
lb.scores.Length;
foreach(IScore score in lb.scores) {
Debug.Log("" + score.rank + ":" + score.userID);
}
Debug.Log(status);
});
}
public void DisplayGPScore(){
ILeaderboard lb = PlayGamesPlatform.Instance.CreateLeaderboard();
lb.id = ZRayGameManager.WORLD_LEADERBOARD_ID;
lb.LoadScores(ok =>
{
if (ok) {
LoadUsersAndDisplay(lb);
}
else {
Debug.Log("Error retrieving leaderboardi");
}
});
}
public void PostScore(int score){
PlayGamesPlatform.Instance.ReportScore(score, ZRayGameManager.WORLD_LEADERBOARD_ID, (bool success) => {
if(success){
Debug.Log("High score " + score + " posted");
}else{
Debug.Log("High score " + score + " could not be posted");
}
});
}
我简化了代码(不相关的部分除外)
我得到以下输出
I/Unity (15970):
I/Unity (15970): Authentication successful
I/Unity (15970):
I/Unity (15970): (Filename: ./artifacts/generated/common/runtime/UnityEngineDe
bugBindings.gen.cpp Line: 37)
I/Unity (15970):
I/Unity (15970): Username: RighthandedTuba240
I/Unity (15970): User ID: g02637898988054749692
I/Unity (15970): IsUnderage: True
I/Unity (15970):
I/Unity (15970): (Filename: ./artifacts/generated/common/runtime/UnityEngineDe
bugBindings.gen.cpp Line: 37)
I/Unity (15970):
I/Unity (15970): [Play Games Plugin DLL] 07/17/16 18:46:52 +03:00 DEBUG: Repo
rtScore: score=100, board=CgkIppHPm-wIEAIQCQ
I/Unity (15970):
I/Unity (15970): (Filename: ./artifacts/generated/common/runtime/UnityEngineDe
bugBindings.gen.cpp Line: 37)
I/Unity (15970):
I/Unity (15970): [Play Games Plugin DLL] 07/17/16 18:46:52 +03:00 DEBUG: Nati
ve Submitting score: 100 for lb CgkIppHPm-wIEAIQCQ with metadata:
I/Unity (15970):
I/Unity (15970): (Filename: ./artifacts/generated/common/runtime/UnityEngineDe
bugBindings.gen.cpp Line: 37)
I/Unity (15970):
I/Unity (15970): User is authenticated
I/Unity (15970):
I/Unity (15970): (Filename: ./artifacts/generated/common/runtime/UnityEngineDe
bugBindings.gen.cpp Line: 37)
I/Unity (15970):
I/Unity (15970): [Play Games Plugin DLL] 07/17/16 18:46:52 +03:00 DEBUG: Load
Scores, board=GooglePlayGames.PlayGamesLeaderboard callback is System.Action`1[S
ystem.Boolean]
I/Unity (15970):
I/Unity (15970): (Filename: ./artifacts/generated/common/runtime/UnityEngineDe
bugBindings.gen.cpp Line: 37)
I/Unity (15970):
I/Unity (15970): [Play Games Plugin DLL] 07/17/16 18:46:52 +03:00 DEBUG: Invo
king user callback on game thread
I/Unity (15970):
I/Unity (15970): (Filename: ./artifacts/generated/common/runtime/UnityEngineDe
bugBindings.gen.cpp Line: 37)
I/Unity (15970):
I/Unity (15970): High score 100 posted
I/Unity (15970):
I/Unity (15970): (Filename: ./artifacts/generated/common/runtime/UnityEngineDe
bugBindings.gen.cpp Line: 37)
I/Unity (15970):
I/Unity (15970): [Play Games Plugin DLL] 07/17/16 18:46:55 +03:00 DEBUG: Ente
ring internal callback for LeaderboardManager#InternalFetchCallback
I/Unity (15970):
I/Unity (15970): (Filename: ./artifacts/generated/common/runtime/UnityEngineDe
bugBindings.gen.cpp Line: 37)
I/Unity (15970):
I/Unity (15970): [Play Games Plugin DLL] 07/17/16 18:47:10 +03:00 DEBUG: Star
ting Auth Transition. Op: SIGN_OUT status: ERROR_NOT_AUTHORIZED
I/Unity (15970):
I/Unity (15970): (Filename: ./artifacts/generated/common/runtime/UnityEngineDe
bugBindings.gen.cpp Line: 37)
I/Unity (15970):
I/Unity (15970): [Play Games Plugin DLL] 07/17/16 18:47:10 +03:00 DEBUG: Ente
ring internal callback for LeaderboardManager#InternalFetchSummaryCallback
I/Unity (15970):
I/Unity (15970): (Filename: ./artifacts/generated/common/runtime/UnityEngineDe
bugBindings.gen.cpp Line: 37)
I/Unity (15970):
W/Unity (15970): !!! [Play Games Plugin DLL] 07/17/16 18:47:10 +03:00 WARNING:
Error returned from fetchScoreSummary: GooglePlayGames.Native.PInvoke.FetchScor
eSummaryResponse
W/Unity (15970):
W/Unity (15970): (Filename: ./artifacts/generated/common/runtime/UnityEngineDe
bugBindings.gen.cpp Line: 37)
W/Unity (15970):
I/Unity (15970): [Play Games Plugin DLL] 07/17/16 18:47:10 +03:00 DEBUG: Invo
king user callback on game thread
I/Unity (15970):
I/Unity (15970): (Filename: ./artifacts/generated/common/runtime/UnityEngineDe
bugBindings.gen.cpp Line: 37)
I/Unity (15970):
I/Unity (15970): Error retrieving leaderboardi
结论:
用户可以进行身份验证,因为方法Sign In()告知身份验证成功并打印出用户信息。
用户可以将他/她的分数发布到计分板上,因为PostScore方法显示“已发布高分100”
用户无法显示计分板分数,错误为
W /团结(15970):!!! [Play游戏插件DLL] 07/17/16 18:47:10 +03:00警告:fetchScoreSummary返回的错误:GooglePlayGames.Native.PInvoke.FetchScor eSummaryResponse
我在控制台中找不到计分板的任何设置,无法授予用户对此计分板的访问权限等。
请帮忙!! 我在这里停留了2周(但是有所改善,我无法在3天前登录)
要显示页首横幅,您需要调用startActivityForResult
并传递意图以创建默认的页首横幅UI。 在以下代码段中, REQUEST_LEADERBOARD
是请求代码的任意整数。
startActivityForResult(Games.Leaderboards.getLeaderboardIntent(mGoogleApiClient,
LEADERBOARD_ID), REQUEST_LEADERBOARD);
要获得玩家的分数:
Private void loadScoreOfLeaderBoard() {
Games.Leaderboards.loadCurrentPlayerLeaderboardScore(getApiClient(), getString(R.string.your_leaderboard_id), LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback(new ResultCallback<Leaderboards.LoadPlayerScoreResult>() {
@Override
public void onResult(final Leaderboards.LoadPlayerScoreResult scoreResult) {
if (isScoreResultValid(scoreResult)) {
// here you can get the score like this
mPoints = scoreResult.getScore().getRawScore();
}
}
});
}
private boolean isScoreResultValid(final Leaderboards.LoadPlayerScoreResult scoreResult) {
return scoreResult != null && GamesStatusCodes.STATUS_OK == scoreResult.getStatus().getStatusCode() && scoreResult.getScore() != null;
}
SIGN_OUT status: ERROR_NOT_AUTHORIZED
确保您拥有正确的SHA1指纹 。 由于与Google Play服务集成,因此Android上的授权请求与其他授权工作流程不同。
使用OAuth 2.0访问Google API时,所有应用程序都遵循基本模式。 在较高的级别,您遵循四个步骤:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.