Skip to content

Instantly share code, notes, and snippets.

/123 Secret

Created Aug 2, 2017
Embed
What would you like to do?
using PlayFab;
using PlayFab.ClientModels;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class PlayFabLogin : MonoBehaviour
{
private static string playerID;
public static string PlayerID { get { return playerID; } }
public GameObject errorRegister;
public GameObject errorLogin;
public GameObject SuccessRegister;
public GameObject SuccessLogin;
public GameObject creatPanel;
public GameObject loginPanel;
public void Start()
{
PlayFabSettings.TitleId = "BE31"; // Please change this value to your own titleId from PlayFab Game Manager
creatPanel.SetActive(false);
loginPanel.SetActive(true);
errorRegister.SetActive(false);
errorLogin.SetActive(false);
SuccessRegister.SetActive(false);
SuccessLogin.SetActive(false);
}
public void LoginPlayFabAccount(string _username, string _password)
{
if (_username.Contains("@"))
{
LoginWithEmailAddressRequest loginRequest = new LoginWithEmailAddressRequest();
loginRequest.Email = _username;
loginRequest.Password = _password;
PlayFabClientAPI.LoginWithEmailAddress(loginRequest, OnLoginPlayFabAccountSuccess, OnLoginPlayFabAccountFailure);
}
else
{
LoginWithPlayFabRequest loginRequest = new LoginWithPlayFabRequest();
loginRequest.Username = _username;
loginRequest.Password = _password;
PlayFabClientAPI.LoginWithPlayFab(loginRequest, OnLoginPlayFabAccountSuccess, OnLoginPlayFabAccountFailure);
}
}
private void OnLoginPlayFabAccountSuccess(LoginResult _result)
{
SuccessLogin.SetActive(true);
playerID = _result.PlayFabId;
StartCoroutine(CharacterSelection());
Debug.LogFormat("Logged in user with PlayFab ID {0}.", _result.PlayFabId);
}
private void OnLoginPlayFabAccountFailure(PlayFabError _error)
{
errorLogin.SetActive(true);
playerID = null;
Debug.LogWarning("Failed to login user:");
Debug.LogError(_error.GenerateErrorReport());
}
public void Register(string _username, string _password, string _email)
{
{
RegisterPlayFabUserRequest registerRequest = new RegisterPlayFabUserRequest();
registerRequest.Username = _username;
registerRequest.Password = _password;
registerRequest.Email = _email;
PlayFabClientAPI.RegisterPlayFabUser(registerRequest, OnRegisterSuccess, OnRegisterFailure);
}
}
public void OnRegisterSuccess(RegisterPlayFabUserResult _result)
{
StartCoroutine(Back());
Debug.LogFormat("Registered {0} with PlayFab ID {1}.", _result.Username, _result.PlayFabId);
StartCoroutine(SuccessReg());
}
public void OnRegisterFailure(PlayFabError _error)
{
errorRegister.SetActive(true);
Debug.LogWarning("Failed to register user:");
Debug.LogError(_error.GenerateErrorReport());
}
public void AccountRecover(string _email)
{
if (_email.Contains("@"))
{
SendAccountRecoveryEmailRequest recoverPass = new SendAccountRecoveryEmailRequest();
recoverPass.Email = _email;
recoverPass.TitleId = "BE31";
PlayFabClientAPI.SendAccountRecoveryEmail(recoverPass, OnRecoverSuccess, OnRecoverFailure);
}
}
public void OnRecoverSuccess(SendAccountRecoveryEmailResult _result)
{
Debug.Log("Check your mail");
}
public void OnRecoverFailure(PlayFabError _error)
{
Debug.LogWarning("Failed to register user:");
Debug.LogError(_error.GenerateErrorReport());
}
// private void OnLoginSuccess(LoginResult result)
// {
// Debug.Log("Congratulations, you made your first successful API call!");
// }
private void OnLoginFailure(PlayFabError error)
{
errorLogin.SetActive(true);
Debug.LogWarning("Something went wrong with your first API call. :(");
Debug.LogError("Here's some debug information:");
Debug.LogError(error.GenerateErrorReport());
}
IEnumerator CharacterSelection()
{
yield return new WaitForSeconds(2f);
Application.LoadLevel("Level1");
}
IEnumerator Back()
{
yield return new WaitForSeconds(2f);
creatPanel.SetActive(false);
loginPanel.SetActive(true);
}
IEnumerator SuccessReg()
{
SuccessRegister.SetActive(true);
yield return new WaitForSeconds(3f);
SuccessRegister.SetActive(false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment