Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using PlayFab;
- using PlayFab.ClientModels;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- using UnityEngine.UI;
- using UnityEngine.SceneManagement;
- public class PlayFabLoginScript : MonoBehaviour {
- public InputField regUsername, regEmail, regPassword;
- public GameObject regPanel;
- public InputField loginUsername, loginPassword;
- public GameObject loginPanel;
- public GameObject userInputPanel;
- public GameObject infoPanel;
- public GameObject infoPanele;
- public Text infoUsername, infoCreatedAt, playername;
- public Text errorText;
- // Use this for initialization
- void Start ()
- {
- // Request erstellen
- var request = new LoginWithCustomIDRequest { CustomId = "GettingStartedGuide", CreateAccount = true };
- // API Request => PlayFab
- PlayFabClientAPI.LoginWithCustomID(request, OnLoginSuccess, OnLoginFailure);
- }
- private void OnLoginFailure(PlayFabError obj)
- {
- Debug.Log("Es ist etwas schief gelaufen! :-(");
- }
- private void OnLoginSuccess(LoginResult obj)
- {
- Debug.Log("API Call hat funktioniert! :-)");
- }
- // Update is called once per frame
- void Update () {
- }
- // Registrierung
- public void Register()
- {
- var request = new RegisterPlayFabUserRequest();
- request.TitleId = PlayFabSettings.TitleId;
- // Eingabedaten zuweisen
- request.Email = regEmail.text;
- request.Username = regUsername.text;
- request.Password = regPassword.text;
- // Übergabe des Registrierungs-Request an PlayFab API
- PlayFabClientAPI.RegisterPlayFabUser(request, OnRegisterResult, OnPlayFabError);
- }
- private void OnPlayFabError(PlayFabError obj)
- {
- //print("Registrierung fehlgeschlagen!");
- print("Error: " + obj.Error);
- //errorText.text = obj.Error.ToString();
- string output = "";
- switch(obj.Error)
- {
- case PlayFabErrorCode.AccountBanned:
- output = "Lieber Panda: Dein Nutzerkonto wurde leider Deaktiviert. Bei Fragen kontaktiere bitte den Support.";
- break;
- case PlayFabErrorCode.EmailAddressNotAvailable:
- output = "E-Mail Adresse wird bereits verwendet";
- break;
- case PlayFabErrorCode.InvalidParams:
- output = "Oops da ist etwas Falsch!";
- break;
- case PlayFabErrorCode.InvalidUsernameOrPassword:
- output = "Falscher Benutzername oder Passwort";
- break;
- case PlayFabErrorCode.UsernameNotAvailable:
- output = "Dieser name ist vergeben Oder Ungültig.";
- break;
- default:
- break;
- }
- // Text der Fehlermeldung zuweisen
- errorText.text = output;
- }
- private void OnRegisterResult(RegisterPlayFabUserResult obj)
- {
- print("Registrierung hat funktioniert!");
- // Panel Register ausblenden
- userInputPanel.SetActive(false);
- SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex +1);
- }
- public void Login()
- {
- var request = new LoginWithPlayFabRequest();
- request.TitleId = PlayFabSettings.TitleId;
- request.Username = loginUsername.text;
- request.Password = loginPassword.text;
- // Request an PlayFab API übergeben
- PlayFabClientAPI.LoginWithPlayFab(request, OnLoginResult, OnPlayFabError);
- }
- private void OnLoginResult(LoginResult obj)
- {
- print("Login erfolgreich!");
- // Login Panel deaktivieren
- userInputPanel.SetActive(false);
- // Account Informationen holen - Methode starten
- GetAccountInfo();
- SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex +1);
- }
- // Account Informationen
- public void GetAccountInfo()
- {
- var request = new GetAccountInfoRequest();
- // Request an API übergeben
- PlayFabClientAPI.GetAccountInfo(request, OnGetAccountInfoSuccess, OnPlayFabError);
- }
- private void OnGetAccountInfoSuccess(GetAccountInfoResult resultData)
- {
- // Info Panel eingeblendet? falls nein => einblenden
- if(!infoPanel.activeSelf)
- {
- infoPanel.SetActive(true);
- infoPanele.SetActive(true);
- }
- print("Account Daten erhalten");
- // Daten ausgeben lassen
- print(resultData.AccountInfo.Username);
- // Ergebnis auf Textfeld anzeigen
- infoUsername.text = resultData.AccountInfo.Username.ToString();
- playername.text = "Hallo " + resultData.AccountInfo.Username.ToString();
- //infoCreatedAt.text = resultData.AccountInfo.Created.ToString();
- infoCreatedAt.text = resultData.AccountInfo.PrivateInfo.Email.ToString();
- print(resultData.AccountInfo.Created);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement