接入環(huán)境
1、unity版本:2021.3.21f1
特別說明:通過Unityhub安裝的Unity,需要安裝對應(yīng)版本所需的JDK、SDK、NDK,我們默認(rèn)使用Unity自帶的,不需要使用自己下載的,否則可能會導(dǎo)致打包失敗的問題。
2、google登錄sdk版本:GoogleSignIn_v1.0.4.1
特別說明:
(1)GoogleSignIn官方插件地址是:GoogleSignIn,但是這個版本目前有些問題,IOS打包報(bào)錯,因?yàn)镮OS部分代碼沒有更新。
(2)所以我們使用別人解決完了的版本:無bug版GoogleSignIn_v1.0.4.1,這里面有文檔可以看
(3)可以直接通過這個地址下載unitypackage包導(dǎo)入自己項(xiàng)目:GoogleSignIn_v1.0.4.1.unitypackage,這個文件需要下載。GoogleSignIn_v1.0.4.1.unitypackage需要導(dǎo)入自己項(xiàng)目。
3、安卓依賴管理插件EDM4U:EDM4U下載地址,這個插件需要下載,external-dependency-manager-1.2.175.unitypackage需要導(dǎo)入自己項(xiàng)目。
開始接入
創(chuàng)建工程
1、新建Unity工程
2、導(dǎo)入前面提到的兩個unitypackage包
3、創(chuàng)建UI,一個登錄按鈕,一個顯示用文本,一個掛載腳本的空物體。
4、新建腳本,腳本里的代碼可以直接從https://github.com/CodeMasterYi/google-signin-unity這個示例代碼里面復(fù)制進(jìn)來。然后把腳本拖到GoogleSdkObj上,statusText拖過去進(jìn)行賦值,webclientid下面再詳細(xì)說。
5、給按鈕添加點(diǎn)擊事件,如圖所示。
6、接下來就是這個WebClientId了。
WebClientId獲取
1、進(jìn)入這個地址:谷歌API控制臺,如果沒有cloud項(xiàng)目的話需要新建cloud項(xiàng)目,如果已有直接選擇項(xiàng)目進(jìn)入。
2、創(chuàng)建OAuth 同意屏幕,如果已有可以忽略。
這4個步驟完成就可以了。
3、在“憑據(jù)”頁面上,創(chuàng)建兩個 Android 類型的客戶端 ID
–這是借某位大佬的一張圖–
4、在“憑據(jù)”頁面上,創(chuàng)建一個 Web 類型的客戶端 ID
5、找到創(chuàng)建完成的憑據(jù),復(fù)制出WebClientId
6、把這個WebClientId賦值到代碼,或者直接在inspector界面賦值。
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Google;
using UnityEngine;
using UnityEngine.UI;
public class SigninSampleScript : MonoBehaviour
{
public Text statusText;
public string webClientId = "131761931994-ljnoj13a9gfhruftaqv2a5iicr0i30ub.apps.googleusercontent.com";
private GoogleSignInConfiguration configuration;
// Defer the configuration creation until Awake so the web Client ID
// Can be set via the property inspector in the Editor.
void Awake()
{
configuration = new GoogleSignInConfiguration
{
WebClientId = webClientId,
RequestIdToken = true
};
GameObject.DontDestroyOnLoad(this);
}
public void OnSignIn()
{
GoogleSignIn.Configuration = configuration;
GoogleSignIn.Configuration.UseGameSignIn = false;
GoogleSignIn.Configuration.RequestIdToken = true;
AddStatusText("Calling SignIn");
GoogleSignIn.DefaultInstance.SignIn().ContinueWith(
OnAuthenticationFinished);
}
public void OnSignOut()
{
AddStatusText("Calling SignOut");
GoogleSignIn.DefaultInstance.SignOut();
}
public void OnDisconnect()
{
AddStatusText("Calling Disconnect");
GoogleSignIn.DefaultInstance.Disconnect();
}
internal void OnAuthenticationFinished(Task<GoogleSignInUser> task)
{
if (task.IsFaulted)
{
using (IEnumerator<System.Exception> enumerator =
task.Exception.InnerExceptions.GetEnumerator())
{
if (enumerator.MoveNext())
{
GoogleSignIn.SignInException error =
(GoogleSignIn.SignInException)enumerator.Current;
AddStatusText("Got Error: " + error.Status + " " + error.Message);
}
else
{
AddStatusText("Got Unexpected Exception?!?" + task.Exception);
}
}
}
else if (task.IsCanceled)
{
AddStatusText("Canceled");
}
else
{
AddStatusText("Welcome: " + task.Result.DisplayName + "!");
}
}
public void OnSignInSilently()
{
GoogleSignIn.Configuration = configuration;
GoogleSignIn.Configuration.UseGameSignIn = false;
GoogleSignIn.Configuration.RequestIdToken = true;
AddStatusText("Calling SignIn Silently");
GoogleSignIn.DefaultInstance.SignInSilently()
.ContinueWith(OnAuthenticationFinished);
}
public void OnGamesSignIn()
{
GoogleSignIn.Configuration = configuration;
GoogleSignIn.Configuration.UseGameSignIn = true;
GoogleSignIn.Configuration.RequestIdToken = false;
AddStatusText("Calling Games SignIn");
GoogleSignIn.DefaultInstance.SignIn().ContinueWith(
OnAuthenticationFinished);
}
private List<string> messages = new List<string>();
void AddStatusText(string text)
{
if (messages.Count == 5)
{
messages.RemoveAt(0);
}
messages.Add(text);
string txt = "";
foreach (string s in messages)
{
txt += "\n" + s;
}
statusText.text = txt;
}
}
打包測試
1、設(shè)置一下unityplayersetting,圈起來的地方要注意,
(1)包名要和google上架的一致,
(2)打包方式il2cpp,
(3)keystore要填好,
(4)custom main gradle Template要勾上,然后google地址換成阿里云的
maven {
// url "https://maven.google.com"
url "https://maven.aliyun.com/nexus/content/groups/public"
}
(5)custo gradle properties Template要勾上,然后gradleTemplate.properties腳本里需要加上這兩句
android.useAndroidX=true
android.enableJetifier=true
2、切換到安卓平臺
3、注冊安卓依賴到mainTemplate.gradle文件
(1)
(2)resolve之后修改maven地址
4、打包測試文章來源:http://www.zghlxwxcb.cn/news/detail-492140.html
整個工程已上傳,點(diǎn)擊下面的鏈接可免費(fèi)下載
1、測試工程下載
2、GoogleSignIn_v1.0.4.1.unitypackage下載
3、安卓依賴管理插件EDM4U下載文章來源地址http://www.zghlxwxcb.cn/news/detail-492140.html
到了這里,關(guān)于Unity接入Google登錄超詳細(xì)流程的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!