Use case: 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.
Before you start
- Follow the Installation instructions.
- Follow the Get started instructions.
- Follow the Best practices: dependency injection guide.
How do I...?
Instantiate a CompositeAuthenticator in PlatformServices
To instantiate a CompositeAuthenticator in the PlatformServices class, follow these steps:
- Use the
CompositeAuthenticatorSettingsBuilderto build a prioritized list of supportedIAuthenticator. - Create an instance of the
CompositeAuthenticatorusing theCompositeAuthenticatorSettingsas argument. - Add the following references of the created instance of the
CompositeAuthenticator: - A private read-only reference
- A public reference as
ICompositeAuthenticator,IAuthenticationStateProviderandIAccessTokenProvider - Initialize the
CompositeAuthenticatorand any other services in theInitializeAsyncmethod. - Shutdown the
CompositeAuthenticatorand any other services in theShutdownmethod.
public static class PlatformServices
{
static CompositeAuthenticator s_CompositeAuthenticator;
public static ICompositeAuthenticator CompositeAuthenticator => s_CompositeAuthenticator;
public static IServiceAuthorizer serviceAuthorizer => s_CompositeAuthenticator;
public static async Task InitializeAsync()
{
var playerSettings = UnityCloudPlayerSettings.Instance;
var httpClient = new UnityHttpClient();
var serviceHostResolver = UnityRuntimeServiceHostResolverFactory.Create();
var compositeAuthenticatorSettings = new CompositeAuthenticatorSettingsBuilder(httpClient, PlatformSupportFactory.GetAuthenticationPlatformSupport(), serviceHostResolver, playerSettings)
.AddDefaultBrowserAuthenticatedAccessTokenProvider(playerSettings)
.AddDefaultPkceAuthenticator(playerSettings)
.Build();
s_CompositeAuthenticator = new CompositeAuthenticator(compositeAuthenticatorSettings);
await s_CompositeAuthenticator.InitializeAsync();
}
public static void Shutdown()
{
s_CompositeAuthenticator = null;
}
}
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
LoginManagerscript so it references your buttons and anIAuthenticationStateProvider. - Update the
Awakemethod to do the following:- Retrieve the
CompositeAuthenticatorreference fromPlatformServices. - Subscribe to the
AuthenticationStateChangedevent. - Subscribe to the buttons'
onClickevents. - Call
ApplyAuthenticationStateto update the UI when the scene loads.
- Retrieve the
- Make sure the subscriptions are cleaned up in
OnDestroy. - Implement the
ApplyAuthenticationStatemethod 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 theAuthenticationStateand theRequiresGUIproperty 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 the `IUrlRedirectionAuthenticator` interface) requires these methods. If the flow isn't interactive, calling `LoginAsync` or `LogoutAsync` throws exceptions.
public class LoginManager : MonoBehaviour
{
[SerializeField]
Button m_LoginButton;
[SerializeField]
Button m_LogoutButton;
ICompositeAuthenticator m_CompositeAuthenticator;
void Awake()
{
m_CompositeAuthenticator = PlatformServices.CompositeAuthenticator;
m_CompositeAuthenticator.AuthenticationStateChanged += ApplyAuthenticationState;
m_LoginButton.onClick.AddListener(new UnityEngine.Events.UnityAction(OnLoginButtonClick));
m_LogoutButton.onClick.AddListener(new UnityEngine.Events.UnityAction(OnLogoutButtonClick));
}
void Start()
{
ApplyAuthenticationState(m_CompositeAuthenticator.AuthenticationState);
}
void OnDestroy()
{
m_CompositeAuthenticator.AuthenticationStateChanged -= ApplyAuthenticationState;
m_LoginButton.onClick.RemoveAllListeners();
m_LogoutButton.onClick.RemoveAllListeners();
}
void ApplyAuthenticationState(AuthenticationState state)
{
switch (state)
{
case AuthenticationState.LoggedOut:
m_LoginButton.interactable = m_CompositeAuthenticator.RequiresGUI;
m_LogoutButton.interactable = false;
break;
case AuthenticationState.LoggedIn:
m_LoginButton.interactable = false;
m_LogoutButton.interactable = m_CompositeAuthenticator.RequiresGUI;
break;
case AuthenticationState.AwaitingInitialization:
case AuthenticationState.AwaitingLogin:
case AuthenticationState.AwaitingLogout:
m_LoginButton.interactable = false;
m_LogoutButton.interactable = false;
break;
}
}
async void OnLoginButtonClick()
{
await m_CompositeAuthenticator.LoginAsync();
}
async void OnLogoutButtonClick()
{
await m_CompositeAuthenticator.LogoutAsync();
}
}
Leverage the authorization headers 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 IServiceAuthorizer to apply authorization information to HTTP requests, which the PlatformServices provides.
For an example of a service that expects an IServiceAuthorizer, refer to the Get user information guide.