question

marcmptest avatar image
marcmptest asked

Getting player's ID in unity?

Hello, please forgive me as I am new to playfab, may I know what would be the best way to retrieve the current logged in user's ID in unity? Specifically, I would like to retrieve Player ID (title) from the game manager through an API call, there is GetAccountInfo in the documentation, but I don't know how to manipulate it, may I have an example snippet on how to get it to work in unity c#?

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.

Brian Jensen avatar image Brian Jensen commented ·
public void LoginAccount() {
        var request = new LoginWithPlayFabRequest() { Username = UserName, Password = Password };
        PlayFabClientAPI.LoginWithPlayFab(request, Success, Failed);
}


private void Failed(PlayFabError obj) {
        Debug.Log("Failed to get");
}


private void Success(LoginResult obj) {
	Debug.Log(obj.PlayFabId);
}

0 Likes 0 ·
madmojoman avatar image
madmojoman answered

The PlayFabId returned in the result is the player's id I believe you're looking for. It's just a string. So setting up a reference to it locally will allow you to use it in other areas for other calls in to CloudScript or a server. You're already showing it sets up a Debug.Log that will display the PlayFabId on success, so if you are seeing that displayed in the console you're all set to use the example below.

You can check the documentation from the "Help" tab on the website and click on the "PlayFab documentation site" link on that page to find more details about any of the requests and results right there.

// Public string variable to hold the PlayFabId

public string LocalPlayer_PlayFabId = "NotSet";

// When receive the successful LoginResult obj

LocalPlayer_PlayFabId = obj.PlayFabId;

// Using it elsewhere...

Debug.Log("This is the player's PlayFabId: " + LocalPlayer_PlayFabId);

10 |1200

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

Talha avatar image
Talha answered
string MyPlayfabID;

void GetAccountInfo() {
		GetAccountInfoRequest request = new GetAccountInfoRequest();
		PlayFabClientAPI.GetAccountInfo(request, Successs, fail);
	}


void Successs (GetAccountInfoResult result)
	{      
		
		MyPlayfabID = result.AccountInfo.PlayFabId;

	}


void fail(PlayFabError error){

		Debug.LogError (error.GenerateErrorReport ());
	}


there are many ways to get the player's Playfab ID. This being the most efficient in my view.Even if your player uses another phone to log into his/her existing account, it would persist and wont cause errors.

10 |1200

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

ichigomashimaro77 avatar image
ichigomashimaro77 answered

Another easy way to get it is in the OnLogin method. The login result contains the playfab ID. I know this is an old post, just leaving this here for anyone searching, Playfab's constant web requests can be a pain, i wish they stored more data locally.

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.