Unity 接入 Google 登录

unity 接入Google 登录

1、原生sdk 接入
打jar 包

  • 新建工程 empty activity
  • New modular 在lib 里面添加 class.jar 位于unity : Unity2018.2\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes 位置下
  • Add as library
  • 在mylibrary gradle 添加依赖
    implementation ‘com.google.android.gms:play-services-auth:16.0.1’
  • 添加任务
task deleteOldJar(type:Delete){
    delete 'release/GoogleSignIn.jar'
}

task exportJar(type:Copy){
    from('build/intermediates/packaged-classes/release')
    into('build/libs')
    include('classes.jar')
    rename('classes.jar','GoogleSignIn.jar')
}

exportJar.dependsOn(deleteOldJar,build)
  • 终端里面 : gradlew exportJar 导出jar包
  • SHA1 : keytool -list -v -store [path.keystore]
    2、导入unity
    Androidmainfest

    android:label="@string/app_name">
    
        
        
    
    

3、Export to androidstudio
Gradle 中
Maven 替换 Google()

repositories {
    jcenter()
    maven {
        url 'https://maven.google.com'
    }
}

添加 maven

allprojects {
    repositories {
        flatDir {
            dirs 'libs'
        }
        maven {
            url 'https://maven.google.com'
        }
    }
}

添加依赖

apply plugin: 'com.android.application'

dependencies {
    implementation 'com.google.android.gms:play-services-auth:16.0.1'
}

String.xml 里面添加
server_client_id
具体获取位置
https://console.developers.google.com/apis/credentials
server_client_id
4、具体Java代码

public void GoogleSignIn()
{
    String serverClientId = getString(R.string.server_client_id);
    Log.d("SDK","google sign in init");
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestServerAuthCode(serverClientId)
            .requestIdToken(serverClientId)
            .requestEmail()
            .build();

    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

    GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
    if(account == null) {
        Intent signInIntent = mGoogleSignInClient.getSignInIntent();
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
        // The Task returned from this call is always completed, no need to attach
        // a listener.
        Task task = GoogleSignIn.getSignedInAccountFromIntent(data);
        handleSignInResult(task);
    }
}

private void handleSignInResult(Task completedTask) {
    try {
        GoogleSignInAccount account = completedTask.getResult(ApiException.class);
        Toast.makeText(this, "Login Success", Toast.LENGTH_LONG).show();
        // Signed in successfully, show authenticated UI.
        //updateUI(account);
        String str = "";
        if(account != null)
            str = account.getDisplayName() +"|"+ account.getServerAuthCode()+"|"+ account.getIdToken()+"|"+account.getId();
        UnityPlayer.UnitySendMessage("Main Camera", "GetGoogleProfile", str);
    } catch (ApiException e) {
        // The ApiException status code indicates the detailed failure reason.
        // Please refer to the GoogleSignInStatusCodes class reference for more information.
        Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
        //updateUI(null);
        Toast.makeText(this, "Login Fail", Toast.LENGTH_LONG).show();
        Toast.makeText(this,String.valueOf(e.getStatusCode()), Toast.LENGTH_LONG).show();
    }
}

public void GoogleSignOut()
{
    mGoogleSignInClient.signOut()
            .addOnCompleteListener(this, new OnCompleteListener() {
                @Override
                public void onComplete(@NonNull Task task) {
                    // [START_EXCLUDE]
                    //updateUI(null);
                    // [END_EXCLUDE]
                    UnityPlayer.UnitySendMessage("Main Camera","OnGoogleSignOutComplete","Google Sign Out");
                }
            });
}

5、如果使用gradle 打包方式,使用Android studio 打包,可以不需要打Jar包,直接将.java 文件仍在unity中,到Android studio 会自动放在Java 文件夹下,也是可以直接调用的。

你可能感兴趣的:(笔记)