Skip to content

Commit b33f416

Browse files
author
盧彥辰
committed
feat: Google登入腳本建置及按鈕綁定
issue #6
1 parent 893d881 commit b33f416

File tree

4 files changed

+231
-2
lines changed

4 files changed

+231
-2
lines changed

Assets/Scenes/GoogleSignIn.unity

+37-2
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,18 @@ MonoBehaviour:
564564
m_TargetGraphic: {fileID: 4433900990688558991}
565565
m_OnClick:
566566
m_PersistentCalls:
567-
m_Calls: []
567+
m_Calls:
568+
- m_Target: {fileID: 4433900992328973397}
569+
m_MethodName: OnSignIn
570+
m_Mode: 1
571+
m_Arguments:
572+
m_ObjectArgument: {fileID: 0}
573+
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
574+
m_IntArgument: 0
575+
m_FloatArgument: 0
576+
m_StringArgument:
577+
m_BoolArgument: 0
578+
m_CallState: 2
568579
--- !u!224 &4433900990688558985
569580
RectTransform:
570581
m_ObjectHideFlags: 0
@@ -720,7 +731,18 @@ MonoBehaviour:
720731
m_TargetGraphic: {fileID: 4433900991080339282}
721732
m_OnClick:
722733
m_PersistentCalls:
723-
m_Calls: []
734+
m_Calls:
735+
- m_Target: {fileID: 4433900992328973397}
736+
m_MethodName: OnSignOut
737+
m_Mode: 1
738+
m_Arguments:
739+
m_ObjectArgument: {fileID: 0}
740+
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
741+
m_IntArgument: 0
742+
m_FloatArgument: 0
743+
m_StringArgument:
744+
m_BoolArgument: 0
745+
m_CallState: 2
724746
--- !u!224 &4433900991080339292
725747
RectTransform:
726748
m_ObjectHideFlags: 0
@@ -988,10 +1010,23 @@ GameObject:
9881010
- component: {fileID: 4433900992328973387}
9891011
- component: {fileID: 4433900992328973385}
9901012
- component: {fileID: 4433900992328973386}
1013+
- component: {fileID: 4433900992328973397}
9911014
m_Layer: 5
9921015
m_Name: Content
9931016
m_TagString: Untagged
9941017
m_Icon: {fileID: 0}
9951018
m_NavMeshLayer: 0
9961019
m_StaticEditorFlags: 0
9971020
m_IsActive: 1
1021+
--- !u!114 &4433900992328973397
1022+
MonoBehaviour:
1023+
m_ObjectHideFlags: 0
1024+
m_CorrespondingSourceObject: {fileID: 0}
1025+
m_PrefabInstance: {fileID: 0}
1026+
m_PrefabAsset: {fileID: 0}
1027+
m_GameObject: {fileID: 4433900992328973396}
1028+
m_Enabled: 1
1029+
m_EditorHideFlags: 0
1030+
m_Script: {fileID: 11500000, guid: da6e1d4b1094441d0a42d220ff993176, type: 3}
1031+
m_Name:
1032+
m_EditorClassIdentifier:

