- 接入FireBaseSdk和GoogleSignIn插件
- FireBase接入文档
- GoogleSignIn
- 编写登录代码
- 可能出现的问题
- 运行时弹出Google登录界面,选了账号后没有反应
- 出现DllNotFoundException: native-googlesignin
- 参考
https://firebase.google.com/download/unity?authuser=0
接入FireBase时,FirebaseAuth.unitypackage必须接入,这是使用FireBase进行身份验证的入口
https://github.com/googlesamples/google-signin-unity
编写登录代码public FirebaseAuth auth;
// Start is called before the first frame update
void Start()
{
GoogleSignIn.Configuration = new GoogleSignInConfiguration()
{
RequestIdToken = true,
//此处填写从FireBase控制台上下载的配置文件中client_id client_type == 3 的 oauth的值
WebClientId = ""
};
}
private void OnGUI()
{
if (GUI.Button(new Rect(2000,300,100,100), "Google"))
{
Debug.Log("GoogleLogin()");
auth = FirebaseAuth.DefaultInstance;
Task signIn = GoogleSignIn.DefaultInstance.SignIn();
TaskCompletionSource signInCompleted = new TaskCompletionSource();
signIn.ContinueWith(task =>
{
if (task.IsCanceled)
{
Debug.Log("signIn.ContinueWith Login cancel");
signInCompleted.SetCanceled();
}
else if (task.IsFaulted)
{
Debug.Log("signIn.ContinueWith Login error : " + task.Exception.Message);
signInCompleted.SetException(task.Exception);
}
else
{
var result = ((Task) task).Result;
Credential credential =
Firebase.Auth.GoogleAuthProvider.GetCredential(result.IdToken,
null);
auth.SignInWithCredentialAsync(credential).ContinueWith (authTask => {
if (authTask.IsCanceled) {
Debug.LogError("SignInWithCredentialAsync was canceled.");
signInCompleted.SetCanceled();
} else if (authTask.IsFaulted)
{
Debug.LogError("SignInWithCredentialAsync encountered an error: " + authTask.Exception);
signInCompleted.SetException(authTask.Exception);
} else
{
Debug.Log("Login Google success");
signInCompleted.SetResult(((Task) authTask).Result);
FirebaseUser newUser = authTask.Result;
Debug.Log("DisplayName:" + newUser.DisplayName + " provierId:" + newUser.ProviderId
+" userid:" + newUser.UserId + " newUserid:" + newUser.TokenAsync(false).Id
+ " koTen:" + newUser.TokenAsync(false).Result);
}
});
}
});
}
}
注意:webClient必须要有值,填写值为FireBase控制台上下载的配置文件中client_type == 3 的 oauth的值
测试时使用国内机无法登录,在android studio中查看日志会看到有报错
Failed to validate DexClassLoader. java.lang.ClassNotFoundException: Didn’t find class “com.google.android.gms.chimera.DynamiteModuleInitializer”
但是使用XiaoMi 9T海外机时可以正常登录,可能时因为Play服务版本问题造成
出现DllNotFoundException: native-googlesignin不使用package包导入,下载github上的demo用例,直接把用例上的文件复制粘贴过来
参考https://firebase.google.com/docs/auth/unity/google-signin?hl=zh&authuser=0
https://github.com/googlesamples/google-signin-unity
https://blog.csdn.net/Good_V/article/details/114528453



