question

Victor Nedelea avatar image
Victor Nedelea asked

How to register player at startup in Unity?

So, I simply want to create a registration menu and I've already done that. What I need help with is this:

1)If a player enters for the first time in the application, it means that it has no DeviceID and thus it needs to be created and only for this time, the player will have to insert the Username, Email and Password and after clicking the Submit button the app should continue.

2)If the player enters the app from the same device, it will automatically Log In without asking for any details since the Device ID exists in the database.

Thanks.

Player Dataunity3d
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

To implement your requirements, you need to keep the field “CreateAccount” of login API such as LoginWithAndroidDeviceID as “true” and do the API call in the Unity Start() function. Besides, you should add the judgment for the response of login API, if the value of “NewlyCreated” is true, it means it’s the first time for the player to enter the game, the player needs to provide the username, email, and password, and you should call the API AddUsernamePassword to submit the information. If the value of “NewlyCreated” false, it means it’s not the player’s first login, the player can enter the game directly. You can refer to the document -- Login basics and best Practices - PlayFab | Microsoft Docs for more information about best practice of integrating anonymous login and recoverable login.

The possible code sample could be something like this.

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


public class LoginSample : MonoBehaviour
{
    public InputField UsernameInputField;
    public InputField EmailInputField;
    public InputField PasswordInputField;


    void Start()
    {
        var request = new LoginWithAndroidDeviceIDRequest { AndroidDeviceId = "GettingStartedGuide", CreateAccount = true };
        PlayFabClientAPI.LoginWithAndroidDeviceID(request, OnLoginSuccess, OnFailure);
    }


    private void OnLoginSuccess(LoginResult result)
    {
        if (result.NewlyCreated)
        {
            //Display InputField UI
            var addUsernamePasswordRequest = new AddUsernamePasswordRequest { Username = UsernameInputField.text, Email = EmailInputField.text, Password = PasswordInputField.text };
            PlayFabClientAPI.AddUsernamePassword(addUsernamePasswordRequest, OnAddSuccess, OnFailure);
        }
        else
        {
            //Enter the game logic
        }
    }


    private void OnAddSuccess(AddUsernamePasswordResult result)
    {
        //Enter the game logic 
    }


    private void OnFailure(PlayFabError error)
    {
        Debug.LogError(error.GenerateErrorReport());
    }
}
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.