Quickstart: Set and get player data
In the PlayFab APIs, the function names utilize the term UserData. In the Game Manager, this concept is described as Player Data. They’re identical, and interchangeable.
Requirements
- A PlayFab developer account.
- An installed copy of the Unity Editor. To install Unity for personal use via Unity Hub, or Unity+ for professional use, see Download Unity.
The PlayFab Unity3D SDK supports Unity Editor version 5.3 (released December 2015) and higher.
- A Unity Project can be:
- A brand new project. For more information, see Starting Unity for the first time.
- A guided tutorial project. For more information, see Getting Started with Unity.
- An existing project.
- The PlayFab Unity3D SDK.
About the code examples
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 C# SDK, you must modify the code to use an async Task model. Methods that must be modified have Async appended to the method name in the signature. For example, SetObject in the Unity SDK becomes SetObjectAsync in the C# SDK. For more information, see Asynchronous programming with async and await. Although you can use an existing Unity project, this quickstart assumes you’re using the sample created 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.- Open Game Manager. If you’re unfamiliar with it, see the Game Manager quickstart.
- Select the Players tab.
- Select the name of the Player to access 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 using statement:
using System.Collections.Generic; -
To set player data, add a
SetUserDatamethod to thePlayFabLoginclass.SetUserDatauses 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 KVPsAncestorwith the valueArthur, andSuccessorwith the valueFred. -
To get player data, add a
GetUserDatamethod to thePlayFabLoginclass.SetUserDatauses the GetUserData method to retrieve the player data for the specified player. -
Call
SetUserDataandGetUserDatain theOnLoginSuccessmethod. - Save the file.
