using PlayFab.ClientModels;
namespace LoginSystemWithPlayfab
public class PlayfabController : MonoBehaviour
[Header ("Playfab Data")]
[SerializeField] protected string titleId;
[HideInInspector] public static string PlayFabID;
[HideInInspector] public string Nickname;
protected string idLogin;
protected string password;
protected bool isFirstLogin;
public string emailSuporte;
[Header ("Login - Main Menu")]
[SerializeField] GameObject mainMenu;
[SerializeField] GameObject exitGameMenu;
[SerializeField] GameObject LoginMenu;
[SerializeField] TMP_InputField inputIdLogin;
[SerializeField] TMP_InputField inputPasswordLogin;
[SerializeField] Toggle rememberMe;
[Header ("Login - CreateAccount")]
[SerializeField] GameObject createAccountMenu;
[SerializeField] TMP_InputField inputUser;
[SerializeField] TMP_InputField inputEmail;
[SerializeField] TMP_InputField inputPassword;
[SerializeField] TMP_InputField inputConfirmPassword;
[Header ("Login - RecoverPassword")]
[SerializeField] GameObject recoverPasswordMenu;
[SerializeField] TMP_InputField inputEmailRecoverAccount;
[Header ("Login - Notification")]
[SerializeField] GameObject notificationMenu;
[SerializeField] TextMeshProUGUI notification;
[SerializeField] GameObject loadingMenu;
rememberMe.isOn = PlayerPrefs.GetString ("rememberMe") == "isOn";
inputIdLogin.text = PlayerPrefs.GetString ("idLogin", "");
if (Input.GetKeyDown (KeyCode.Escape))
exitGameMenu.SetActive (!exitGameMenu.activeInHierarchy);
void Login (string _user, string _password)
loadingMenu.SetActive (true);
protected void UserLogin ()
var request = new LoginWithPlayFabRequest { Username = idLogin, Password = password };
PlayFabClientAPI.LoginWithPlayFab (request, UserLoginSucess, UserLoginFail);
void UserLoginSucess (LoginResult _result)
Debug.Log ("UserLoginSucess");
PlayFabID = _result.PlayFabId;
void UserLoginFail (PlayFabError _error)
Debug.Log ("UserLoginFail");
protected void EmailLogin ()
var requestEmail = new LoginWithEmailAddressRequest { Email = idLogin, Password = password };
PlayFabClientAPI.LoginWithEmailAddress (requestEmail, EmailLoginSucess, EmailLoginFail);
void EmailLoginSucess (LoginResult _result)
Debug.Log ("EmailLoginSucess");
PlayFabID = _result.PlayFabId;
void EmailLoginFail (PlayFabError _error)
Debug.Log ("EmailLoginFail");
loadingMenu.SetActive (false);
protected void UpdateDisplayName (string _displayName)
loadingMenu.SetActive (true);
var request = new UpdateUserTitleDisplayNameRequest { DisplayName = _displayName };
PlayFabClientAPI.UpdateUserTitleDisplayName (request, UpdateDisplayNameSucess, UpdateDisplayNameFail);
protected virtual void UpdateDisplayNameSucess (UpdateUserTitleDisplayNameResult _result)
Debug.Log ("User Display Name Updated Sucess!");
loadingMenu.SetActive (false);
protected virtual void UpdateDisplayNameFail (PlayFabError _error)
Debug.Log ("User Display Name Updated Fail!");
loadingMenu.SetActive (false);
public void DisconnectFromPlayfab ()
Debug.Log ("DisconnectFromPlayfab");
PlayFabClientAPI.ForgetAllCredentials ();
public void CreateAccount (string _user, string _email, string _password)
loadingMenu.SetActive (true);
var registerRequest = new RegisterPlayFabUserRequest { Email = _email, Password = _password, Username = _user };
PlayFabClientAPI.RegisterPlayFabUser (registerRequest, CreateAccountSucess, CreateAccountFail);
protected virtual void CreateAccountSucess (RegisterPlayFabUserResult _result)
Debug.Log ("Create Account Sucess!");
inputIdLogin.text = idLogin;
inputPasswordLogin.text = "";
protected virtual void CreateAccountFail (PlayFabError _error)
Debug.Log ("CreateAccountFail " + _error.Error);
loadingMenu.SetActive (false);
void RecoverAccount (string _email)
loadingMenu.SetActive (true);
var request = new SendAccountRecoveryEmailRequest { TitleId = titleId, Email = _email };
PlayFabClientAPI.SendAccountRecoveryEmail (request, RecoverAccountSucess, RecoverAccountFail);
protected virtual void RecoverAccountSucess (SendAccountRecoveryEmailResult _result)
Debug.Log ("Account Recovered!");
ShowMenu (Screens.Login);
loadingMenu.SetActive (false);
protected virtual void RecoverAccountFail (PlayFabError _error)
Debug.Log ("Account Recover Fail!" + _error.ErrorMessage);
ShowMenu (Screens.Login);
loadingMenu.SetActive (false);
#region Handling UI Elements
protected void ShowMenu (Screens _screen)
mainMenu.SetActive (true);
LoginMenu.SetActive (true);
createAccountMenu.SetActive (false);
recoverPasswordMenu.SetActive (false);
notificationMenu.SetActive (false);
exitGameMenu.SetActive (false);
loadingMenu.SetActive (false);
case Screens.CreateAccount:
mainMenu.SetActive (true);
LoginMenu.SetActive (false);
createAccountMenu.SetActive (true);
recoverPasswordMenu.SetActive (false);
notificationMenu.SetActive (false);
exitGameMenu.SetActive (false);
loadingMenu.SetActive (false);
case Screens.RecoverAccount:
mainMenu.SetActive (true);
LoginMenu.SetActive (false);
createAccountMenu.SetActive (false);
recoverPasswordMenu.SetActive (true);
notificationMenu.SetActive (false);
exitGameMenu.SetActive (false);
loadingMenu.SetActive (false);
exitGameMenu.SetActive (true);
public void ShowNotification (string _notification)
notification.text = _notification;
notificationMenu.SetActive (true);
public void ShowCreateAccountMenu ()
ShowMenu (Screens.CreateAccount);
public void ShowLoginMenu ()
ShowMenu (Screens.Login);
public void ShowRecoverAccountMenu ()
ShowMenu (Screens.RecoverAccount);
public void ShowExitGameMenu ()
ShowMenu (Screens.ExitGame);
Login (inputIdLogin.text, inputPasswordLogin.text);
public void BtnCreateAccount ()
if (string.IsNullOrEmpty (inputUser.text) ||
string.IsNullOrEmpty (inputEmail.text) ||
string.IsNullOrEmpty (inputPassword.text) ||
string.IsNullOrEmpty (inputConfirmPassword.text))
ShowNotification ("You need to fill all fields");
loadingMenu.SetActive (false);
else if (inputPassword.text.Length < 6)
ShowNotification ("Password need 6-100 characters.");
loadingMenu.SetActive (false);
else if (inputPassword.text != inputConfirmPassword.text)
ShowNotification ("Password do not match");
loadingMenu.SetActive (false);
CreateAccount (inputUser.text, inputEmail.text, inputPassword.text);
public void BtnRecoverPassword ()
RecoverAccount (inputEmailRecoverAccount.text);
public void BtnExitGame ()
#region Handling API Login Results
protected virtual void LoginSucess (LoginResult _result)
Debug.Log ("Login Sucess!");
PlayerPrefs.SetString ("rememberMe", "isOn");
PlayerPrefs.SetString ("idLogin", idLogin);
PlayerPrefs.DeleteKey ("rememberMe");
PlayerPrefs.DeleteKey ("idLogin");
var _request = new GetAccountInfoRequest ();
PlayFabClientAPI.GetAccountInfo (_request,
if (result.AccountInfo.Username != result.AccountInfo.TitleInfo.DisplayName)
var requestDisplayName = new UpdateUserTitleDisplayNameRequest { DisplayName = idLogin };
PlayFabClientAPI.UpdateUserTitleDisplayName (requestDisplayName,
loadingMenu.SetActive (false);
mainMenu.SetActive (false);
UpdateDisplayNameFail (error);
loadingMenu.SetActive (false);
mainMenu.SetActive (false);
protected virtual void LoginFail (PlayFabError _error)
Debug.Log ("Login Fail!");