暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using AppleAuth;
  2. using AppleAuth.Enums;
  3. using AppleAuth.Extensions;
  4. using AppleAuth.Interfaces;
  5. using AppleAuth.Native;
  6. using UnityEngine;
  7. public class MainMenu : MonoBehaviour
  8. {
  9. private const string AppleUserIdKey = "AppleUserId";
  10. private IAppleAuthManager _appleAuthManager;
  11. public LoginMenuHandler LoginMenu;
  12. public GameMenuHandler GameMenu;
  13. private void Start()
  14. {
  15. // If the current platform is supported
  16. if (AppleAuthManager.IsCurrentPlatformSupported)
  17. {
  18. // Creates a default JSON deserializer, to transform JSON Native responses to C# instances
  19. var deserializer = new PayloadDeserializer();
  20. // Creates an Apple Authentication manager with the deserializer
  21. this._appleAuthManager = new AppleAuthManager(deserializer);
  22. }
  23. this.InitializeLoginMenu();
  24. }
  25. private void Update()
  26. {
  27. // Updates the AppleAuthManager instance to execute
  28. // pending callbacks inside Unity's execution loop
  29. if (this._appleAuthManager != null)
  30. {
  31. this._appleAuthManager.Update();
  32. }
  33. this.LoginMenu.UpdateLoadingMessage(Time.deltaTime);
  34. }
  35. public void SignInWithAppleButtonPressed()
  36. {
  37. this.SetupLoginMenuForAppleSignIn();
  38. this.SignInWithApple();
  39. }
  40. private void InitializeLoginMenu()
  41. {
  42. this.LoginMenu.SetVisible(visible: true);
  43. this.GameMenu.SetVisible(visible: false);
  44. // Check if the current platform supports Sign In With Apple
  45. if (this._appleAuthManager == null)
  46. {
  47. this.SetupLoginMenuForUnsupportedPlatform();
  48. return;
  49. }
  50. // If at any point we receive a credentials revoked notification, we delete the stored User ID, and go back to login
  51. this._appleAuthManager.SetCredentialsRevokedCallback(result =>
  52. {
  53. Debug.Log("Received revoked callback " + result);
  54. this.SetupLoginMenuForSignInWithApple();
  55. PlayerPrefs.DeleteKey(AppleUserIdKey);
  56. });
  57. // If we have an Apple User Id available, get the credential status for it
  58. if (PlayerPrefs.HasKey(AppleUserIdKey))
  59. {
  60. var storedAppleUserId = PlayerPrefs.GetString(AppleUserIdKey);
  61. this.SetupLoginMenuForCheckingCredentials();
  62. this.CheckCredentialStatusForUserId(storedAppleUserId);
  63. }
  64. // If we do not have an stored Apple User Id, attempt a quick login
  65. else
  66. {
  67. this.SetupLoginMenuForQuickLoginAttempt();
  68. this.AttemptQuickLogin();
  69. }
  70. }
  71. private void SetupLoginMenuForUnsupportedPlatform()
  72. {
  73. this.LoginMenu.SetVisible(visible: true);
  74. this.GameMenu.SetVisible(visible: false);
  75. this.LoginMenu.SetSignInWithAppleButton(visible: false, enabled: false);
  76. this.LoginMenu.SetLoadingMessage(visible: true, message: "Unsupported platform");
  77. }
  78. private void SetupLoginMenuForSignInWithApple()
  79. {
  80. this.LoginMenu.SetVisible(visible: true);
  81. this.GameMenu.SetVisible(visible: false);
  82. this.LoginMenu.SetSignInWithAppleButton(visible: true, enabled: true);
  83. this.LoginMenu.SetLoadingMessage(visible: false, message: string.Empty);
  84. }
  85. private void SetupLoginMenuForCheckingCredentials()
  86. {
  87. this.LoginMenu.SetVisible(visible: true);
  88. this.GameMenu.SetVisible(visible: false);
  89. this.LoginMenu.SetSignInWithAppleButton(visible: true, enabled: false);
  90. this.LoginMenu.SetLoadingMessage(visible: true, message: "Checking Apple Credentials");
  91. }
  92. private void SetupLoginMenuForQuickLoginAttempt()
  93. {
  94. this.LoginMenu.SetVisible(visible: true);
  95. this.GameMenu.SetVisible(visible: false);
  96. this.LoginMenu.SetSignInWithAppleButton(visible: true, enabled: false);
  97. this.LoginMenu.SetLoadingMessage(visible: true, message: "Attempting Quick Login");
  98. }
  99. private void SetupLoginMenuForAppleSignIn()
  100. {
  101. this.LoginMenu.SetVisible(visible: true);
  102. this.GameMenu.SetVisible(visible: false);
  103. this.LoginMenu.SetSignInWithAppleButton(visible: true, enabled: false);
  104. this.LoginMenu.SetLoadingMessage(visible: true, message: "Signing In with Apple");
  105. }
  106. private void SetupGameMenu(string appleUserId, ICredential credential)
  107. {
  108. this.LoginMenu.SetVisible(visible: false);
  109. this.GameMenu.SetVisible(visible: true);
  110. this.GameMenu.SetupAppleData(appleUserId, credential);
  111. }
  112. private void CheckCredentialStatusForUserId(string appleUserId)
  113. {
  114. // If there is an apple ID available, we should check the credential state
  115. this._appleAuthManager.GetCredentialState(
  116. appleUserId,
  117. state =>
  118. {
  119. switch (state)
  120. {
  121. // If it's authorized, login with that user id
  122. case CredentialState.Authorized:
  123. this.SetupGameMenu(appleUserId, null);
  124. return;
  125. // If it was revoked, or not found, we need a new sign in with apple attempt
  126. // Discard previous apple user id
  127. case CredentialState.Revoked:
  128. case CredentialState.NotFound:
  129. this.SetupLoginMenuForSignInWithApple();
  130. PlayerPrefs.DeleteKey(AppleUserIdKey);
  131. return;
  132. }
  133. },
  134. error =>
  135. {
  136. var authorizationErrorCode = error.GetAuthorizationErrorCode();
  137. Debug.LogWarning("Error while trying to get credential state " + authorizationErrorCode.ToString() + " " + error.ToString());
  138. this.SetupLoginMenuForSignInWithApple();
  139. });
  140. }
  141. private void AttemptQuickLogin()
  142. {
  143. var quickLoginArgs = new AppleAuthQuickLoginArgs();
  144. // Quick login should succeed if the credential was authorized before and not revoked
  145. this._appleAuthManager.QuickLogin(
  146. quickLoginArgs,
  147. credential =>
  148. {
  149. // If it's an Apple credential, save the user ID, for later logins
  150. var appleIdCredential = credential as IAppleIDCredential;
  151. if (appleIdCredential != null)
  152. {
  153. PlayerPrefs.SetString(AppleUserIdKey, credential.User);
  154. }
  155. this.SetupGameMenu(credential.User, credential);
  156. },
  157. error =>
  158. {
  159. // If Quick Login fails, we should show the normal sign in with apple menu, to allow for a normal Sign In with apple
  160. var authorizationErrorCode = error.GetAuthorizationErrorCode();
  161. Debug.LogWarning("Quick Login Failed " + authorizationErrorCode.ToString() + " " + error.ToString());
  162. this.SetupLoginMenuForSignInWithApple();
  163. });
  164. }
  165. private void SignInWithApple()
  166. {
  167. var loginArgs = new AppleAuthLoginArgs(LoginOptions.IncludeEmail | LoginOptions.IncludeFullName);
  168. this._appleAuthManager.LoginWithAppleId(
  169. loginArgs,
  170. credential =>
  171. {
  172. // If a sign in with apple succeeds, we should have obtained the credential with the user id, name, and email, save it
  173. PlayerPrefs.SetString(AppleUserIdKey, credential.User);
  174. this.SetupGameMenu(credential.User, credential);
  175. },
  176. error =>
  177. {
  178. var authorizationErrorCode = error.GetAuthorizationErrorCode();
  179. Debug.LogWarning("Sign in with Apple failed " + authorizationErrorCode.ToString() + " " + error.ToString());
  180. this.SetupLoginMenuForSignInWithApple();
  181. });
  182. }
  183. }