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
<activity
    android:name="com.lekaiyou.tienlentest.MainActivity">
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <meta-data android:name="unityplayer.UnityActivity" android:value="true"/>
</activity>

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<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        handleSignInResult(task);
    }
}

private void handleSignInResult(Task<GoogleSignInAccount> 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<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> 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 文件夹下,也是可以直接调用的。

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页