Introducing EconomyKit's C# library for Unity!

Add items, currencies, trading, and more in minutes!

Published Monday, November 28, 2022

We're happy to announce the first release of our C# library for integrating EconomyKit with Unity projects!

Installation #

The latest release of our C# library can be obtained at https://github.com/WorldQL/economykit-csharp/releases Download EconomyKit.UnityClient.dll and RestSharp.dll and drag them into your Unity project. Then, you can import them and start using them in your code.

Example #

Consider this simple example scene:

Screenshot of Unity UI example

It does the following:

  1. Accepts a PlayFab ID through an InputField.
  2. Looks up the player using the ID provided.
  3. Displays the user's display name (fetched from EconomyKit) in the Text object username.
  4. Adds 25 gold (a commodity defined on the server side) to the player

Here's a class to drive this example:

// Imports removed
public class ExampleEconomyKitManager : MonoBehaviour
{
public Button yourButton;
public TMP_InputField inputField;
public TMP_Text username;
public TMP_Text goldBalance;

private static CommodityApi _commodityApiInstance;
private static PlayerIDApi _playerIDApiInstance;

// ID of arbitrary gold commodity, must exist on your instance.
private static readonly Guid GOLD_COMMODITY = Guid.Parse("76849985-5ab1-4d32-81f1-07eac096f5c2");
void Start()
{
Configuration.Default.BasePath = "https://YOURCOMPANY.economykit.com/";
// Configure JWT HTTP bearer authorization: bearerAuth
Configuration.Default.AccessToken = "YOUR_JWT_HERE";

// Create our API instances
_commodityApiInstance = new CommodityApi(Configuration.Default);
_playerIDApiInstance = new PlayerIDApi(Configuration.Default);

Button btn = yourButton.GetComponent<Button>();
btn.onClick.AddListener(TaskOnClick);
}

void TaskOnClick()
{
Debug.Log("Clicked the button!");

try
{
// We don't have Player's EconomyKit GUID, only their PlayFab ID.
var playerPlayfabId = inputField.GetComponent<TMP_InputField>().text;
// Fetch Player's EconomyKit GUID from her PlayFab ID
var playerLookupPayload = new FetchPlayerRequest(null, IdentifierType.PF, playerPlayfabId);
var playerIdentityResult = _playerIDApiInstance.CreateOrFetchPlayer(playerLookupPayload);

username.GetComponent<TMP_Text>().text = playerIdentityResult.DisplayName;

// Add 25 Gold to Player's account.
var commodityStack = new CommodityStackCreateBody(playerIdentityResult.PlayerId,
GOLD_COMMODITY, 25, 0);
var result = _commodityApiInstance.GiveCommodity(commodityStack);

goldBalance.GetComponent<TMP_Text>().text = result.Quantity.ToString("F");
}
catch (ApiException e)
{
Debug.Log("Exception when calling: " + e.Message );
Debug.Log("Status Code: "+ e.ErrorCode);
Debug.Log(e.StackTrace);
}
}
}