Unity Google Android登录接入

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();
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值