PlayFab and Unity
PlayFab bills itself as “The most powerful backend platform for your game.” It offers a host of features if you are building a multiplayer game. Since I am building a multiplayer game I thought I would try it out.
After signing up for an account, I was directed to a three step process to get it up and running. Step 1, get the SDK. I was very happy to see that it was in gitHub!
Step 2, making your first API call. In order to do this, I had to first install the package, and then add some code. To install the package, all I had to do was click assets, import a package and then choose custom package. After selecting the package, I choose to install all of the items int he package.
Once that was done, I changed back to my starting scene, and created a new script for PlayFabManager.cs. There is a message in the getting started that you must add your TitleId You must set the TitleId in the SDK before making API calls.
“You must set the TitleId in the SDK before making API calls.”
They do not mention that before doing this, you should import PlayFab, so that you can add the single call to set the value. They say it only needs to be done once for your application.
PlayFabSettings.TitleId = "<YOUR TITLE ID HERE>";
With our unity package installed, and that one setting set, I started coding the PlayFabManager class.
using UnityEngine; using System.Collections; using PlayFab; using PlayFab.ClientModels; public class PlayFabManager : MonoBehaviour { public string PlayFabId; void Awake () { PlayFabSettings.TitleId = "<YOUR TITLE ID HERE>"; } // Use this for initialization void Start () { Login (PlayFabSettings.TitleId); } // Update is called once per frame void Update () { } void Login(string titleId) { LoginWithCustomIDRequest request = new LoginWithCustomIDRequest() { TitleId = titleId, CreateAccount = true, CustomId = SystemInfo.deviceUniqueIdentifier }; PlayFabClientAPI.LoginWithCustomID(request, (result) => { PlayFabId = result.PlayFabId; Debug.Log("Got PlayFabID: " + PlayFabId); if(result.NewlyCreated) { Debug.Log("(new account)"); } else { Debug.Log("(existing account)"); } }, (error) => { Debug.Log("Error logging in player with custom ID:"); Debug.Log(error.ErrorMessage); }); } }
Once I started the game, the usual title screen showed, just as it always had, but the debug messages told me that this was a new client, and that I had been registered!
That was about as simple as it possibly could have been. Logging in to the PlayFab website, I see a new anonymous client under the Players tab, along with a last login. Already valuable information!
Next up for playfab will be getting the users to log in, if they like, so that we can save their coins!