在游戏中添加Google Play Games Leaderboard 排行榜

做了一款安卓小游戏,想让用户和全世界的玩家pk分数,如果自己建一个服务器收集分数再做排行太麻烦了,毕竟只是个小游戏,想找第三方sdk,之前发现SwarmConnect貌似有这个功能,但是这个官网和文档都太不友好,后来发现google play就有leaderboard排行榜功能,那就开始接入吧。

Play后台配置及获取参数

在google play console配置排行榜

  • 配置游戏信息:应用描述、截图等基本信息,与普通应用配置一致,这里略去
  • 配置排行榜:进入排行榜,点击创建排行榜,根据实际需求配置,比如按分数从高到低或是从低到高排序。创建成功之后会生成leaderboard_id用于上报分数及显示排行榜接口中使用。
    image.png
  • 配置测试用户:进入Play游戏服务---测试用户数量中添加测试人员google账号gmail邮箱,这里需要注意,并非在“测试---开放式测试、封闭式测试、内部测试”中配置。只有配置了测试用户的账号才能参与测试。
    image.png

配置用户登录信息

  • 创建凭证:用于使用google账号登录游戏使用
    image.png
  • 新建OAuth 2.0 client客户端:

    创建凭据

    创建凭据

    这是创建好的页面,在创建时一定要注意指纹为软件包签名文件的sha1码,可使用命令keytool -v -list -keystore xxx.jks获取结果中的sha1码。软件包名称一定要与软件的实际包名相符。
    填写凭据信息

  • 关联client与google play 中的游戏:创建完成后点击刷新OAuth客户端即可从列表中选择与之关联。

代码调用

gradle引入相应sdk

用户使用leaderboard功能必须先要登录google账号,并且需要安装有google games(用户登录时,若没安装,sdk会引导玩家先安装),所以要同时集成leaderboard(集成在play-services-games)和登录的功能

implementation 'com.google.android.gms:play-services-games:21.0.0' //排行榜
implementation 'com.google.android.gms:play-services-auth:19.0.0' //登录

调用登录接口(必须先登录才能使用leaderboard)

    //优先尝试静默登录,静默登录不会弹出登录确认框,对玩家体验更好
    public void signInSilently() {
        GoogleSignInOptions signInOptions = GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN;
        final GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(mCtx);
        if (GoogleSignIn.hasPermissions(account, signInOptions.getScopeArray())) {
            // Already signed in.
            // The signed in account is stored in the 'account' variable.
            GoogleSignInAccount signedInAccount = account;
        } else {
            // Haven't been signed-in before. Try the silent sign-in first.
            GoogleSignInClient signInClient = GoogleSignIn.getClient(mCtx, signInOptions);
            signInClient
                    .silentSignIn()
                    .addOnCompleteListener(
                            new OnCompleteListener() {
                                @Override
                                public void onComplete(@NonNull Task task) {

                                    if (task.isSuccessful()) {
                                        // The signed in account is stored in the task's result.
                                        GoogleSignInAccount signedInAccount = task.getResult();
                                    } else {
                                        // Player will need to sign-in explicitly using via UI.
                                        // See [sign-in best practices](http://developers.google.com/games/services/checklist) for guidance on how and when to implement Interactive Sign-in,
                                        // and [Performing Interactive Sign-in](http://developers.google.com/games/services/android/signin#performing_interactive_sign-in) for details on how to implement
                                        // Interactive Sign-in.
                                        // 若静默登录失败,采用带界面的显式登录
                                         googleSignIn()
                                    }
                                }

                            });
        }
    }

// 调用google显式登录
    private void googleSignIn() {
        GoogleSignInClient signInClient = GoogleSignIn.getClient(this,
                GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN);
        Intent intent = signInClient.getSignInIntent();
        this.startActivityForResult(intent, RC_SIGN_IN);
    }

上报分数到leaderboard

    public void submitScore(int score) {
        GoogleSignInAccount lastSignedInAccount = GoogleSignIn.getLastSignedInAccount(mCtx);
        if (lastSignedInAccount == null) {
            ToastUtil.showToast(mCtx, mCtx.getString(R.string.submit_score_signin_toast));
            signInSilently();
            return;
        }
        //leaderboard_id即为创建排行榜时生成的id
        Games.getLeaderboardsClient(mCtx, lastSignedInAccount)
                .submitScore(mCtx.getString(R.string.leaderboard_id), score);
    }

展示排行榜

public void showLeaderboard(final Activity activity) {
        GoogleSignInAccount lastSignedInAccount = GoogleSignIn.getLastSignedInAccount(mCtx);
        if (lastSignedInAccount == null) {
            ToastUtil.showToast(mCtx, mCtx.getString(R.string.submit_score_signin_toast));
            signInSilently();
            return;
        }
        //leaderboard_id即为创建排行榜时生成的id
        Games.getLeaderboardsClient(mCtx, lastSignedInAccount)
                .getLeaderboardIntent(mCtx.getString(R.string.leaderboard_id))
                .addOnSuccessListener(new OnSuccessListener() {
                    @Override
                    public void onSuccess(Intent intent) {
                        activity.startActivityForResult(intent, RC_LEADERBOARD_UI);
                    }
                });
    }

你可能感兴趣的:(在游戏中添加Google Play Games Leaderboard 排行榜)