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
PlatformServices
class if they're not already present:- A public reference to
IUserInfoProvider
andCloudConfiguration
A private reference to
ServiceHttpClient
,UserInfoProvider
andCloudConfiguration
static 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
InitializeAsync
method. The Integrate authentication in your scene guide includes definitions of thes_HttpClient
,AccessTokenProvider
, andplayerSettings
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); // ... }
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:
In your scene, create a
Text
field that displays the current authentication state (or the username if the user is logged in).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.Update the
UserNameUpdater
class so it references yourText
field,IAuthenticator
, andIUserInfoProvider
.public class UserNameUpdater : MonoBehaviour { [SerializeField] Text m_Text; IAuthenticator m_Authenticator; IUserInfoProvider m_UserInfoProvider; }
The
Awake
method should retrieve services fromPlatformServices
. You can then subscribe (and unsubscribe inOnDestroy
) to theAuthenticationStateChanged
event.void Awake() { m_Authenticator = PlatformServices.Authenticator; m_UserInfoProvider = PlatformServices.UserInfoProvider; m_Authenticator.AuthenticationStateChanged += OnAuthenticationStateChanged; } void OnDestroy() { m_Authenticator.AuthenticationStateChanged -= OnAuthenticationStateChanged; }
The
OnAuthenticationStateChanged
method updates the text based on the current authentication state, and theGetUserInfoAsync
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; } }
Select Play. The text updates in real time based on the authentication state.