Guest User

Untitled

a guest
Apr 22nd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. using GameSparks.Api.Requests;
  4. using GameSparks.Api.Responses;
  5. using System;
  6.  
  7. public class LoginManager : MonoBehaviour
  8. {
  9. public string SceneAfterLogin;
  10. public static LoginManager Instance;
  11. private void Awake() {
  12. Instance = this;
  13. }
  14.  
  15. private void Start() {
  16. GameSparksManager.Instance.OnGameSparksInitializationCompleted.AddListener(TryAutoLogin);
  17. }
  18.  
  19. private void TryAutoLogin() {
  20. if (PlayerPrefs.HasKey("login")) {
  21. Login(PlayerPrefs.GetString("login"));
  22. }
  23. }
  24. public static void Login(Action<string> registrationerror = null)
  25. {
  26. var login = new System.Guid().ToString();
  27. Login(login, registrationerror);
  28. }
  29.  
  30. public static void Login(string login, Action<string> registrationerror = null)
  31. {
  32. Login(login, SystemInfo.deviceUniqueIdentifier, Instance.Success,
  33. (s1, s2) => Register(s1, s2, Instance.Success, registrationerror));
  34. }
  35.  
  36. static void Login(string login, string password, Action success, Action<string, string> error = null)
  37. {
  38. new AuthenticationRequest()
  39. .SetUserName(login)
  40. .SetPassword(SystemInfo.deviceUniqueIdentifier)
  41. .Send((response) =>
  42. {
  43. if (response.HasErrors)
  44. {
  45. if (error != null) {
  46. error(login, password);
  47. }
  48. }
  49. else
  50. {
  51. Debug.Log("Successful login");
  52. PlayerPrefs.SetString("login", login);
  53. success();
  54. }
  55. });
  56. }
  57.  
  58. static void Register(string login, string password, Action success, Action<string> error = null) {
  59. new RegistrationRequest()
  60. .SetUserName(login)
  61. .SetDisplayName(login)
  62. .SetPassword(SystemInfo.deviceUniqueIdentifier)
  63. .Send((response) =>
  64. {
  65. if (response.HasErrors)
  66. {
  67. if (error != null) {
  68. error(login);
  69. }
  70. }
  71. else
  72. {
  73. Debug.Log("Successful registration");
  74. success();
  75. }
  76. });
  77. }
  78.  
  79. void Success() {
  80. SceneManager.LoadScene(SceneAfterLogin);
  81. }
  82. }
Add Comment
Please, Sign In to add comment