文档

​
​

Development

User Acquisition

Monetization

工业

Authentication

Unity SDK

Admin API

Client API

Open Unity Dashboard

Authentication

此页面不支持所选语言。
LiveOps
​
​
Authentication
  • Overview
  • Get started
  • Concepts
    • How Authentication works
    • Approaches to authentication
    • Sessions and cached players
    • Manage profiles
    • Manage player names
    • Manage linked external identities
    • Player prefs
    • Delete accounts
    • Authentication native SDK
    • Digital Services Act Notifications
  • Tutorials
  • Reference
  • Privacy and consent
  1. Unity Authentication

Authentication Native SDK

Implement player authentication with the Native SDK to sign in anonymously, refresh tokens, and manage player sessions with OpenID providers.
阅读时间5 分钟
最后更新于 4 个月前

Prerequisites

To get started with Unity Authentication, you need to do the following:
  1. Create a Unity ID.
  2. Create a project in the Unity Dashboard.

Get started

Follow these steps to get started with the Native SDK:
  1. In the Unity Dashboard, select Development > Settings to retrieve your Project ID from the Project settings page.
  2. Retrieve your environment name from the Environments page.
  3. Download and install the SDK from the Cloud Dashboard.
  4. Create a
    Unity::Authentication::AuthenticationClient
    object.
  5. Use the
    SetProjectId
    and
    SetEnvironmentName
    parameters on the AuthenticationClient object you created in the previous step to set those values to the ones from the Unity Dashboard.
  6. Run
    SignInAnonymously
    , and take the resulting
    PlayerId
    and
    IdToken
    from the
    AuthenticationSignInResponse
    .
    • Alternatively, after the callback has fired successfully, get
      PlayerId
      and
      IdToken
      by using
      AuthenticationClient::GetToken
      and
      AuthenticationClient::GetPlayerId
      .

Response structures

AuthenticationSignInCallback
and
AuthenticationSignInResponse
run and pass information when a
SignInAnonymously
or
RefreshToken
call is completed.
The
AuthenticationSignInResponse
contains the following information:
  • Token
    : the ID token that is returned by the
    AuthenticationSignIn
    call.
  • PlayerId
    : the player ID that is associated with the ID token.
  • ExpiresIn
    : an
    int
    value that represents the number of seconds before the token expires.
注意
An AuthenticationPlayer
PlayerId
contains references to all external platform sign-ins by this
PlayerId
when the external platform sign-ins are implemented.
The following is an example of an AuthenticationClient and sign-in callback:
unity::uaslib::AuthenticationClient client; client.SetEnvironmentName(UNITY_ENVIRONMENT_NAME); client.SetProjectId(UNITY_PROJECT_ID); Unity::Authentication::AuthenticationSignInCallback cb; cb = [](const unity::uaslib::AuthenticationSignInResponse &response) { std::cout << "Authentication Sign In Response:\n"; std::cout << "Token: " << response.Token << "\n"; std::cout << "PlayerId: " << response.PlayerId << "\n"; std::cout << "ExpiresIn: " << response.ExpiresIn << "\n"; }; client.SignInAnonymously(cb); //Running ClearSessionToken prior to another SignInAnonymously //will result in a new PlayerId client.ClearSessionToken(); client.SignInAnonymously(cb); client.RefreshToken(cb);

Refresh a token

After sign-in, the SDK can extend the authentication for a specific
PlayerId
for a session by refreshing the authentication token. To refresh the token, call
RefreshToken
, which will re-authenticate the same
PlayerId
and return a new token in the callback.
cb = [&tmpProcessing](const unity::uaslib::AuthenticationSignInResponse &response) { std::cout << "Authentication Sign In Response:\n"; std::cout << "Token: " << response.Token << "\n"; std::cout << "PlayerId: " << response.PlayerId << "\n"; std::cout << "ExpiresIn: " << response.ExpiresIn << "\n"; tmpProcessing = false; }; client.RefreshToken(cb);

Delete a player

