使用 LibGDX 进行 Google 登录

Google Sign-In with LibGDX(使用 LibGDX 进行 Google 登录)

本文介绍了使用 LibGDX 进行 Google 登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题.我正在用 LibGDX 制作游戏.现在我想实现谷歌登录.我到处搜索,但找不到任何东西.

I've a problem. I'm making a game with LibGDX. Now I want to implement Google Sign-In. I searched everywhere, but can't find anything.

我需要一个解析器来抽象特定平台的代码,但我不知道该怎么做.有人可以帮忙吗?

What I need is a Resolver to abstract code for specific platform, but I don't know how to do it. Can someone help?

编辑

这是代码,这是我的 Android 解析器:

Here's the code, this is my Android Resolver:

public GoogleResolverAndroid(final Context context) {

    handler = new Handler();
    this.context = context;

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();

    mGoogleApiClient = new GoogleApiClient.Builder(this.context)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
}
@Override
public void loginGoogle() {
    signIn();
}

@Override
public boolean getIsLoggedInGoogle() {
    return isLoggedIn;
}

public void signIn() {

    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    ((AndroidLauncher)context).startActivityForResult(signInIntent, RC_SIGN_IN);
    mGoogleApiClient.connect();

}

private void signOut() {
    Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
            new ResultCallback<Status>() {
                @Override
                public void onResult(Status status) {
                    isLoggedIn = false;
                }
            });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        handleSignInResult(result);
    }

    if (requestCode == REQUEST_RESOLVE_ERROR) {
        mResolvingError = false;
        if (resultCode == RESULT_OK) {
            // Make sure the app is not already connected or attempting to connect
            if (!mGoogleApiClient.isConnecting() &&
                    !mGoogleApiClient.isConnected()) {
                mGoogleApiClient.connect();
            }
        }
    }
    } 
//some code other code
//
//
//




@Override
public void onConnected(@Nullable Bundle bundle) {

    OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
    if (opr.isDone())
    {
        Gdx.app.debug(TAG, "Loggato");
        GoogleSignInResult result = opr.get();
        handleSignInResult(result);
    } else {
        opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
            @Override
            public void onResult(GoogleSignInResult googleSignInResult) {
                handleSignInResult(googleSignInResult);
            }
        });
    }
}

@Override
public void onConnectionSuspended(int i) {
    Gdx.app.debug(TAG, "onConnectionSuspended ma non so perchè");
}

这是我在 libgdx 中调用解析器方法的类

and this is my class that call the resolver method in libgdx

       // Google
    googleLoginButton = new LoginButton(tbs, stage, main);
    googleLoginButton.setPosition(stage.getViewport().getWorldWidth()/2-googleLoginButton.getWidth() - 10,
            stage.getViewport().getWorldHeight()/2-googleLoginButton.getHeight()/2 - 200);

    googleLoginButton.addListener(new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            main.googleResolver.loginGoogle();
            googlePrefs = main.googleResolver.getGooglePrefs();

            gLoginIn = true;

            Gdx.app.debug(TAG, googlePrefs.toString());
        }
    });

推荐答案

我为我的游戏解决这个问题的方法是使用 接口.因此,您必须编写 Android 和 iOS 特定代码,在核心游戏中使用此接口.

The way I solved this for my game was with the use of Interfacing. So you have to write Android and iOS specific code that use this interfaces from your core game.

这篇关于使用 LibGDX 进行 Google 登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 LibGDX 进行 Google 登录

基础教程推荐