Sample | Integrate Identity in a Unity project
You can use the Identity samples to retrieve a valid access token for a user. The samples outline how to initialize the services required to fetch this token and how to use that token to retrieve information about the user.
Prerequisites
To use the Identity sample, you require the following:
Authentication sample
The Authentication sample outlines an interactive user login flow.
Installation of Authentication
To install the sample, follow these steps:
- In your Unity project, go to Window > Package Manager > Digital Twins Identity.
Expand Samples and select Import beside the Authentication sample.
After the import process completes, you can view the imported assets under the
Assets/Samples/Digital Twins Identity
folder.
Run the Authentication sample
To run the sample, follow these steps:
- In your Unity project, go to File > Open Scene.
- Go to
Assets/Samples/Digital Twins Identity/<package-version>/Authentication/AuthenticationSample.unity
and run the scene. - In the Game view, select Login if you are logged out.
- Log into the browser window that launches with your Unity ID account.
- Return to the sample scene to confirm that you are logged in. Note: Your device stores a refresh token until you log out. This token lets the Identity service automatically attempt to log you in when you relaunch the sample.
- (Optional) Relaunch the sample, without logging out, to test the automatic login.
Main components for Authentication
This section describes the scripts that make up the Authentication sample.
Login manager script
The LoginManager
class is the sample's main script.
The LoginManager
gets a reference to an IInteractiveAuthenticator
, which calls the login and logout methods and exposes an AuthenticationStateChanged
event. The LoginManager
uses the AuthenticationStateChanged
event to update the state of the UI.
To open the login manager script, go to the Assets/Samples/Digital Twins Identity/<package-version>/Common/Scripts/LoginManager.cs
file.
Platform services script
The PlatformServices
class initializes and disposes of the IInteractiveAuthenticator
and its dependencies. The PlatformServices
class exposes the authenticator through a property so any component that requires authentication (for example, the LoginManager
) can retrieve the authenticator.
The PlatformServices
class has two accompanying classes called PlatformServicesInitialization
and PlatformServicesShutdown
that call the initialization and shutdown methods through Unity's standard Monobehaviour
methods Awake()
and OnDestroy()
.
To open the platform services script, go to the Assets/Samples/Digital Twins Identity/<package-version>/Common/Scripts/PlatformServices.cs
file.
Get User Information sample
The Get User Information sample outlines how to use the Identity service to retrieve information about a logged-in user.
Installation of Get User Information
To install the sample, follow these steps:
- In your Unity project, go to Window > Package Manager > Digital Twins Identity.
- Expand Samples and select Import beside the Get User Information sample.
After the import process completes, you can view the imported assets under the
Assets/Samples/Digital Twins Identity
folder.
Run the Get User Information sample
To run the sample, follow the Run the Authentication sample procedure but replace step 2 with the following: Go to Assets/Samples/Digital Twins Identity/<package-version>/GetUserInformation/GetUserInfoSample.unity
and run the scene.
After you log into the sample, it displays your username.
Main components for Get User Information
This section describes the additional script that makes up the Get User Information sample.
User name updater script
The UserNameUpdater
class handles an IAuthenticator
and IUserInfoProvider
, which are initialized through the PlatformServices
class. The IUserInfoProvider
can retrieve the UserInfo
for a logged in user, from which the username is retrieved. The UI then updates with the username.
To open the user name updater script, go to the Assets/Samples/Digital Twins Identity/<package-version>/Get User Information/Scripts/UserNameUpdater.cs
file.