Aa active Authentication session can use the
DeletePlayer
method to delete the currently signed-in player from the Authentication service. Upon a successful return from the service, the AuthenticationClient will clear the cached
Token
and
PlayerId
, and a new sign-in will be needed.
deleteCb = [](const unity::uaslib::AuthenticationDeletePlayerResponse &response) { std::cout << "Authentication Delete Player Response:\n"; std::cout << (response.IsSuccessful() ? "Completed Successful\n" : "Completed Failure\n"); tmpProcessing = false; };client.DeletePlayer(deleteCb);

OpenID sign-in, linking, and unlinking

Setup

Add an OpenID Connect ID Provider to the Unity Dashboard to use OpenID sign-ins. Navigate to Player Authentication > Identity Providers to set the OpenID Connect ID provider.

Sign-in

You can create new Unity Authentication sessions connected to the IdProvider after you have set up an OpenID ID Provider on the Unity Dashboard. Provide a token from the service and the IdProvider name, along with an optional
AuthenticationSignInCallback
to SignInOpenID to create the new session.
An associated AuthenticationPlayer containing all connected IdProviders is available through the
GetPlayer()
method.
auto IdToken = "AnOpenIdToken"; auto IdProvider = "OpenIdProviderName"; cb = [this](const unity::uaslib::AuthenticationSignInResponse &response) { std::cout << "Authentication Sign In Response:\n"; std::cout << "Token: " << response.Token << "\n"; std::cout << "PlayerId: " << response.PlayerId << "\n"; std::cout << "ExpiresIn: " << response.ExpiresIn << "\n"; }; client.SignInOpenID(IdToken, IdProvider, cb);

Linking

Using the
LinkOpenID()
method with an
IdToken
from the service, the
IdProvider
name, and an additional
AuthenticationLinkCallback
, you can associate an OpenID connection with an anonymous authentication session.
You can also add an additional OpenID connection to an existing OpenID-connected authentication session.
Unity::Authentication::AuthenticationLinkCallback cb; auto IdToken = "AnOpenIdToken"; auto IdProvider = "OpenIdProviderName"; cb = [this](const unity::uaslib::AuthenticationLinkPlayerResponse &response) { std::cout << "Authentication Sign In Response:\n"; std::cout << "PlayerId: " << response.PlayerId << "\n"; }; client.SignInOpenID(IdToken, IdProvider, cb);

Unlinking

You can remove an OpenID connection from an OpenID connected authentication session by running
UnlinkOpenID()
with the
IdProvider
and
EnvironmentId
. The
IdProvider
and
EnvironmentId
are accessible from the ExternalId’s vector of an
AuthenticationPlayer
retrieved from either the SignIn or Link responses or using the
GetPlayer()
method.
The following is an example of an AuthenticationClient and sign-in callback:
unity::uaslib::AuthenticationClient client; client.SetEnvironmentName(UNITY_ENVIRONMENT_NAME); client.SetProjectId(UNITY_PROJECT_ID); Unity::Authentication::AuthenticationSignInCallback cb; cb = [](const unity::uaslib::AuthenticationSignInResponse &response) { std::cout << "Authentication Sign In Response:\n"; std::cout << "Token: " << response.Token << "\n"; std::cout << "PlayerId: " << response.PlayerId << "\n"; std::cout << "ExpiresIn: " << response.ExpiresIn << "\n"; }; client.SignInAnonymously(cb); //Running ClearSessionToken prior to a another SignInAnonymously //will result in a new PlayerId client.ClearSessionToken(); client.SignInAnonymously(cb); client.RefreshToken(cb);

Copyright © 2026 Unity Technologies
法律信息隐私政策CookiesDocumentation Terms of Use请勿出售或分享我的个人信息您的隐私选择(Cookie 设置)

“Unity”、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属公司在美国和其他地方的商标或注册商标(此处查看更多信息)。其他名称或品牌是其各自所有者的商标。

为方便起见,一些页面是机器翻译的,可能包含不准确的内容。如有信息不一致的情况,以英文版本为准。

  • 在本页上
    • Prerequisites

    • Get started

    • Response structures

      • Refresh a token

      • Delete a player

      • OpenID sign-in, linking, and unlinking

        • Setup

        • Sign-in

        • Linking

        • Unlinking


报告此页面的问题