I’m trying to implement PlayFab to my game, but I have a hard time understanding their API. Their documentation isn’t very Unity friendly…
Anyone in here using it?
Anyways… I’m want to display the logged in users Display Name but I don’t know how to get the dislay name.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using PlayFab;
using PlayFab.ClientModels;
public class MainMenuScript : MonoBehaviour {
public UILabel welcomeLabel;
string playerDisplayName;
void Start()
{
// Get the Display name of the user and print a weclome message
welcomeLabel.text = "Welcome, " + playerDisplayName;
}
}
I have created a login scene, which works just fine… When the player is logged in, a new scene loads which will be the main menu where the player can (or WILL be able to) change his password, email and display name… But I have no idea how to get that information using c#…
Please help? Free bananas for you all…
Hey Kovenant,
First of all, thanks for checking out PlayFab!
To access a player’s title display name:
-
You will want to call GetAccountInfo
-
This API call will give you an object,
-
You will find the DisplayName within UserTitleInfo
-
[Note] the links above take you to our primary API documentation. As you look at more and more PlayFab calls you will find they share similar structures and parameters.
All of our C# APIs follow a common callback pattern, where one makes a request and the response is triggered asynchronously when the server returns the information. So for GetAccountInfo this would look like:
/// <summary>
/// Gets the account info for the logged in player
/// </summary>
public void GetAccountInfo()
{
// build the request, in this case, there are no parameters to set
GetAccountInfoRequest request = new GetAccountInfoRequest();
// Send the request, and provide 2 callbacks for when the request succeeds or fails
PlayFabClientAPI.GetAccountInfo(request, OnGetAccountInfoSuccess, OnPlayFabCallbackError);
}
/// <summary>
/// Callback for GetAccountInfo Success
/// </summary>
/// <param name="Result"> Result - from the API Call</param>
void OnGetAccountInfoSuccess( GetAccountInfoResult result)
{
//voila
Debug.Log(result.AccountInfo.TitleInfo.DisplayName);
}
/// <summary>
/// Generic callback for errors
/// </summary>
/// <param name= Error Details </param>
void OnPlayFabCallbackError(PlayFabError error)
{
Debug.LogWarning(error.errorMessage);
}
In addition to the above, I would like to point you to our getting started guide. After that, additional support can be found at our knowledge base and forums.
Lastly, I would love to get your thoughts on how we can improve our documentation and on-boarding experience.
Feel free to drop me or our team a line with any questions, comments or suggestions:
All the best,
Zac
1 Like
I tried to use PlayFab Unity SDK for C#. And the first problem I saw is that there is no way to pass other object to callback functions… The style of SDK is using callbacks for requests to http services. And it’s a common way not to block main thread of an application for example if you want to perform a blocking operation such as DB query. But the problem now is that there is no way to continue working withing callback functions we can’t sent custom parameters and objects to them.
Example:
- User authentication mechanism
- On server we make a request to playfab DB from user_connect_validation function()
- Request result is returned to a callback function
- We need to save session or user object in our application and we need to do this in a playfab callback.
- We can’t provide any objects from user_connect_validation() function to our callback
- And due to an async way of playfab request we can’t proceed working in user_connect_validation function(), in large scale applications or because of network delays we can’t guaranty that DB response is received before checking it for being valid.
Hey iShadowfax,
You bring up a very good point. We have pass-through-to-callback parameters on our backlog of items to add. In the mean time I would like to propose a work-around taking advantage of C# closures.
void OnGUI()
{
if(GUILayout.Button("Test Closure"))
{
StartCoroutine(GetAccountInfo());
}
}
/// <summary>
/// Using coroutines and an anonymous function closure and for pass-through parameters
/// </summary>
public static IEnumerator GetAccountInfo()
{
// here is the variable we want to close on
string playerDisplayName = string.Empty;
// start a new request, using a simple API call
GetAccountInfoRequest request = new GetAccountInfoRequest();
//request.PlayFabId = "9078655678908089078"; //if you want to test a fail condition pass in an erroneous id
PlayFabClientAPI.GetAccountInfo(request,
// Success callback, this can also be achieved with named delegates and actions
(result) => {
playerDisplayName = result.AccountInfo.TitleInfo.DisplayName;
},
// Error callback, this can also be achieved with named delegates and actions
(error) => {
Debug.Log(string.Format("Caught PlayFab Error({0}): {1}", error.Error, error.ErrorMessage));
// since I am yielding on playerDisplayName, better set this to unblock the coroutine
playerDisplayName = error.Error.ToString();
});
// yield while we await the callback, this is just one of many ways to do this
while(string.IsNullOrEmpty(playerDisplayName))
{
yield return 0;
}
// output when we unblock.
Debug.Log(playerDisplayName);
yield return null;
}
This may or may not work for all your scenarios; however, I feel like it is an underused tactic for getting around API signatures. In addition to using closures, I would also like to point out that using static properties and singletons can give you an easily accessible location to access from within any callback.
Again, these are just guidelines; contact us at devrel@playfab.com and we will be happy to look over your scenario and help you determine if we are the right solution for your project.
I hope this helps provides some alternatives. Some additional guides on C# closures can be found here:
Zac
zac@playfab.com
Thank you, this helps. There is another issue I have, I use uLink to make both dedicated server and clients. To use network views, prefabs must be included both in client and server assembly. But we can’t use both PlayFabCliend & PlayFabServer SDKs in the same unity project nowadays… Is a solution for this now?
Unfortunately, at this time those libraries conflict with each other. I would recommend writing a simple editor script (or bash / powershell / external to Unity) that can swap assets between server and client. You can keep the build assets in a misc folder outside the /Assets folder/. When you need to switch between builds, run the script which moves out all of the folders that have conflicting code and moves in the intended assets.
Depending on how you have your project organized, this could be more or less messy. I will ask around the office to see if we have any other suggestions.
Zac
iShadowfax,
Apologies for the delay; I have some good news regarding running both Client and Server SDKs in the same Unity project.
These steps should enable your project to run both SDKs side by side.
-
Import the ClientSDK,
-
Remove the /PlayFabClientSDK/Playfab/PlayFabSDK/Public/PlayFabSettings.cs
-
Import only the core files from the ServerSDK
-
This is the /PlayFabClientSDK/Playfab/PlayFabSDK/Public/ folder
I would like to point out that the server version of PlayFabSettings has a property for the DeveloperSecretKey.
-
For server builds:
-
Make sure to set DeveloperSecretKey to your key (found in your game manager settings)
-
For client builds:
-
You should never ship any client builds with DeveloperSecretKey populated. Set back to null; otherwise, your server security could be compromised.
Happy Developing!
Zac