question

raluca1s98 avatar image
raluca1s98 asked

Display username in the room created/joined

Hey , I am trying to display my username that my player created when they register/login in the game , but I'm very confuse how to do it. I look over the documentation but still I don't know how I can do it. This is the script I am using and I am using Photon too. I was thinking to create this game object "displayName" to the player but I don't know how to make it work with this script that I have..

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PlayFab;
using PlayFab.ClientModels;
using UnityEngine.UI;
using Photon.Pun;
using Photon.Realtime;
using System;


public class PlayFabAuth : MonoBehaviour
{
    public MPManager mp;


    public InputField user;
    public InputField pass;
    public InputField email;
    public Text message;
    
    


    public bool IsAuthenticated = false;
    LoginWithPlayFabRequest loginRequest;
    // Start is called before the first frame update
    void Start()
    {
        PlayFabSettings.TitleId = "D36F6";
       


        email.gameObject.SetActive(false);
    }


    // Update is called once per frame
    public void Login()
    {        
        loginRequest = new LoginWithPlayFabRequest();
        loginRequest.Username = user.text;
        loginRequest.Password = pass.text;


        PlayFabClientAPI.LoginWithPlayFab(loginRequest, result => {
            
            IsAuthenticated = true;
            mp.ConnectToMaster();
            message.text = "Welcome " + user.text + ", Conecting..";
            Debug.Log("You are now logged in");
            
            


        }, error =>
        {
            
            IsAuthenticated = false;
            message.text = "Failed to login[" + error.ErrorMessage + "]";
            email.gameObject.SetActive(true);
            Debug.Log(error.ErrorMessage);
        }, null);
}
    public void Register(){
        RegisterPlayFabUserRequest request= new RegisterPlayFabUserRequest();
        request.Email = email.text;
        request.Username = user.text;
        request.Password = pass.text;
        








        PlayFabClientAPI.RegisterPlayFabUser(request, result => {
            message.text = "Your account has been created";


        }, error =>
         {


             message.text = "Failed to create your account[" + error.ErrorMessage +"]";
         });
    }
   


}


unity3d
10 |1200

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

1 Answer

·
Sarah Zhang avatar image
Sarah Zhang answered

You can use the following code to access the “user display name” which in the responses of Login API. Please test the PlayFab API via external REST API testing tools, such as Postman, to check the API response before you use PlayFab API with Unity SDK. You can derive how to get the fields’ values based on the responses. Besides, you can also search the keywords in the corresponding API references to find the field you want.

... 
 PlayFabClientAPI.LoginWithPlayFab(new PlayFab.ClientModels.LoginWithPlayFabRequest
        {
            Username = "",
            Password=""
        }, result => {
            var displayName= result.InfoResultPayload.PlayerProfile.DisplayName;
            Debug.Log(displayName);
        }, error => {
        });
...
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.