Advertisement
Guest User

Untitled

a guest
Jun 28th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. using PlayFab;
  2. using PlayFab.ClientModels;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using System;
  7. using UnityEngine.UI;
  8. using UnityEngine.SceneManagement;
  9.  
  10. public class PlayFabLoginScript : MonoBehaviour {
  11.  
  12. public InputField regUsername, regEmail, regPassword;
  13. public GameObject regPanel;
  14. public InputField loginUsername, loginPassword;
  15. public GameObject loginPanel;
  16. public GameObject userInputPanel;
  17.  
  18. public GameObject infoPanel;
  19. public GameObject infoPanele;
  20. public Text infoUsername, infoCreatedAt, playername;
  21. public Text errorText;
  22.  
  23. // Use this for initialization
  24. void Start ()
  25. {
  26. // Request erstellen
  27. var request = new LoginWithCustomIDRequest { CustomId = "GettingStartedGuide", CreateAccount = true };
  28. // API Request => PlayFab
  29. PlayFabClientAPI.LoginWithCustomID(request, OnLoginSuccess, OnLoginFailure);
  30. }
  31.  
  32. private void OnLoginFailure(PlayFabError obj)
  33. {
  34. Debug.Log("Es ist etwas schief gelaufen! :-(");
  35. }
  36.  
  37. private void OnLoginSuccess(LoginResult obj)
  38. {
  39. Debug.Log("API Call hat funktioniert! :-)");
  40. }
  41.  
  42. // Update is called once per frame
  43. void Update () {
  44.  
  45. }
  46.  
  47. // Registrierung
  48. public void Register()
  49. {
  50. var request = new RegisterPlayFabUserRequest();
  51. request.TitleId = PlayFabSettings.TitleId;
  52. // Eingabedaten zuweisen
  53. request.Email = regEmail.text;
  54. request.Username = regUsername.text;
  55. request.Password = regPassword.text;
  56.  
  57. // Übergabe des Registrierungs-Request an PlayFab API
  58. PlayFabClientAPI.RegisterPlayFabUser(request, OnRegisterResult, OnPlayFabError);
  59. }
  60.  
  61. private void OnPlayFabError(PlayFabError obj)
  62. {
  63. //print("Registrierung fehlgeschlagen!");
  64. print("Error: " + obj.Error);
  65. //errorText.text = obj.Error.ToString();
  66.  
  67. string output = "";
  68.  
  69. switch(obj.Error)
  70. {
  71. case PlayFabErrorCode.AccountBanned:
  72. output = "Lieber Panda: Dein Nutzerkonto wurde leider Deaktiviert. Bei Fragen kontaktiere bitte den Support.";
  73. break;
  74. case PlayFabErrorCode.EmailAddressNotAvailable:
  75. output = "E-Mail Adresse wird bereits verwendet";
  76. break;
  77. case PlayFabErrorCode.InvalidParams:
  78. output = "Oops da ist etwas Falsch!";
  79. break;
  80. case PlayFabErrorCode.InvalidUsernameOrPassword:
  81. output = "Falscher Benutzername oder Passwort";
  82. break;
  83. case PlayFabErrorCode.UsernameNotAvailable:
  84. output = "Dieser name ist vergeben Oder Ungültig.";
  85. break;
  86. default:
  87. break;
  88. }
  89.  
  90. // Text der Fehlermeldung zuweisen
  91. errorText.text = output;
  92. }
  93.  
  94. private void OnRegisterResult(RegisterPlayFabUserResult obj)
  95. {
  96. print("Registrierung hat funktioniert!");
  97. // Panel Register ausblenden
  98. userInputPanel.SetActive(false);
  99. SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex +1);
  100.  
  101. }
  102.  
  103. public void Login()
  104. {
  105. var request = new LoginWithPlayFabRequest();
  106. request.TitleId = PlayFabSettings.TitleId;
  107. request.Username = loginUsername.text;
  108. request.Password = loginPassword.text;
  109.  
  110. // Request an PlayFab API übergeben
  111. PlayFabClientAPI.LoginWithPlayFab(request, OnLoginResult, OnPlayFabError);
  112. }
  113.  
  114. private void OnLoginResult(LoginResult obj)
  115. {
  116. print("Login erfolgreich!");
  117. // Login Panel deaktivieren
  118. userInputPanel.SetActive(false);
  119. // Account Informationen holen - Methode starten
  120. GetAccountInfo();
  121. SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex +1);
  122.  
  123. }
  124.  
  125. // Account Informationen
  126. public void GetAccountInfo()
  127. {
  128. var request = new GetAccountInfoRequest();
  129. // Request an API übergeben
  130. PlayFabClientAPI.GetAccountInfo(request, OnGetAccountInfoSuccess, OnPlayFabError);
  131. }
  132.  
  133. private void OnGetAccountInfoSuccess(GetAccountInfoResult resultData)
  134. {
  135. // Info Panel eingeblendet? falls nein => einblenden
  136. if(!infoPanel.activeSelf)
  137. {
  138. infoPanel.SetActive(true);
  139. infoPanele.SetActive(true);
  140.  
  141. }
  142.  
  143. print("Account Daten erhalten");
  144. // Daten ausgeben lassen
  145. print(resultData.AccountInfo.Username);
  146. // Ergebnis auf Textfeld anzeigen
  147. infoUsername.text = resultData.AccountInfo.Username.ToString();
  148. playername.text = "Hallo " + resultData.AccountInfo.Username.ToString();
  149. //infoCreatedAt.text = resultData.AccountInfo.Created.ToString();
  150. infoCreatedAt.text = resultData.AccountInfo.PrivateInfo.Email.ToString();
  151. print(resultData.AccountInfo.Created);
  152.  
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement