Player Accounts Building Block
Add a sign-in workflow and scripts to control player data to your scene.
読み終わるまでの所要時間 6 分最終更新 1ヶ月前
Use the Player Accounts Unity Building Block to add sign-in functionality to your project, for example, to track and preserve users' progress in your application. To learn more about PLayer Accounts, refer to the Player Accounts documentation.
This Building Block includes a test scene that demonstrates the player authentication process. You can use the scripts in this Building Block to authenticate players in your own project. For more information, refer to Add the Player Accounts Building Block to a different scene.
The Player Accounts Building Block uses the following Unity Gaming Services (UGS):
Unity Gaming ServiceTopic | Description |
|---|---|
| Authentication | Assigns each player a unique identity. To use these authentication flows, (set up an identity provider)[set-up-an-identity-provider]. To learn more, refer to Authentication. |
| Cloud Save | Stores Player Data and Game Data for your games, and provides access classes to control read and write access to Player Data and protected Game Data. To learn more, refer to Cloud Save. |
| Cloud Code | Provides server authority that you can use to write to protected or private access classes. You can use Cloud Code to write to game data or protected player data, or to read private game data. To learn more, refer to Cloud Code Authentication. |
Prerequisites
To set up a project for the Player Accounts Building Block, follow the instructions in:
- Follow the instructions in the LiveOps Building Blocks prerequisites.
- Install the Unity Building Block - Player Account from the Unity Asset Store.
- Follow the instructions in Get started with Unity Player Accounts.
Initial setup
To use all features of the Player Accounts test scene, follow the steps in this section.
Set up an identity provider
The Player Accounts Building Block test scene demonstrates multiple sign-in methods. For your project to recognize these authentication methods, do the following:
Deploy the Cloud Code module
The test scene uses the Cloud Code module to update the global score which is the result of the values of all authenticated players added together. To use this Cloud Code module, deploy it:
BlocksGameModule.ccmr- Follow the instructions in LiveOps Building Blocks prerequisites to set up deployment in your project.
- Go to Services > Deployment.
- In the Deployment window, in the Blocks.CloudCode section, select the BlocksGameModule.ccmr checkbox.
- Select Deploy Selected.
Use the Player Accounts Building Block test scene
The Player Accounts Building Block test scene demonstrates the following steps in a player account workflow:
- Sign-in: Uses the Unity Extensible Markup Language (UXML) to display a sign-in interface that authenticates players. Authenticated players can update or view the global score. For more information, refer to The Player Accounts test scene interface.
- Data visualization: Displays the player data that the labels in the asset declare.
PlayerAccountScene.uxml - Cloud integration: Displays the shared global score that the Cloud Code module manages.
BlocksGameModule.ccmr
To open the Player Accounts Building Block test scene:
- In the Project window, go to Assets> Blocks > PlayerAccount > TestScenes.
- Right-click the asset and select Open.
PlayerAccountScene
To use the Player Accounts Building Block test scene:
-
Enter Play Mode.
-
In Play Mode, select one of the following options to sign in:
- Sign in Anonymously: Creates anonymous player data to authenticate the user. To learn more, refer to Anonymous authentication.
- Sign in with Unity: Uses Unity account details to authenticate the player. To learn more about this authentication method, refer to External authentication. To use this sign-in method, set up a Unity Player Accounts ID provider.
- Sign in with Password: Accepts a custom username and password that meets the requirements. To use this sign-in method, set up the Username & Password provider.
-
In the new scene that loads, select the Increment Global Score button to increase the global score by a value of one. The global score is the result of the values of all authenticated players added together.
Add the Player Accounts Building Block to a different scene
To add player authentication from the Player Accounts building block to a new scene, do the following:
- Deploy the Cloud Code module.
- Go to Assets > Blocks > Achievements > Prefabs.
- Select the to open it.
AchievementsPrefab
Any changes you make to affect the when you enter Play mode.
Achievements.achAchievementsPrefabYou can also add the Player Accounts user interface assets to a different scene. To learn more, refer to Customize the Player Accounts user interface.
Customize the Player Accounts starter scene
The player accounts starter scene contains the following scripts that you can modify to change how player authentication works in the scene:
- The Player Accounts user interface scripts.
- The Player Accounts player data scripts.
Customize the Player Accounts user interface
The Player Accounts Building Block contains Unity Extensible Markup Language (UXML) assets to display the test scene user interface (UI). To learn about UXML scripts, refer to Introduction to UXML.
To customize the Player Accounts user interface:
- Go to Window > UI Toolkit > UI builder.
- In the toolbar, select the Blocks Runtime Theme.Viewport
?
- In the Library panel go to Blocks > PlayerAccount.
- Select PlayerDataLabel and drag it to the Hierarchy panel.
- Modify the exposed properties in the Attributes panel.
To learn more about UI builder, refer to UI Builder.
Player Accounts user interface assets
The Player Accounts Building Block contains the following Unity Extensible Markup Language (UXML) assets that you can modify or add to a different scene.
Asset name | Description |
|---|---|
| Displays authentication options that you can use to sign in anonymously, with Unity's Player Account web portal, or with a username and password. To learn more, refer to Platform-agnostic authentication. |
| A reusable label that displays and tracks the player ID with clipboard integration. |
| A reusable, data-bound label that tracks the player name. You can edit the name in the UXML field in the test scene. |
| A reusable label that loads Public, Default, or Protected player data according to the assigned data-key attribute. |
| Specialized |
| Debug control that displays all available default access class keys, and loads the value of one on demand for the player. |
The Player Accounts UXML scripts use the following classes:
- from the Authentication package.
AuthenticationService - from the Services Core package.
ServiceObserver&T
Player Accounts player data scripts
The Player Accounts Building Block contains scripts that manage player data, such as sign-in information, and game data, for example, scores. These scripts use the base class, which you can use from the Services Core package, to manage the lifetime of services without the need to track them manually.
ServiceObserver<T>Find the following script assets in the Project window: Assets > Blocks > PlayerAccount > > Runtime.
Scripts
?
Asset name | Description |
|---|---|
| Handles sign-in events and authentication state, and allows the project to register callbacks. |
| Displays the information that |
| A Cloud Code module file that uses a write lock mechanism to manage game-level shared data samples of a global score. |
Troubleshooting
If the Increment Global Score button in the Player Accounts test scene doesn't update the Global Score value, follow the instructions to deploy the Cloud Code module.
To learn more about read and write restrictions for Cloud Save Data, refer to the Services API documentation.