In the PlayFab APIs, the function names utilize the term UserData. In the Game Manager, this concept is described as Player Data. They are identical, and interchangeable.
Get started using PlayFab Player Data. This quickstart shows you how to set and retrieve player data using C# in Unity and using PlayFab CloudScript.
Player data is information that applies to an individual player or player group (shared data) and is stored as Key/Value Pairs (KVPs) by PlayFab. This article covers client API calls, which are safe to call from any process or context. It also covers server API calls, which should only be made from a dedicated server process you control, or a carefully secured CloudScript call. Server APIs require your dev secret key, which you should never provide-to or publish-with your client.
The C# Samples in this article are written for the Unity SDK. The Unity SDK uses an event driven model to handle non-synchronous tasks. To run the sample code using the standard C# or Xamarin C# SDKs, you must modify the code to use an async Task model. Methods that must be modified have Async append to the method name in the signature. For example, SetObject in the Unity SDK becomes SetObjectAsync in the standard C# SDK. For more information, see Asynchronous programming with async and await.
The C# Samples in this article are written for the Unity SDK. The Unity SDK uses an event driven model to handle nonsynchronous tasks. To run the sample code using the standard C# or Xamarin C# SDKs, you must modify the code to use an async Task model. Methods that must be modified have Async append to the method name in the signature. For example, SetObject in the Unity SDK becomes SetObjectAsync in the standard C# SDK. For more information, see Asynchronous programming with async and await.
While you can use an existing using Unity Project, the procedures in this quickstart assume that you're using the sample that you create when you complete the Quickstart: PlayFab Client library for C# in Unity.
Player data in Game Manager
You can always get and set player data through Game Manager by performing the following steps.
Select the name of the Player. This will put you into the Players Account tab.
Select the Player Data tab to see their data.
Add a method to set the player data and retrieve the player data
In the Unity Editor, open your sample project.
In the Project window, open Assets > Scripts and then open the PlayFabLogin script.
In Visual Studio, add the following to the following using statement:
using System.Collections.Generic;
To set player data, add a SetUserData method to the PlayFabLogin class. SetUserData uses the UpdateUserData method to create or update the player data for the logged in player.
The method creates (or updates, if the Key Value Pairs (KVPS) already exist) the KVPs Ancestor with the value Arthur, and Successor with the value Fred.
C#
voidSetUserData() {
PlayFabClientAPI.UpdateUserData(new UpdateUserDataRequest() {
Data = new Dictionary<string, string>() {
{"Ancestor", "Arthur"},
{"Successor", "Fred"}
}
},
result => Debug.Log("Successfully updated user data"),
error => {
Debug.Log("Got error setting user data Ancestor to Arthur");
Debug.Log(error.GenerateErrorReport());
});
}
To get player data, add a GetUserData method to the PlayFabLogin class. SetUserData uses the GetUserData method to retrieve the player data for the specified player.
To call the add calls to the SetUserData and GetUserData in the OnLoginSuccess method:
C#
privatevoidOnLoginSuccess(LoginResult result) {
Debug.Log("Congratulations, you made your first successful API call!");
SetUserData();
GetUserData(result.PlayFabId);
}
Save the file.
Run the sample
In the Unity Editor, select the play button. On success, the results of the of calls display in the console window.
Stream simulated operational telemetry data from physical assets to Azure Digital Twins and visualize data in Unity and mixed reality. Learn how to use an ARM template, create a user interface in Unity, and identify performance events before deploying to a HoloLens 2.