驱蚊app
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 

120 Zeilen
4.2 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. public 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. var loginArgs = new AppleAuthLoginArgs(LoginOptions.None);
  76. this._appleAuthManager.LoginWithAppleId(
  77. loginArgs,
  78. credential =>
  79. {
  80. // If a sign in with apple succeeds, we should have obtained the credential with the user id, name, and email, save it
  81. PlayerPrefs.SetString(AppleUserIdKey, credential.User);
  82. this.SetupGameMenu(credential.User, credential);
  83. },
  84. error =>
  85. {
  86. var authorizationErrorCode = error.GetAuthorizationErrorCode();
  87. Debug.LogWarning("Sign in with Apple failed " + authorizationErrorCode.ToString() + " " + error.ToString());
  88. });
  89. }
  90. private void SetupGameMenu(string appleUserId, ICredential credential)
  91. {
  92. //var appleIdCredential = credential as IAppleIDCredential;
  93. //var passwordCredential = receivedCredential as IPasswordCredential;
  94. GameData.Instance.appleUserId = appleUserId;
  95. GameData.Instance.isYouke = false;
  96. UIManager.Instance.OpenUI<UIMainMenu>();
  97. }
  98. public void ClickYkLoginBtn()
  99. {
  100. Debug.Log("YouKe Login Clicked");
  101. //GameData.Instance.username = "visitor";
  102. GameData.Instance.appleUserId = "Tourist account";
  103. GameData.Instance.isYouke = true;
  104. UIManager.Instance.OpenUI<UIMainMenu>();
  105. }
  106. }