Unity:Firebase接入Google登录
Unity:Firebase接入Google登錄
- 開啟Firebase的登錄方式
- 問題小結
- Google登錄代碼
- 調用登錄代碼
- 參考文章:
此文章只是粗淺之作,記錄而已,有錯望指出,不勝感激
開啟Firebase的登錄方式
進入Firebase控制臺==>Authentication ==> Sign-in method ==>Google
設置項目名稱,郵件,
最主要的是設置Web SDK配置
這個Web客戶端ID是首先在Google配置之后自動分發的,在以下網址配置:
配置谷歌應用信息
選擇你的項目:
填寫你的包名,keystore的SHA1值
將這個給你的Client ID填寫到Firebase中,同時,在代碼的初始化中也填寫這個Client ID
問題小結
1、當我把Signin的Unity的包導入項目后發現自己的部分代碼不能用了,可以把Parse文件刪除再重新導入,這部分在后面的參考文章處有。2、DllNotFoundException: Unable to load DLL ‘native-googlesignin’: The specified module could not be found.
在項目中進入到GoogleSignIn\Editor\m2repository\com\google\signin\google-signin-support\1.0.4目錄中,將所有后綴為.srcaar的文件更改為后綴.aar,也就是說把"src”刪除即可,參考網址在后面
3、當我用IOS登錄時調用了Google的登錄頁面,有顯示選擇Google賬戶,但是之后就再無反應,不清楚是哪里的問題,后面查資料有說是因為在2021年初Google禁止了網頁的登錄請求,所以導致IOS的Google登錄已經無法使用了,我這邊就沒有繼續下去,如果有大佬知道原因,希望給出指點!
ok,這樣就可以登錄到Google了
Google登錄代碼
using Firebase.Auth; using Google; using System; using System.Collections; using System.Collections.Generic; using System.Threading.Tasks; using UnityEngine;/// <summary> /// 谷歌登錄 /// </summary> public class GoogleLoginManager : SingleTon<GoogleLoginManager> {string idToken;TaskCompletionSource<FirebaseUser> signInCompleted;FirebaseAuth auth;public bool IsInit{get => GoogleSignIn.Configuration != null;}/// <summary>/// 谷歌登錄初始化配置/// </summary>public void Init(){GoogleSignIn.Configuration = new GoogleSignInConfiguration{RequestIdToken = true,// Copy this value from the google-service.json file.// oauth_client with type == 3//填入在配置谷歌項目SHA1值時給你的Client IDWebClientId = "*******************************************"};}/// <summary>/// 谷歌登錄/// </summary>public void Login(Action<bool> action = null){Debuger.Log("Enter Google Script Login Method");Task<GoogleSignInUser> signIn = GoogleSignIn.DefaultInstance.SignIn();signInCompleted = new TaskCompletionSource<FirebaseUser>();signIn.ContinueWith(task =>{if (task.IsCanceled){signInCompleted.SetCanceled();}else if (task.IsFaulted){signInCompleted.SetException(task.Exception);}else{idToken = ((Task<GoogleSignInUser>)task).Result.IdToken;auth = Firebase.Auth.FirebaseAuth.DefaultInstance;action?.Invoke(true);}});}/// <summary>/// 谷歌登錄到Firebase/// </summary>/// <param name="action"></param>public void LoginToFirebase(Action<string> action){if (signInCompleted == null || string.IsNullOrEmpty(idToken) || auth == null){return;}Credential credential = Firebase.Auth.GoogleAuthProvider.GetCredential(idToken, null);auth.SignInWithCredentialAsync(credential).ContinueWith(authTask =>{if (authTask.IsCanceled){if (LoginResultManager.Instance != null)LoginResultManager.Instance.OpenLoginResult(false);signInCompleted.SetCanceled();}else if (authTask.IsFaulted){if (LoginResultManager.Instance != null)LoginResultManager.Instance.OpenLoginResult(false);signInCompleted.SetException(authTask.Exception);}else{signInCompleted.SetResult(((Task<FirebaseUser>)authTask).Result);Firebase.Auth.FirebaseUser newUser = authTask.Result;Debuger.Log(String.Format("User Login Successful : {0} ({1})", newUser.DisplayName, newUser.UserId));action?.Invoke(newUser.UserId);}});} }調用登錄代碼
GoogleLoginManager.Instance.SignIn((isLogin) =>{if (isLogin){GoogleLoginManager.Instance.LoginToFirebase((UserId) =>{//登錄到Firebase后的操作});}else{Debuger.LogError($"Login is Fail");}});參考文章:
GitHub社區,介紹的也比較詳細了
鏈接: https://github.com/googlesamples/google-signin-unity
導入Signin的Unity包時報錯
鏈接: https://github.com/googlesamples/google-signin-unity/issues/140
Firebase接入Google的官方指導網址:
鏈接: https://firebase.google.com/docs/auth/unity/google-signin?authuser=0
DllNotFoundException:無法加載DLL’native-googlesignin’:找不到指定的模塊:
鏈接: https://github.com/googlesamples/google-signin-unity/issues/106
總結
以上是生活随笔為你收集整理的Unity:Firebase接入Google登录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何自学大数据开发?
- 下一篇: Tikz学习笔记(一)