新版本的GMS的api和老版本的有很大的差异,刚接了一下,在这里留一个记号,以便查阅:
判定是否已经登录
private static boolean isSignedIn(Cocos2dxActivity cocos2dxActivity) { return GoogleSignIn.getLastSignedInAccount(cocos2dxActivity) != null; }
登录GameCenter
public static void loginGameCenter(Cocos2dxActivity cocos2dxActivity) { Log.d(TAG, "signInSilently()"); mGoogleSignInClient = GoogleSignIn.getClient(cocos2dxActivity, new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN).build()); if(!isSignedIn(cocos2dxActivity)){ startSignInIntent(cocos2dxActivity); } else { //静默登录 mGoogleSignInClient.silentSignIn().addOnCompleteListener(cocos2dxActivity, new OnCompleteListener<GoogleSignInAccount>() { @Override public void onComplete(@NonNull Task<GoogleSignInAccount> task) { if (task.isSuccessful()) { Log.d(TAG, "signInSilently(): success"); } else { Log.d(TAG, "signInSilently(): failure", task.getException()); } } }); } }
登录按钮点击之后调用:
private static void startSignInIntent(Cocos2dxActivity cocos2dxActivity) { GoogleSignInClient signInClient = GoogleSignIn.getClient(cocos2dxActivity, GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN); Intent intent = signInClient.getSignInIntent(); cocos2dxActivity.startActivityForResult(intent, RC_SIGN_IN); }
提交分数到排行榜
public static void uploadScore(String leaderboard_id,long value,Cocos2dxActivity cocos2dxActivity){ if(!isSignedIn(cocos2dxActivity)){ return; } Games.getLeaderboardsClient(cocos2dxActivity, GoogleSignIn.getLastSignedInAccount(cocos2dxActivity)) .submitScore(leaderboard_id, value); }
//显示排行榜
public static void showLeaderboard(String leaderboard_id, final Cocos2dxActivity cocos2dxActivity){ Games.getLeaderboardsClient(cocos2dxActivity, GoogleSignIn.getLastSignedInAccount(cocos2dxActivity)) .getLeaderboardIntent(leaderboard_id) .addOnSuccessListener(new OnSuccessListener<Intent>() { @Override public void onSuccess(Intent intent) { cocos2dxActivity.startActivityForResult(intent, 9004); } }); }
参考
https://developers.google.cn/identity/sign-in/android/?hl=zh-cn