admin管理员组

文章数量:1530845

Unity接入Google登录

  • 接入FireBaseSdk和GoogleSignIn插件
    • FireBase接入文档
    • GoogleSignIn
  • 编写登录代码
  • 可能出现的问题
    • 运行时弹出Google登录界面,选了账号后没有反应
    • 出现DllNotFoundException: native-googlesignin
  • 参考
    • 2022.7.18 踩坑记录

接入FireBaseSdk和GoogleSignIn插件

FireBase接入文档

https://firebase.google/download/unity?authuser=0
接入FireBase时,FirebaseAuth.unitypackage必须接入,这是使用FireBase进行身份验证的入口

GoogleSignIn

https://github/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<GoogleSignInUser> signIn = GoogleSignIn.DefaultInstance.SignIn();

            signIn.ContinueWith(task =>
            {
                if (task.IsCanceled)
                {
                    Debug.Log("signIn.ContinueWith Login cancel");
                }
                else if (task.IsFaulted)
                {
                    Debug.Log("signIn.ContinueWith Login error : " + task.Exception.Message);
                }
                else
                {
                    var result = ((Task<GoogleSignInUser>) task).Result;
                    Credential credential =
                        Firebase.Auth.GoogleAuthProvider.GetCredential(result.IdToken,
                            null);
                    auth.SignInWithCredentialAsync(credential).ContinueWith (authTask => {
                        if (authTask.IsCanceled) {
                            Debug.LogError("SignInWithCredentialAsync was canceled.");
                        } else if (authTask.IsFaulted)
                        {
                            Debug.LogError("SignInWithCredentialAsync encountered an error: " + authTask.Exception);
                        } else
                        {
                            Debug.Log("Login Google success");
                            FirebaseUser newUser = authTask.Result;
                            Debug.Log("userid:" + newUser.UserId + "  koTen:");
                            newUser.TokenAsync(false).ContinueWith((t) =>
                            {
                                var toKen = t.Result;
                                Debug.Log("toKen=" + toKen);
                                string info = newUser.UserId + "/" + toKen;
                            });
                        
                        }
                    });
                }
            });
        }
    }

注意:webClient必须要有值,填写值为FireBase控制台上下载的配置文件中client_type == 3 的 oauth的值

可能出现的问题

运行时弹出Google登录界面,选了账号后没有反应

测试时使用国内机无法登录,在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/docs/auth/unity/google-signin?hl=zh&authuser=0
https://github/googlesamples/google-signin-unity
https://blog.csdn/Good_V/article/details/114528453

2022.7.18 踩坑记录

如果项目是用了多线程,那么原本登录时调用的 ContinueWith 需要改为 ContinueWithOnMainThread ,只在主线程调用,例如


如果有做Google Play服务检查,那么要在检查完毕后再调用FireBase的登录接口
检查Google Play服务代码

Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
            {
                var dependencyStatus = task.Result;
                Debug.Log("result--->" + dependencyStatus);
                //在这里可以添加一个标记,表示已经完成检查
                //checkOver = true
                if (dependencyStatus == Firebase.DependencyStatus.Available)
                {
                	//检查成功后需要做的事
                }
                else
                {
                    UnityEngine.Debug.LogError(System.String.Format(
                        "Could not resolve all Firebase dependencies: {0}", dependencyStatus));
                }
            });

本文标签: UnityGoogle