驱蚊app
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

120 行
4.1 KiB

  1. using AppleAuth;
  2. using AppleAuth.Enums;
  3. using AppleAuth.Extensions;
  4. using AppleAuth.Interfaces;
  5. using AppleAuth.Native;
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using UnityEngine;
  11. public class UILogin : UIBase
  12. {
  13. private const string AppleUserIdKey = "AppleUserId";
  14. private IAppleAuthManager _appleAuthManager;
  15. public override void OnOpen(params object[] args) {
  16. // If the current platform is supported
  17. if (AppleAuthManager.IsCurrentPlatformSupported)
  18. {
  19. // Creates a default JSON deserializer, to transform JSON Native responses to C# instances
  20. var deserializer = new PayloadDeserializer();
  21. // Creates an Apple Authentication manager with the deserializer
  22. this._appleAuthManager = new AppleAuthManager(deserializer);
  23. }
  24. if (PlayerPrefs.HasKey(AppleUserIdKey))
  25. {
  26. var storedAppleUserId = PlayerPrefs.GetString(AppleUserIdKey);
  27. this.CheckCredentialStatusForUserId(storedAppleUserId);
  28. }
  29. }
  30. public override void OnClose() { }
  31. private void Update()
  32. {
  33. // Updates the AppleAuthManager instance to execute
  34. // pending callbacks inside Unity's execution loop
  35. if (this._appleAuthManager != null)
  36. {
  37. this._appleAuthManager.Update();
  38. }
  39. }
  40. public void ClickIOSLoginBtn()
  41. {
  42. Debug.Log("IOS Login Clicked");
  43. SignInWithApple();
  44. }
  45. private void CheckCredentialStatusForUserId(string appleUserId)
  46. {
  47. // If there is an apple ID available, we should check the credential state
  48. this._appleAuthManager.GetCredentialState(
  49. appleUserId,
  50. state =>
  51. {
  52. switch (state)
  53. {
  54. // If it's authorized, login with that user id
  55. case CredentialState.Authorized:
  56. this.SetupGameMenu(appleUserId, null);
  57. return;
  58. // If it was revoked, or not found, we need a new sign in with apple attempt
  59. // Discard previous apple user id
  60. case CredentialState.Revoked:
  61. case CredentialState.NotFound:
  62. PlayerPrefs.DeleteKey(AppleUserIdKey);
  63. return;
  64. }
  65. },
  66. error =>
  67. {
  68. var authorizationErrorCode = error.GetAuthorizationErrorCode();
  69. Debug.LogWarning("Error while trying to get credential state " + authorizationErrorCode.ToString() + " " + error.ToString());
  70. });
  71. }
  72. private void SignInWithApple()
  73. {
  74. var loginArgs = new AppleAuthLoginArgs(LoginOptions.IncludeEmail | LoginOptions.IncludeFullName);
  75. this._appleAuthManager.LoginWithAppleId(
  76. loginArgs,
  77. credential =>
  78. {
  79. // If a sign in with apple succeeds, we should have obtained the credential with the user id, name, and email, save it
  80. PlayerPrefs.SetString(AppleUserIdKey, credential.User);
  81. this.SetupGameMenu(credential.User, credential);
  82. },
  83. error =>
  84. {
  85. var authorizationErrorCode = error.GetAuthorizationErrorCode();
  86. Debug.LogWarning("Sign in with Apple failed " + authorizationErrorCode.ToString() + " " + error.ToString());
  87. });
  88. }
  89. private void SetupGameMenu(string appleUserId, ICredential credential)
  90. {
  91. var appleIdCredential = credential as IAppleIDCredential;
  92. //var passwordCredential = receivedCredential as IPasswordCredential;
  93. GameData.Instance.appleUserId = appleUserId;
  94. GameData.Instance.isYouke = false;
  95. UIManager.Instance.OpenUI<UIMainMenu>();
  96. }
  97. public void ClickYkLoginBtn()
  98. {
  99. Debug.Log("YouKe Login Clicked");
  100. //GameData.Instance.username = "visitor";
  101. GameData.Instance.appleUserId = "123";
  102. GameData.Instance.isYouke = false;
  103. UIManager.Instance.OpenUI<UIMainMenu>();
  104. }
  105. }