docs.unity3d.com
    Show / Hide Table of Contents

    Get user information

    This section explains how to set up your scene to get information about your application's user.

    Getting user information is an example of a Unity Cloud service that you can call after authenticating the user in your application. This service needs an Access Token to identify the user and authorize the call.

    Prerequisites

    To get user information, you must do the following:

    • Integrate authentication in your scene

    Overview

    To get user information in your scene, perform the following procedures:

    1. Instantiate a UserInfoProvider in PlatformServices
    2. Leverage the UserInfoProvider in your scene

    Instantiate a UserInfoProvider in PlatformServices

    To instantiate a UserInfoProvider in the PlatformServices class, see the following steps:

    1. Add the following references in the PlatformServices class if they're not already present:

      • A public reference to IUserInfoProvider and CloudConfiguration
      • A private reference to ServiceHttpClient, UserInfoProvider and CloudConfiguration

           static ServiceHttpClient s_ServiceHttpClient;
           static UserInfoProvider s_UserInfoProvider;
           static CloudConfiguration s_CloudConfiguration;
        
           public static IUserInfoProvider UserInfoProvider => s_UserInfoProvider;
           public static CloudConfiguration CloudConfiguration => s_CloudConfiguration;
        
    2. Initialize the services in the InitializeAsync method. The Integrate authentication in your scene guide includes definitions of the s_HttpClient, AccessTokenProvider, and playerSettings variables.

          public static async Task InitializeAsync()
          {
              // ...
      
              s_CloudConfiguration = UnityCloudConfigurationFactory.Create();
      
              s_ServiceHttpClient = new ServiceHttpClient(s_HttpClient, AccessTokenProvider, playerSettings);
              s_UserInfoProvider = new UserInfoProvider(s_ServiceHttpClient, s_CloudConfiguration);
      
              // ...
          }
      
    3. Shutdown the services in the Shutdown method.

          public static void Shutdown()
          {
              // ...
              s_ServiceHttpClient = null;
              s_UserInfoProvider = null;
              s_CloudConfiguration = null;
      
              // ...
          }
      

    Leverage the UserInfoProvider in your scene

    To leverage the UserInfoProvider in your scene, see the following steps:

    1. In your scene, create a Text field that displays the current authentication state (or the username if the user is logged in). Creating a text field in the scene

    2. Create a UserNameUpdater script and attach it to the LoginManager GameObject. This script fills the text with the correct values based on the authentication state.

    3. Update the UserNameUpdater class so it references your Text field, IAuthenticator, and IUserInfoProvider.

           public class UserNameUpdater : MonoBehaviour
           {
               [SerializeField]
               Text m_Text;
      
               IAuthenticator m_Authenticator;
               IUserInfoProvider m_UserInfoProvider;
           }
      
    4. The Awake method should retrieve services from PlatformServices. You can then subscribe (and unsubscribe in OnDestroy) to the AuthenticationStateChanged event.

           void Awake()
           {
               m_Authenticator = PlatformServices.Authenticator;
               m_UserInfoProvider = PlatformServices.UserInfoProvider;
      
               m_Authenticator.AuthenticationStateChanged += OnAuthenticationStateChanged;
           }
      
           void OnDestroy()
           {
               m_Authenticator.AuthenticationStateChanged -= OnAuthenticationStateChanged;
           }
      
    5. The OnAuthenticationStateChanged method updates the text based on the current authentication state, and the GetUserInfoAsync method can be called when the user is logged in.

          async void OnAuthenticationStateChanged(AuthenticationState state)
          {
              switch (state)
              {
                  case AuthenticationState.AwaitingLogin:
                  case AuthenticationState.AwaitingLogout:
                      m_Text.text = "...";
                      break;
                  case AuthenticationState.LoggedOut:
                      m_Text.text = "Logged out";
                      break;
                  case AuthenticationState.LoggedIn:
                      var userInfo = await m_UserInfoProvider.GetUserInfoAsync();
                      m_Text.text = userInfo.Name;
                      break;
              }
          }
      
    6. Select Play. The text updates in real time based on the authentication state.

    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023