docs.unity3d.com
  • Manual
  • Scripting API
  • Changelog
  • License

    • Identity
      • Installation
    • Get started
    • Best practice | Dependency injection
    • Integrate authentication in your scene
    • Get user information
    • Test with WebGL in a localhost
    • Sample | Integrate Identity in a Unity project

    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:

    • An installed Identity package
    • A valid Unity ID

    Authentication sample

    The Authentication sample outlines an interactive user login flow.

    Installation of Authentication

    To install the sample, follow these steps:

    1. In your Unity project, go to Window > Package Manager > Digital Twins Identity.
    2. Expand Samples and select Import beside the Authentication sample.

      Screenshot of the Package Manager's Identity samples

      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:

    1. In your Unity project, go to File > Open Scene.
    2. Go to Assets/Samples/Digital Twins Identity/<package-version>/Authentication/AuthenticationSample.unity and run the scene.
    3. In the Game view, select Login if you are logged out.
    4. Log into the browser window that launches with your Unity ID account.
    5. 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.
    6. (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:

    1. In your Unity project, go to Window > Package Manager > Digital Twins Identity.
    2. 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.

    Back to top Generated by DocFX
    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