Unity - Authentication
Estimated Time
Configuring Catena authentication in your Unity project is estimated to take <10 minutes.
Prerequisites
In order to use the Catena Unity SDK, you must be running Catena, either locally or deployed elsewhere.
- You must have completed the Unity Quickstart Guide.
Adding Authentication
The first real call you'll want to make to Catena is authenticating a player to register a session. Catena supports a variety of authentication providers. For the purposes of this guide, we will be using our UNSAFE provider, which is used for development workflows.
For more information on Authentication in Catena, such as different authentication types, refer to the Authentication documentation.
Logging A Player In
- Reminder, if you have not yet completed the Unity Quickstart Guide, please do so now.
- Determine the Scene you would like to add Catena to. We will henceforth refer to this as "your Scene".
- Create an empty
GameObject
in your Scene.- Rename it to
CatenaStatus
. - Add a
TextMeshPro - Text
component to it.
- Rename it to
- Create an empty
GameObject
in your Scene.- Rename this
GameObject
toCatenaPlayer
. - Add the
CatenaPlayer
component to yourGameObject
. - Link the
CatenaStatus
object in the Status Text field.
- Rename this
What Is The Catena Player?
The Catena Player component houses functionality for operations related to a specific player.
- Import the Catena Unity SDK to whatever Script you'd like to call Catena from.
using Catena.CatenaAuthentication;
using CatenaUnitySDK;
- From the function you'd like to make calls to Catena from, write the following code.
var username = "test01";
var catenaPlayer = FindObjectOfType<CatenaPlayer>();
catenaPlayer.OnAccountLoginComplete += (object sender, Catena.CatenaAccounts.Account account) =>
{
Debug.Log($"Player Logged In With ID: {account.Id}");
};
catenaPlayer.CompleteLogin(Provider.Unsafe, username); // This will trigger the catenaPlayer.OnAccountLoginComplete callback when login is completed
The testXX
pattern is a specially recognized development username. By passing in test01
to the CompleteLogin
function, we perform a low-friction UNSAFE login for development environments. You may update this username to be any variation of testXX
to login to different accounts.
Logging A Player Out
Let's update the above code to first log a player in, and then immediately log that player out. Here is an entire class that ties it all together.
using UnityEngine;
using Catena.CatenaAuthentication;
using CatenaUnitySDK;
public class SceneManager : MonoBehaviour
{
bool _playerLoggedIn = false;
bool _loggingOut = false;
void Start()
{
RegisterCallbacks();
LoginPlayer();
}
void Update()
{
if (_playerLoggedIn && !_loggingOut)
{
LogoutPlayer();
}
}
void RegisterCallbacks()
{
var catenaPlayer = FindObjectOfType<CatenaPlayer>();
// Login Callback
catenaPlayer.OnAccountLoginComplete += (object sender, Catena.CatenaAccounts.Account account) =>
{
Debug.Log($"Player Logged In With ID: {account.Id}");
_playerLoggedIn = true;
};
// Logout Callback
catenaPlayer.OnSessionInvalid += (_, _) =>
{
Debug.Log("Player Logged Out");
_playerLoggedIn = false;
};
}
void LoginPlayer()
{
var catenaPlayer = FindObjectOfType<CatenaPlayer>();
var username = "test01";
Debug.Log("Logging Player In");
catenaPlayer.CompleteLogin(Provider.Unsafe, username);
}
void LogoutPlayer()
{
var catenaPlayer = FindObjectOfType<CatenaPlayer>();
_loggingOut = true;
Debug.Log("Logging Player Out");
catenaPlayer.Logout();
}
}
What Next?
Now that you've successfully authenticated a player you can now make authenticated calls against Catena, such as matchmaking players together.
Unity Matchmaking
Matchmake players together