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:
Overview
To get user information in your scene, perform the following procedures:
- Instantiate a UserInfoProvider in PlatformServices
- Leverage the UserInfoProvider in your scene
Instantiate a UserInfoProvider in PlatformServices
To instantiate a UserInfoProvider in the PlatformServices class, see the following steps:
Add the following references in the
PlatformServicesclass if they're not already present:- A public reference to
IUserInfoProviderandCloudConfiguration A private reference to
ServiceHttpClient,UserInfoProviderandCloudConfigurationstatic ServiceHttpClient s_ServiceHttpClient; static UserInfoProvider s_UserInfoProvider; static CloudConfiguration s_CloudConfiguration; public static IUserInfoProvider UserInfoProvider => s_UserInfoProvider; public static CloudConfiguration CloudConfiguration => s_CloudConfiguration;
- A public reference to
Initialize the services in the
InitializeAsyncmethod. The Integrate authentication in your scene guide includes definitions of thes_HttpClient,AccessTokenProvider, andplayerSettingsvariables.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); // ... }Shutdown the services in the
Shutdownmethod.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:
In your scene, create a
Textfield that displays the current authentication state (or the username if the user is logged in).
Create a
UserNameUpdaterscript and attach it to the LoginManager GameObject. This script fills the text with the correct values based on the authentication state.Update the
UserNameUpdaterclass so it references yourTextfield,IAuthenticator, andIUserInfoProvider.public class UserNameUpdater : MonoBehaviour { [SerializeField] Text m_Text; IAuthenticator m_Authenticator; IUserInfoProvider m_UserInfoProvider; }The
Awakemethod should retrieve services fromPlatformServices. You can then subscribe (and unsubscribe inOnDestroy) to theAuthenticationStateChangedevent.void Awake() { m_Authenticator = PlatformServices.Authenticator; m_UserInfoProvider = PlatformServices.UserInfoProvider; m_Authenticator.AuthenticationStateChanged += OnAuthenticationStateChanged; } void OnDestroy() { m_Authenticator.AuthenticationStateChanged -= OnAuthenticationStateChanged; }The
OnAuthenticationStateChangedmethod updates the text based on the current authentication state, and theGetUserInfoAsyncmethod 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; } }Select Play. The text updates in real time based on the authentication state.