第一步:设置google play console,添加排行榜
第二步:设置android 工程对应参数
第三步:设置测试模拟器(选择真机测试,略过此步骤,注意android系统要带有google服务,一般选择三星的手机)
第四步:添加登录账号、上传分数、显示排行榜功能代码
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.games.AnnotatedData;
import com.google.android.gms.games.Games;
import com.google.android.gms.games.LeaderboardsClient;
import com.google.android.gms.games.PlayersClient;
import com.google.android.gms.games.leaderboard.LeaderboardScore;
import com.google.android.gms.games.leaderboard.LeaderboardVariant;
import com.google.android.gms.games.leaderboard.Leaderboards;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import org.cocos2dx.lib.Cocos2dxActivity;
protected void onCreate(Bundle savedInstanceState) {
...
...
...
// DO OTHER INITIALIZATION BELOW
this_tmp = this;
}
// 排行榜
private static final String TAG = "GFB";
private static AppActivity this_tmp;
//登录google账号
public static void loginGameCenter() {
Log.d(TAG, "signInSilently()");
// Create the client used to sign in to Google services.
GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(this_tmp,
new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN).build());
mGoogleSignInClient.silentSignIn().addOnCompleteListener(this_tmp,
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());
}
}
});
}
//提交分数到排行榜
public static void uploadScore(String leaderboard_id,int value){
Games.getLeaderboardsClient(this_tmp, GoogleSignIn.getLastSignedInAccount(this_tmp))
.submitScore(leaderboard_id, value);
}
//显示排行榜
public static void showLeaderboard(String leaderboard_id){
Games.getLeaderboardsClient(this_tmp, GoogleSignIn.getLastSignedInAccount(this_tmp))
.getLeaderboardIntent(leaderboard_id)
.addOnSuccessListener(new OnSuccessListener<Intent>() {
@Override
public void onSuccess(Intent intent) {
this_tmp.startActivityForResult(intent, 9004);
}
});
}
//排行榜分数
private static int score;
//得到排行榜数据
public static void getScoreData(String leaderboard_id){
Games.getLeaderboardsClient(this_tmp, GoogleSignIn.getLastSignedInAccount(this_tmp))
.loadCurrentPlayerLeaderboardScore(
leaderboard_id, LeaderboardVariant.TIME_SPAN_ALL_TIME,
LeaderboardVariant.COLLECTION_PUBLIC)
.addOnSuccessListener(this_tmp, new OnSuccessListener<AnnotatedData<LeaderboardScore>>(){
public long score_tmp;
@Override
public void onSuccess(AnnotatedData<LeaderboardScore> leaderboardScoreAnnotatedData) {
if (leaderboardScoreAnnotatedData != null) {
if (leaderboardScoreAnnotatedData.get() != null) {
score_tmp = leaderboardScoreAnnotatedData.get().getRawScore();
score = (int) score_tmp;
Log.d(TAG,"score_tmp"+String.valueOf(score));
}
}
}
}
);
}
//得到排行榜分数
public static int getScore()
{
Log.d(TAG,"score"+String.valueOf(score));
return score;
}