LoginWithCustomID in Unity

Get started with the PlayFab Client library for Unity on Authentication mechanism discovery. We will first perform some test with a basic structure, then move to a more complete approach for mobile devices and perform the same in an MVC ASP.Net Core Website.

This quickstart helps you make your PlayFab API call in the using the Client library for C# in Unity. Before continuing, make sure you have a PlayFab account and are familiar with the PlayFab Game Manager. You can also review how to manage authentication in C# Console

If you need an overview about solutions, check Player Authentication mechanisms in PlayFab

You can found the full code on YAKAGameStudio/Learn (github.com)


Download & Install PlayFab SDK

Follow How to install PlayFab into Unity setup the prerequisites

Set up your Authentication Call

You want to go through the final step ? check this file on GitHub

  1. In Create a new Game Object, and rename it to PlayFab.
  2. In the Assets window, Right-click and select Create > C# Script. Name the script GetStarted.
  3. Add it to the Game Object as shown on the picture below.
  4. Double-click the file to open it in a code-editor.
  5. In your code editor, replace the contents of GetStarted.cs with the code shown below and save the file.

For this demo, all code lines will stay into the GetStarted.cs file. We will see later how to structure the authentication class

Initiate request header

To initiate your request, you should define how CustomID look like and behave.

var LoginRequest = new LoginWithCustomIDRequest
	// Your Title setup in PlayFab SDK
    TitleId = PlayFabSettings.TitleId,
   // Custom unique identifier for the user, use anything you want.
   CustomId = SystemInfo.deviceUniqueIdentifier,
   // Automatically create a PlayFab account if one is not currently linked to this ID.
   CreateAccount = true,
   // Request additional Information
   InfoRequestParameters = LoginRequestParams

You can also request additional information on login to initialize your game or your player profile.

For more information see : getplayercombinedinforequestparams

var LoginRequestParams = new GetPlayerCombinedInfoRequestParams
   GetPlayerProfile = true,
   GetUserVirtualCurrency = true

Call PlayFab API

You are ready to send your authentication request and catch the answer.

PlayFabClientAPI.LoginWithCustomID(LoginRequest, OnLoginSuccess, OnLoginError);

Manage PlayFab answer

We create 2 new functions that will print confirmation of the API call using messages written to the console output.

private void OnLoginSuccess(LoginResult result)
  Debug.Log("Congratulation, you are authenticated");
  Debug.LogFormat("Login Success: {0}", result.PlayFabId);
  Debug.LogFormat("Session: {0}", result.SessionTicket);

private void OnLoginError(PlayFabError error)
  Debug.LogWarning("Something went wrong with your first API call.  :(");
  Debug.LogError("Here's some debug information:");

You can extend the OnLoginSuccess with information requested with the LoginRequestParams. You want to review the full code source or detect an issue? Please use: YAKAGameStudio/Learn (github.com)


If you follow this step-by-step to perform LoginWithCustomID in Unity, you should have the following in your Unity Console

Share your love
Default image
I’m a Microsoft employee, working on Technology Strategy for a large Enterprise account. You can easily assume / understand that I will not covert non-Microsoft Technologies. I will focus on how you can leverage Azure Game Stack for your Game. Even if I’ve some knowledge on infrastructure and development – due to my 20 years spent in consulting, I will assume that I don’t know anything. So, for each post, I’ll provide guidance and/or links related to training. This is sometimes a great habit to do a full knowledge refresh. Specially when facing the cloud technologies. If you are a senior Developer, please be soft. You may already know everything I am describing there.
Articles: 8

Leave a Reply