|
| 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 | +} |
0 commit comments