question

omerdruu avatar image
omerdruu asked

How can i print user's username in another text?

Hello, I met Playfab in a short time and I have 2 questions about user logins. I would be glad if you help. I have a panel called Panel and it has items to register and login. The user can register and login without error. The first thing I want to do: When the user logs in, I close the panel, but when user open game again , this panel becomes active again. When the user logs in for the first time, I want the panel to be closed and this panel to be closed until the user presses the sign out button in the game. In short, I want to keep the user's username and password in memory until the user logs out. The second thing I want to do is: I have a text named isim. The isim is constantly in the upper left corner of the game. I want the username to turn into isim text and stay there forever. The code file I am using is as follows

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using PlayFab;
using PlayFab.ClientModels;
using System;


public class PlayFabManager : MonoBehaviour
{
    [Header("UI")]
    public Text messageText ;
    public InputField usernameInput;
    public InputField emailInput;
    public InputField passwordInput;
    public GameObject Panel;




    public void RegisterButton()
    {
        if (passwordInput.text.Length < 6)
        {
            messageText.text = "Şifreniz en az 6 karakter içermelidir!";
            return;
        }
        var request = new RegisterPlayFabUserRequest
        {
            Email = emailInput.text,
            Password = passwordInput.text,
            Username = usernameInput.text,
            RequireBothUsernameAndEmail = true
        };
        PlayFabClientAPI.RegisterPlayFabUser(request, OnRegisterSuccess, OnError);
    }


    void OnRegisterSuccess(RegisterPlayFabUserResult result)
    {
        messageText.text = "Kayıt Yapıldı Ve Giriş Yapılıyor...";
    }


    void OnError(PlayFabError error)
    {
        messageText.text = error.ErrorMessage;
        Debug.Log(error.GenerateErrorReport());
    }


    public void LoginButton()
    {
        var request = new LoginWithPlayFabRequest
        {
            Password = passwordInput.text,
            Username = usernameInput.text
        };
        PlayFabClientAPI.LoginWithPlayFab(request, OnLoginSuccess, OnError);


    }




    public void ResetPasswordButton()
    {
        var request = new SendAccountRecoveryEmailRequest
        {
            Email = emailInput.text,
            TitleId = "XXXX"
        };
        PlayFabClientAPI.SendAccountRecoveryEmail(request, OnPasswordReset, OnError);
    }






    private void OnPasswordReset(SendAccountRecoveryEmailResult result)
    {
   //     throw new NotImplementedException();
        messageText.text = "Şifre Sıfırlama Bağlantısı Mail Adresinize Gönderildi";
    }






    void OnLoginSuccess(LoginResult result)
    {
        messageText.text = "Giriş Yapıldı";
        Debug.Log("Giriş Başarılı");
        Panel.SetActive(false);
 
    }
}
unity3dAuthentication
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

omerdruu avatar image
omerdruu answered

Thank you very much for your answer. I solved the problem using PlayerPrefs. But I still couldn't find a solution to convert the username to text. There are 2 separate codes that I wrote below. none of these work. What should I do. Where am i making the mistake ?

 UserAccountInfo InfoRequestParameters = new UserAccountInfo
        {


            Username = isim.text
        };








        var request = new UserAccountInfo
        {
           Username  = isim.text
            
            
        };

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Gosen Gao avatar image Gosen Gao commented ·

You can refer to the code below.

PlayFabClientAPI.LoginWithPlayFab(new LoginWithPlayFabRequest
        {
            Username = "Gosen",
            Password = "123456",
            InfoRequestParameters = new GetPlayerCombinedInfoRequestParams
            {
                GetUserAccountInfo = true
            }
        }, result=> {
            isim.text = result.InfoResultPayload.AccountInfo.Username;
        }, error=>{
            Debug.LogError(error.GenerateErrorReport());
        });

But, since you are using LoginWithPlayFab which uses Username as a parameter, you can put the Username entered by the player to the isim.text directly when the player login successfully.

void OnLoginSuccess(LoginResult result) {

	messageText.text = "Giriş Yapıldı";

        Debug.Log("Giriş Başarılı");

        Panel.SetActive(false);

	isim.text = usernameInput.text;

     }
0 Likes 0 ·
Gosen Gao avatar image
Gosen Gao answered

-- In short, I want to keep the user's username and password in memory until the user logs out.

You can use PlayerPrefs to save the user's info and use them to do the silent login.

-- I want the username to turn into isim text and stay there forever.

The loginResult will default contain the user's DisplayName, if you want to get the user's UserName, you can add a InfoRequestParameters to the login request to get the UserAccountInfo which contains the username. And then you can put the username to the text you want.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

omerdruu avatar image
omerdruu answered

Thank you so much. Problem solved

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.