Integrate authentication in your scene
This section explains how to set up your scene to integrate an authentication layer to communicate with Unity Cloud services.
Prerequisites
- Follow the Installation instructions.
- Follow the Get started instructions.
- Follow the Best practices: dependency injection guide.
Overview
To integrate authentication in your scene, perform the following procedures:
- Instantiate a
CompositeAuthenticator
inPlatformServices
. - Tie Identity's features with a UI.
- Leverage the access token to communicate with other Unity Cloud services.
Instantiate a CompositeAuthenticator in PlatformServices
To instantiate a CompositeAuthenticator
in the PlatformServices
class, follow these steps:
- Use the
CompositeAuthenticatorSettingsBuilder
to build a prioritized list of supportedIAuthenticator
. - Create an instance of the
CompositeAuthenticator
using theCompositeAuthenticatorSettings
as argument. - Add the following references of the created instance of the
CompositeAuthenticator
: - A private read-only reference
- A public reference as
ICompositeAuthenticator
,IAuthenticationStateProvider
andIAccessTokenProvider
- Initialize the
CompositeAuthenticator
and any other services in theInitializeAsync
method. - Shutdown the
CompositeAuthenticator
and any other services in theShutdown
method.
Tie the CompositeAuthenticator with UI
If your application needs to support the interactive login flow, create a login UI and link it to the IUrlRedirectionAuthenticator
methods to achieve interactive login and logout.
Please note that if the CompositeAuthenticatorSettings
added automated flow or preauthenticated flow IAuthenticator
, it's not guaranteed that the activated flow is interactive. This means some parts of the UI need to be hidden or disabled from code, depending on the situation. Validate this by checking the value of the ICompositeAuthenticator.RequiresGUI
property when managing UI element.
- Create Login and Logout buttons in your scene.
- Create a LoginManager GameObject and attach a new LoginManager MonoBehaviour to it. This script links your UI with Identity's authentication engine.
- Update the
LoginManager
script so it references your buttons and anIAuthenticationStateProvider
. - Update the
Awake
method to do the following:- Retrieve the
CompositeAuthenticator
reference fromPlatformServices
. - Subscribe to the
AuthenticationStateChanged
event. - Subscribe to the buttons'
onClick
events. - Call
ApplyAuthenticationState
to update the UI when the scene loads.
- Retrieve the
- Make sure the subscriptions are cleaned up in
OnDestroy
. - Implement the
ApplyAuthenticationState
method to update your buttons based on the current authentication state. Make your buttons interactive in specific circumstances, otherwise you risk errors and exceptions. To determine if you should display your buttons, rely on theAuthenticationState
and theRequiresGUI
property that determines whether the UI is relevant in the selected authentication flow. - Define the behaviors for your buttons by calling the appropriate methods.
- Test your scene to see how the buttons update along with the authentication state of the user.

Note: Only an interactive login flow (like the one exposed by theIUrlRedirectionAuthenticator
interface) requires these methods. If the flow isn't interactive, callingLoginAsync
orLogoutAsync
throws exceptions.
Leverage the access token to communicate with other Unity Cloud services
After you set up your login flow, communication with other Unity Cloud services is possible. All Unity Cloud services expect an instance of IAccessTokenProvider
, which the PlatformServices
provides.
For an example of a service that expects an IAccessTokenProvider
, refer to the Get user information guide.