Assets/Script/Model/GoogleSignIn.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Threading.Tasks;
5+
using Google;
6+
using Newtonsoft.Json;
7+
using Newtonsoft.Json.Utilities;
8+
using UnityEngine;
9+
using UnityEngine.UI;
10+
11+
public class GoogleSignInHandler : MonoBehaviour
12+
{
13+
//序列化狀態
14+
Text GoogleMessageText;
15+
16+
//反序列化狀態
17+
Button GoogleLoginButton;
18+
19+
//序列化內容
20+
Button GoogleLogoutButton;
21+
22+
private GoogleSignInConfiguration configuration;
23+
24+
/// <summary>
25+
/// Google 登入初始化
26+
/// </summary>
27+
void Awake()
28+
{
29+
//填入ClientID及可以取得id, token
30+
configuration = new GoogleSignInConfiguration
31+
{
32+
WebClientId = "303413321002-osmliudj71vc5e0lqjd8l6mb137v0clj.apps.googleusercontent.com",
33+
RequestIdToken = true
34+
};
35+
36+
37+
if (GameObject.Find("/Canvas/Content/Image/Message").TryGetComponent<Text>(out Text textSerialization))
38+
{
39+
GoogleMessageText = textSerialization;
40+
}
41+
42+
if (GameObject.Find("/Canvas/Content/LogIn").TryGetComponent<Button>(out Button textGoogleLoginButton))
43+
{
44+
GoogleLoginButton = textGoogleLoginButton;
45+
}
46+
47+
if (GameObject.Find("/Canvas/Content/LogOut").TryGetComponent<Button>(out Button textGoogleLogoutButton))
48+
{
49+
GoogleLogoutButton = textGoogleLogoutButton;
50+
}
51+
52+
53+
GoogleLoginButton.interactable = false;
54+
GoogleLogoutButton.interactable = false;
55+
}
56+
57+
/// <summary>
58+
/// Google 登入
59+
/// </summary>
60+
public void OnSignIn()
61+
{
62+
if (GoogleSignIn.Configuration != null)
63+
{
64+
Debug.Log("GoogleSignIn.Configuration != null");
65+
OnSignOut();
66+
}
67+
68+
GoogleSignIn.Configuration = configuration;
69+
GoogleSignIn.Configuration.UseGameSignIn = false;
70+
GoogleSignIn.Configuration.RequestIdToken = true;
71+
GoogleSignIn.Configuration.RequestEmail = true;
72+
Debug.Log("Google Calling SignIn");
73+
GoogleMessageText.text = "Google Calling SignIn";
74+
75+
GoogleSignIn.DefaultInstance.SignIn().ContinueWith(
76+
OnAuthenticationFinished);
77+
}
78+
79+
/// <summary>
80+
/// Google 登出
81+
/// </summary>
82+
public void OnSignOut()
83+
{
84+
Debug.Log("Google Calling SignOut");
85+
GoogleMessageText.text = "Google Calling SignOut";
86+
GoogleSignIn.DefaultInstance.SignOut();
87+
}
88+
89+
/// <summary>
90+
/// Google 登入資訊回傳
91+
/// </summary>
92+
internal void OnAuthenticationFinished(Task<GoogleSignInUser> task)
93+
{
94+
if (task.IsFaulted) //登入錯誤
95+
{
96+
using (IEnumerator<System.Exception> enumerator =
97+
task.Exception.InnerExceptions.GetEnumerator())
98+
{
99+
100+
if (enumerator.MoveNext()) //錯誤訊息
101+
{
102+
GoogleSignIn.SignInException error =
103+
(GoogleSignIn.SignInException)enumerator.Current;
104+
Debug.Log("Got Error: " + error.Status + " " + error.Message);
105+
GoogleMessageText.text = "Got Error 錯誤訊息: " + error.Status + " " + error.Message;
106+
}
107+
else //例外狀況
108+
{
109+
Debug.Log("Got Unexpected Exception !?" + task.Exception);
110+
GoogleMessageText.text = "Got Unexpected Exception 例外狀況 !? " + task.Exception;
111+
}
112+
}
113+
}
114+
else if (task.IsCanceled) //取消登入
115+
{
116+
Debug.Log("Canceled");
117+
GoogleMessageText.text = "取消登入";
118+
119+
120+
}
121+
else //登入成功
122+
{
123+
var stringBuilder = new StringBuilder();
124+
Debug.Log("Google AuthCode: " + task.Result.AuthCode);
125+
Debug.Log("Google IdToken: " + task.Result.IdToken);
126+
Debug.Log("Google UserId: " + task.Result.UserId);
127+
128+
stringBuilder.Append("---登入成功---\n");
129+
stringBuilder.Append("Google AuthCode: " + task.Result.AuthCode + "\n");
130+
stringBuilder.Append("Google IdToken: " + task.Result.IdToken + "\n");
131+
stringBuilder.Append("Google UserId: " + task.Result.UserId + "\n");
132+
133+
GoogleMessageText.text = stringBuilder.ToString();
134+
}
135+
}
136+
137+
/// <summary>
138+
/// 快速登入(已登入過)
139+
/// </summary>
140+
public void OnSignInSilently()
141+
{
142+
GoogleSignIn.Configuration = configuration;
143+
GoogleSignIn.Configuration.UseGameSignIn = false;
144+
GoogleSignIn.Configuration.RequestIdToken = true;
145+
Debug.Log("Google Calling SignIn Silently");
146+
147+
GoogleSignIn.DefaultInstance.SignInSilently()
148+
.ContinueWith(OnAuthenticationFinished);
149+
}
150+
151+
/// <summary>
152+
/// Google Game Servise 登入
153+
/// </summary>
154+
public void OnGamesSignIn()
155+
{
156+
GoogleSignIn.Configuration = configuration;
157+
GoogleSignIn.Configuration.UseGameSignIn = true;
158+
GoogleSignIn.Configuration.RequestIdToken = false;
159+
160+
Debug.Log("Google Calling Games SignIn");
161+
162+
GoogleSignIn.DefaultInstance.SignIn().ContinueWith(
163+
OnAuthenticationFinished);
164+
}
165+
166+
/// <summary>
167+
/// 更改按鍵可觸碰與否狀態(Google)
168+
/// </summary>
169+
/// <param name="isEnable"></param>
170+
private void UpdateButtonEnable(bool _isEnable)
171+
{
172+
Debug.Log("Google UpdateButtonEnable: " + _isEnable);
173+
GetComponent<Button>().enabled = _isEnable;
174+
}
175+
}

Assets/Script/Model/GoogleSignIn/GoogleSignInHandler.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)