docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    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 an ICompositeAuthenticator in PlatformServices

    To instantiate an ICompositeAuthenticator interface in the PlatformServices class, follow these steps:

    1. Create an IAuthenticationPlaformSupport, and an IHttpClient interface, then get the references to an IAppIdProvider and IAppNamespaceProvider interfaces from the UnityCloudPlayerSettings.Instance singleton.
    2. Inject them in the ServiceConnectorFactory.Create method to build an ICompositeAuthenticator.
    3. Add the following references to the created instance of the ICompositeAuthenticator:
      1. A private reference
      2. A public reference
    4. Initialize the ICompositeAuthenticator and any other services in the InitializeAsync method.
    5. Shutdown the ICompositeAuthenticator and any other services in the Shutdown method.
    public static class PlatformServices
    {
        static ICompositeAuthenticator s_CompositeAuthenticator;
    
        public static ICompositeAuthenticator CompositeAuthenticator => s_CompositeAuthenticator;
    
        public static async Task InitializeAsync()
        {
            var platformSupport = PlatformSupportFactory.GetAuthenticationPlatformSupport();
            var httpClient = new UnityHttpClient();
            var playerSettings = UnityCloudPlayerSettings.Instance;
    
            var serviceConnector = ServiceConnectorFactory.Create(platformSupport, httpClient, playerSettings, playerSettings);
    
            s_CompositeAuthenticator = serviceConnector.CompositeAuthenticator;
    
            await s_CompositeAuthenticator.InitializeAsync();
        }
    
        public static void Shutdown()
        {
            (s_CompositeAuthenticator as IDisposable)?.Dispose();
            s_CompositeAuthenticator = null;
        }
    
     }
    

    Tie the ICompositeAuthenticator 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.

    Note

    The ICompositeAuthenticator interface that was created using the ServiceConnectorFactory.Create method can activate the interactive flow or the service account flow. So, it's not guaranteed that the activated flow is the interactive one. This means that 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 a UI element.

    1. Create Login and Logout buttons in your scene.

      Creating buttons in the scene

    2. Create a LoginManager GameObject and attach a new LoginManager monobehavior to it. This script links your UI with Identity's authentication engine.

    3. Update the LoginManager script so it references your buttons and an IAuthenticationStateProvider interface.

    4. Update the Awake method to do the following:

      1. Retrieve the ICompositeAuthenticator reference from PlatformServices.
      2. Subscribe to the AuthenticationStateChanged event.
      3. Subscribe to the buttons' onClick events.
      4. Call ApplyAuthenticationState to update the UI when the scene loads.
    5. Make sure the subscriptions are cleaned up in OnDestroy.

    6. 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 the AuthenticationState and the RequiresGUI property that determines whether the UI is relevant in the selected authentication flow.

    7. Define the behaviors for your buttons by calling the appropriate methods.

      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.

    8. Test your scene to see how the buttons update along with the authentication state of the user.

    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 class provides.

    See an example of a service that expects an IServiceAuthorizer.

    In This Article
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)