Authentication in Unity

This tutorial will guide you through setting up Unity Authentication in a Unity project. Unity Authentication enables you to manage and authenticate users in your games using Unity's Identity toolkit. We'll cover the steps required to set up authentication, integrate it into your project, and implement sign-in functionality.

Prerequisites

  • Unity Hub and Unity Editor installed.
  • Unity project with Unity Services enabled.
  • Unity account and an active project ID.

Step 1: Set Up Unity Authentication in Unity Dashboard

  1. Log in to the Unity Dashboard.
  2. Select your project or create a new one.
  3. Navigate to Authentication under the "Services" section.
  4. Enable Authentication by clicking on the Activate button.
  5. Configure the Authentication settings as needed, such as user attributes, sign-in methods, and access controls.

Step 2: Install Unity Authentication Package

In order to use Unity Authentication in your project, you need to install the appropriate package.

  1. Open your Unity project.
  2. Navigate to Window > Package Manager.
  3. Search for Authentication in the Package Manager.
  4. Click on Install to add the Authentication package to your project.

Step 3: Set Up Initialization Script

To use authentication in your game, you need to initialize Unity services and authentication at runtime. Add the following code in a C# script (e.g., AuthenticationManager.cs), and attach it to a GameObject in your scene.

using UnityEngine;
using Unity.Services.Core;
using Unity.Services.Authentication;
using System.Threading.Tasks;

public class AuthenticationManager : MonoBehaviour
{
    async void Start()
    {
        await InitializeUnityServicesAsync();
    }

    private async Task InitializeUnityServicesAsync()
    {
        try
        {
            await UnityServices.InitializeAsync();
            Debug.Log("Unity Services initialized successfully.");

            if (!AuthenticationService.Instance.IsSignedIn)
            {
                await SignInAnonymously();
            }
        }
        catch (System.Exception e)
        {
            Debug.LogError($"Error initializing Unity Services: {e.Message}");
        }
    }

    private async Task SignInAnonymously()
    {
        try
        {
            await AuthenticationService.Instance.SignInAnonymouslyAsync();
            Debug.Log("Signed in anonymously.");
        }
        catch (System.Exception e)
        {
            Debug.LogError($"Error signing in anonymously: {e.Message}");
        }
    }
}

This script initializes Unity services when the game starts and signs the user in anonymously if they are not already signed in.

Step 4: Implement Sign-In with Unity Authentication

You can also provide options for specific sign-in methods, such as email or Google sign-in, depending on your game's requirements. Below is an example of how you might implement sign-in with Unity Authentication.

Example: Sign-In with Email and Password

using UnityEngine;
using Unity.Services.Authentication;
using System.Threading.Tasks;

public class AuthenticationManager : MonoBehaviour
{
    async void Start()
    {
        await InitializeUnityServicesAsync();
    }

    private async Task InitializeUnityServicesAsync()
    {
        try
        {
            await UnityServices.InitializeAsync();
            Debug.Log("Unity Services initialized successfully.");
        }
        catch (System.Exception e)
        {
            Debug.LogError($"Error initializing Unity Services: {e.Message}");
        }
    }

    public async Task SignInWithEmailAsync(string email, string password)
    {
        try
        {
            await AuthenticationService.Instance.SignInWithEmailAndPasswordAsync(email, password);
            Debug.Log("Signed in with email successfully.");
        }
        catch (System.Exception e)
        {
            Debug.LogError($"Error signing in with email: {e.Message}");
        }
    }
}

To call this method, create a UI form in Unity for users to input their email and password, and then call SignInWithEmailAsync from a UI button's onClick event.

Step 5: Sign Out

For user management, you may want to implement a sign-out function as well. Here’s how:

public void SignOut()
{
    AuthenticationService.Instance.SignOut();
    Debug.Log("Signed out successfully.");
}

Call this method whenever you want to log the user out of your game.

Conclusion

We covered how to set up Unity Authentication in your Unity project, including initialization, anonymous sign-in, email sign-in, and sign-out functionality. With Unity Authentication, you can manage your users more effectively and enhance security in your games. For more advanced setups, such as custom sign-in providers or linking multiple authentication methods, refer to the official Unity documentation.

Links
Unity 6