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 ID
WebClientId = "*******************************************"
};
}
/// <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");
}
});