Home / Articles

Article image for [PlayFab] Generating IDs and Logging In [Unity]

[PlayFab] Generating IDs and Logging In [Unity]

Published: 2020/05/25 Updated: 2020/05/29

Version used in this article

  • PlayFab SDK: 2.86.2005 18

Introduction

PlayFab has many features, but the first thing you need to do to use them is log in.

Two Types of Login: Anonymous Login and Login Methods That Support Account Recovery

Broadly speaking, PlayFab offers two login methods.

Anonymous login

This is the simplest login method.

Because the user does not need to enter any information, the game can handle login automatically.

That means you do not end up with things like “Logging in is such a hassle, I might as well quit.” (important)

The downside is that if the player loses the device, recovery becomes difficult.

Functions for anonymous login

Example implementation of anonymous login


using UnityEngine;
using PlayFab;
using PlayFab.ClientModels;

// Sample implementation using LoginWithCustomID
public void Login (string id) {
	bool shouldCreateAccount = string.IsNullOrEmpty(id);

	PlayFabClientAPI.LoginWithCustomID(
		new LoginWithCustomIDRequest {
			CustomId = shouldCreateAccount ? CreateNewId() : id,
			CreateAccount = shouldCreateAccount
		},
		result => {
			// Success handling
			Debug.Log("Login successfully");
		},
		error => {
			// Failure handling
			Debug.LogError(error.GenerateErrorReport());
		}
	);
}

// Generate a unique ID
string CreateNewId () {
	return System.Guid.NewGuid().ToString();
}

Login Methods That Support Account Recovery

These are login methods that let players recover their account if something happens to their device.

However, it requires the player to provide information such as the following:

  • Authenticate through an external provider (Facebook, iOS, Google, and so on)
  • Enter a username or email address and a password

For that reason, if you ask the player to use one of these login methods right after installing the game, you risk hearing, “Logging in is such a hassle, I might as well quit.” Be careful about when you introduce them.

If you combine them with anonymous login, you can let players start anonymously and then prompt them to add a login method that supports account recovery later. (See: Quickstart for account linking)

Functions for Login Methods That Support Account Recovery

You can also log in through third-party services using the following methods:

Closing Thoughts

At the moment, I am still in the middle of introducing PlayFab into a project and learning as I go.

If I have made any mistakes, I would appreciate it if you let me know.

References

Share

Twitter