当前位置:   article > 正文

Unity 谷歌登录 Android + iOS_unity 谷歌授权登录

unity 谷歌授权登录

准备官方插件

https://github.com/googlesamples/google-signin-unity

Android

  1. 需要web_clientId
  2. 需要 google-services.json (应该是接Firebase需要)(谷歌官网下载的)在这里插入图片描述
    在这里插入图片描述
代码部分:
	 if (GoogleSignIn.Configuration == null)
	 {
	     GoogleSignIn.Configuration = new GoogleSignInConfiguration()
	     {
	         WebClientId = //写你app的web_client_id,
	         UseGameSignIn = false,
	         RequestIdToken = true,
	         RequestEmail = true
	     };
	 }
	 try
	 {
	     var user = await GoogleSignIn.DefaultInstance.SignIn();
	     //user有你需要的用户的数据
	 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

iOS

开发设备:window
发布设备:Mac
C#登录代码:同Android

unity打包准备

  1. 需要GoogleService-Info.plist(谷歌官网下载的)文件,确保里面有CLIENT_ID在这里插入图片描述

  2. 在Unity的打包后处理OnPreprocessBuild里,需要对info.plist文件进行修改,代码如下:
    在这里插入图片描述

    // 增加info.plist文件内容
    var plistPath = Path.Combine(pathToBuiltProject, "Info.plist");
    var plist = new PlistDocument();
    plist.ReadFromFile(plistPath);
	plist.root.SetString("GIDClientID", /*GoogleService-Info.plist里的CLIENT_ID*/);
    // 下面步骤增加需要是为了让iOS允许第三方登录,不然会报错
    PlistElementArray urlTypes = null;
    if (plist.root.values.ContainsKey("CFBundleURLTypes"))
    {
        try
        {
            urlTypes = plist.root.values["CFBundleURLTypes"] as PlistElementArray;
        }
        catch (Exception e)
        {
            Debug.LogWarning(string.Format("error,Could not obtain CFBundleURLTypes PlistElementArray: {0}", e.Message));
        }
    }
    if (urlTypes == null)
    {
        urlTypes = plist.root.CreateArray("CFBundleURLTypes");
    }
    PlistElementDict dic = urlTypes.AddDict();
    dic.SetString("CFBundleURLName","google-sign-in");
    PlistElementArray schemes = dic.CreateArray("CFBundleURLSchemes");
    /// 这里加的是GoogleService-Info.plist里的CLIENT_ID的倒转
    schemes.AddString("com.googleusercontent.apps.1xxxxxxxxxxxxx4-exxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxg");

    plist.WriteToFile(plistPath);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

MAC电脑内配置

【从window打包xcode项目到Mac需要经过一步cocopod的配置过程,此过程是利用cocopod来对谷歌插件依赖的管理,不然项目build不通过, 步骤如下:】
(注意以下步骤是在Mac电脑上进行的)

  1. 确保你的MAC电脑安装了Cocopod

  2. 打开终端,输入cd 你的项目根目录

  3. 输入pod install
    在这里插入图片描述

  4. 上述步骤完成后,会多一个.workspace的文件,通过点击这个文件打开项目,不要通过点击 .xcodeproj打开。此时你会看到xcode两个文件夹,一个是你的项目,一个是Pod

以上完成后,就可以拉起iOS的谷歌登录

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/706984
推荐阅读
相关标签
  

闽ICP备14008679号