a.以下链接是Google 登录unity sdk 插件文档 直接导入即可
https://github.com/googlesamples/google-signin-unity/releases
b.以下是YouTube上参考视频,如果找不到可以在YouTube 上搜索‘firebase google sign in unity’
YouTube视频: https://www.youtube.com/watch?v=K-5ZtjCP3r8&t=505s
演示APK: https://www.dropbox.com/s/zo5zretv56w5mxz/signindemo.apk?dl=0
登录完整脚本:https://www.dropbox.com/s/qss2wk4v52d48j4/GoogleSignInDemo.cs?dl=0
完整项目:
https://www.dropbox.com/s/0fbg9lxchvhwe5j/Google%20Sign%20In%20Demo.zip?dl=0
c.以下是注意事项:
Google Clud中 凭证 里面有个 OAuth2.0客户端ID,需要有个Android客户端id ,web客户端无法再手机登录。如下图
地址:https://console.cloud.google.com/apis/credentials?project=mass-bf4f7
PPS:如果需要接入Firebase 支持多种登录,可以参考(b)项目,该视频里面包含如何使用
using Google;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
/// <summary>
/// Google 登录
///
/// </summary>
public class GoogleLoginInSDK : MonoBehaviour
{
public string webClientId = "753894345350-pbqn5htaekhq8jcbctg6n5tgj55qvmd6.apps.googleusercontent.com";
private GoogleSignInConfiguration configuration;
// Start is called before the first frame update
void Start()
{
configuration = new GoogleSignInConfiguration { WebClientId = webClientId, RequestEmail = true, RequestIdToken = true };
}
/// <summary>
/// 登录
/// </summary>
public void SignInWithGoogle()
{
Debug.Log("Calling SignIn");
GoogleSignIn.Configuration = configuration;
GoogleSignIn.Configuration.UseGameSignIn = false;
GoogleSignIn.Configuration.RequestIdToken = true;
GoogleSignIn.DefaultInstance.SignIn().ContinueWith(OnAuthenticationFinished);
}
internal void OnAuthenticationFinished(Task<GoogleSignInUser> task)
{
// if it failed, then show the error. Else continue with firebase.
Debug.Log("OnAuthenticationFinished:1" + (task.IsFaulted) + " " + task.IsCanceled);
if (task.IsFaulted)
{
Debug.LogError(task.Exception.Message);
using (IEnumerator<Exception> enumerator = task.Exception.InnerExceptions.GetEnumerator())
{
var _bo = enumerator.MoveNext();
if (_bo)
{
GoogleSignIn.SignInException error = (GoogleSignIn.SignInException)enumerator.Current;
}
else
{
Debug.Log("Got Unexpected Exception?!?" + task.Exception);
}
}
}
else if (task.IsCanceled)
{
Debug.Log("Canceled");
}
else
{
Debug.Log("Welcome: " + task.Result.DisplayName + "!");
Debug.Log("Email = " + task.Result.Email);
Debug.Log("Google ID Token = " + task.Result.IdToken);
Debug.Log("Email = " + task.Result.Email);
Debug.Log(task.Result.IdToken);
}
}
/// <summary>
/// 登出
/// </summary>
public void SignOutFromGoogle()
{
Debug.Log("Calling SignOut");
GoogleSignIn.DefaultInstance.SignOut();
}
}