Unity Android 游戏可以使用 Google Play 游戏插件登录,但无法从排行榜加载分数

Posted

技术标签:

【中文标题】Unity Android 游戏可以使用 Google Play 游戏插件登录,但无法从排行榜加载分数【英文标题】:Unity Android game Can sign in using Google Play Games Plugin, but unable to load scores from the leaderboard 【发布时间】:2016-11-20 06:40:49 【问题描述】:

在输入这个问题之前,我搜索了很多(在这个问题上卡了 2 周:'() 我只针对我的 Unity 项目中的谷歌排行榜功能。我按照以下步骤操作:

1- 集成的 google play 游戏插件,如下所述:https://github.com/playgameservices/play-games-plugin-for-unity

在我的 google play 开发者控制台中创建了一个 google play services 游戏条目,并将其链接到我的应用程序(在同一个控制台中),包 ID 为 com.o6systems.zray,该程序包处于开放 alpha 测试阶段。 我创建了信用凭证并将密钥库/密钥对的 SHA1 插入到 google api 控制台。 我创建了成就、1 个记分牌并将我的资源输入到 Window/GooglePlayGames/Setup/android 设置中

我使用的密钥库与生成 SHA1 代码的密钥库相同。我在播放器设置和密码中输入了它。

我的包名在所有平台上都是一致的(在google api控制台,google play开发者控制台中,在unity中无处不在)

我在控制台中将我的测试用户输入到测试用户中。

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/Unity (15970): !!! [Play 游戏插件 DLL] 07/17/16 18:47:10 +03:00 警告: 从 fetchScoreSummary 返回错误:GooglePlayGames.Native.PInvoke.FetchScor eSummaryResponse

我在控制台中找不到记分牌的任何设置,让用户可以访问此记分牌等。

请帮忙!!我在这里卡了 2 周(但有改善,3 天前我无法登录)

【问题讨论】:

您好 fercis,您还在遇到这个问题吗? 【参考方案1】:

要显示排行榜,您需要调用startActivityForResult 并传入intent 以创建默认排行榜UI。在下面的代码 sn-p 中,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 fingerprint。由于与Google Play services 的集成,Android 上的授权请求不同于其他授权工作流。

在使用 OAuth 2.0 访问 Google API 时,所有应用程序都遵循基本模式。在高层次上,您遵循四个步骤:

从 Google API 控制台获取 OAuth 2.0 凭据。 从 Google 授权服务器获取访问令牌。 将访问令牌发送到 API。 如有必要,请刷新访问令牌。

【讨论】:

您好,感谢您的回复,但我在 Unity 工作,它使用 C# 编写脚本并为 Android 构建游戏。您的解决方案看起来像是一种 Android - Java Android Studio。我检查了很多次 SHA 指纹:/

以上是关于Unity Android 游戏可以使用 Google Play 游戏插件登录,但无法从排行榜加载分数的主要内容,如果未能解决你的问题,请参考以下文章

Unity Android 游戏可以使用 Google Play 游戏插件登录,但无法从排行榜加载分数

Android 和 Unity 3D 游戏开发

unity3d 怎样发布android程序

Unity集成华为游戏服务SDK方式:Unity导出为Android工程

如何在Unity3d游戏中检索Android安装引荐来源网址

怎么修改unity入口androidmainfest