基本接入流程参考大佬的接入教程,感谢大佬!
导入谷歌登录插件包
插件包地址
导入后打包时会在Plugins-Android里下载超多依赖包,但是打包时候会报错,基本都是gradle的错误,依赖包重复。经测试甚至全部删除后再打包也能正常唤起谷歌登录界面,推荐设置
文件名问题
有几个文件后缀名有问题 改一下后缀名,其中Google-signin-support-1.0.4.aar文件改名后勾选一下安卓平台
无法唤起谷歌登录界面
如果打包后无法唤起谷歌登录界面,并且在log中看到类似如下错误
Caused by: java.lang.ClassNotFoundException: com.google.android.gms.common.api.PendingResult
尝试保留几个依赖包com.google.android.gams.play开头的依赖包
WebClientId
需要在谷歌云后台设置OAuth客户端ID,需要注意的是,即便要打包安卓APK,也不能仅创建一个安卓端的ID,至少要三个,俩安卓的,一个web的,其中web的客户端ID才是代码里填的那个!
两个安卓的ID创建时要填SHA1值,其中一个来自本地keystore文件,另一个来自谷歌开发者后台上传aab包后自动创建的
基础代码
#region 谷歌登录
string webClientId = "";
GoogleSignInConfiguration configuration;
public void BindGoogle() // 绑定谷歌
{
UnityEngine.Debug.Log("<color=yellow><b> +++++++ 绑定谷歌 </b></color>");
if (configuration == null)
{
configuration = new GoogleSignInConfiguration
{
WebClientId = webClientId,
RequestIdToken = true,
RequestEmail = true,
};
}
GoogleSignIn.Configuration = configuration;
GoogleSignIn.Configuration.UseGameSignIn = false;
GoogleSignIn.Configuration.RequestIdToken = true;
GoogleSignIn.DefaultInstance.SignIn().ContinueWith(OnAuthenticationFinished);
}
internal void OnAuthenticationFinished(Task<GoogleSignInUser> task)
{
if (task.IsFaulted)
{
using (IEnumerator<System.Exception> enumerator = task.Exception.InnerExceptions.GetEnumerator())
{
if (enumerator.MoveNext())
{
GoogleSignIn.SignInException error = (GoogleSignIn.SignInException)enumerator.Current;
UnityEngine.Debug.Log("<color=yellow><b> +++++++ 登录失败 错误状态: " + error.Status + " 错误信息: " + error.Message + "</b></color>");
}
else
UnityEngine.Debug.Log("<color=yellow><b> +++++++ 登录失败 未知错误:" + task.Exception + "</b></color>");
}
configuration.UseGameSignIn = false;
}
else if (task.IsCanceled)
{
configuration.UseGameSignIn = false;
UnityEngine.Debug.Log("<color=yellow><b> +++++++ 登录失败 用户取消</b></color>");
}
else
{
UnityEngine.Debug.Log("<color=yellow><b> +++++++ 登录成功 用户邮箱" + task.Result.Email + "</b></color>");
}
}
#endregion