Unity SDK

Our Unity SDK is available on github.

To include the SDK into your Unity project, copy the following git URL:

https://github.com/chainengine-xyz/chainengine-sdk.git

Open the Package Manager window, select the option "Add package from git URL," and insert the copied URL.

1345

Minimal Configuration

To start using the SDK, drag the ChainEngineSDK prefab from the Project Files Explorer into your scene.

669

Fill in the Game Id property with the id of your game, as described in the "Make Your First Request" section.

566

Instantiating the SDK

To use the SDK at the start of the scene in a c# script file, use the following snippet:

private ChainEngineSDK client;

private void Start()
{
    client = ChainEngineSDK.Instance();
}

๐Ÿ“˜

What can we do with the Unity SDK?

The Unity SDK is a "client-based" SDK, implying that most functionalities are performed from the player's perspective. Features contain wallet authentication and NFTs fetching (single or multiples).

To interact with ChainEngine servers, the SDK requires the game developer to associate a player by creating a new record or fetching an existing one.

Below, we show how to perform this action with just a few lines.

Authentication

Our SDK provides an authentication method for the player using his wallet. Right now we support MetaMask, Coinbase, Trust Wallet and WalletConnect (which may provide authentication with even more wallets through the WalletConnect's protocol).

To authenticate the player with his wallet is simple as calling our WalletLogin method.

public void WalletLogin()
{
    client.WalletLogin();
}

This will open a browser tab where the player can authenticate, as shown in the image below.

1392

Once the authentication page is open, the player can choose a wallet provider to authenticate with, as shown in the image below.

1280

After successful authentication, the player will be warned, as shown in the image below.

1392

Authenticate with different wallet providers

๐Ÿšง

WalletProvider

By default, our SDK will use the browser to authenticate the player. However, this workflow is perfect for Windows, Mac, Linux, and WebGL games, where the player can sign the authentication using his browser's wallet or WalletConnect. On Mobile games, you can allow the player to authenticate using his mobile native wallet of choice by calling this same method but passing a wallet provider as a parameter, as shown below.

public void TrustWalletLogin()
{
    client.WalletLogin(WalletProvider.TrustWallet);
}

public void MetamaskLogin()
{
    client.WalletLogin(WalletProvider.Metamask);
}

public void CoinbaseLogin()
{
    client.WalletLogin(WalletProvider.Coinbase);
}

Action Subscription

๐Ÿšง

Important

As this authentication workflow is asynchronous and we don't know how long the player will take to authenticate, our SDK provides two different actions OnWalletAuthSuccess and OnWalletAuthFailure. These actions will return the authenticated player data or the authentication error message, respectively.

private void OnEnable() {
    ChainEngineActions.OnWalletAuthSuccess += OnWalletAuthSuccess;
    ChainEngineActions.OnWalletAuthFailure += OnWalletAuthFailure;
}

private void OnDisable() {
    ChainEngineActions.OnWalletAuthSuccess -= OnWalletAuthSuccess;
    ChainEngineActions.OnWalletAuthFailure -= OnWalletAuthFailure;
}

public void WalletLogin()
{
    client.WalletLogin();
}

private void OnWalletAuthSuccess(Player player)
{
    Debug.Log("Player: " +
              $"gameId {player.GameId}\n" +
              $"walletAddress {player.WalletAddress}");
}

private void OnWalletAuthFailure(WalletAuthenticationError error)
{
    Debug.Log(error);
}

As soon as one of these actions is fired, your game will receive the corresponding value, as shown in the image below.

1348

Alternative Way to Create a Player

const string walletAddress = "0x92c762Bb5f8b2468965110AB2969CBc2b0D3806D";

Player player = await client.CreateOrFetchPlayer(walletAddress);

This method will return an instance of a Player, and, for convenience, the SDK will store the Player instance internally to perform requests to ChainEngine's services.

Click to see the Players' attributes
public class Player
{ 
  public string WalletAddress;
  public string GameId;
}

Even though the SDK stores the user instance internally to authenticate interactions with our services, none of this data is exposed outside of the SDK. If applicable, the game developer should store the player's wallet address inside PlayerPrefs to be easily retrievable.

Get NFTs owned by the player

Retrieving NFTs is as simple as a single-line command.

var nfts = await client.GetPlayerNFTs()

This method will return an instance of **PlayerNftCollection", a unique data structure created by the SDK to store NFT data.

Exposing NFTs Metadata from PlayerNftCollection

To access the NFTs Metadata, we simply use the method Items inside the PlayerNftCollection.

Completing the previous code:

var nfts = await client.GetPlayerNFTs()

foreach (var nft in nfts.Items())
{
   Debug.Log($"NFT: {nft.Metadata.Name}");
}

This snippet will then expose the Name attribute inside the metadata of the NFT for all NFTs returned by the SDK.

Click to see all attributes for the NFT object
    public class Nft
    {
        public string Id;
        public string AccountTd;
        public string GameId;
        public string OnChainId;
        public string Chain;
        public string Status;
        public NftMetadata Metadata;
        public string OwnerPlayerId;
    }

๐Ÿ“˜

More about the PlayerNftCollection

The PlayerNftCollection data structure contains internal logic to facilitate the fetching of NFTs and pagination, reducing overhead on the developer's side.

Beyond the Items() method, the developer can also use the methods HasNext and NextPage to navigate multiple pages of NFTs. The first verify the existence of other pages while the latter fetches more data, appending it to the Items() method and returning the incoming List.

Eventually, game developers could need to fetch a single NFT. We've got you covered there too. To perform such an action, see the next section.

Get a Single NFT

Similarly, the SDK also has a convenient method for fetching multiple NFTs:

var nft = await client.GetNFT(id)

Calling a specific NFT will return a unique instance of NFT.

๐Ÿšง

NFT ID

Assure that the ID provided to the method represents the internal ID provided by ChainEngine during minting.

Exposing NFT Image, Amounts, and Custom Properties from the NFT Metadata

You can show NFT image, amounts, and custom properties from the NFT metadata.

NFT Image

To show the NFT image, simply call the nft.Metadata.Image in your code.

For example, see the following code:

StartCoroutine(helper.GetImageUriNftCoroutine(nft.Metadata.Image, nftDetailsImage));

NFT Amounts

To show the NFT amounts, see the following code:

nftAmount.text = nft.Holders[client.Player.Id].ToString();

NFT Custom Properties

To show the NFT custom properties, see the following code:

if (nft.Metadata.Attributes != null)
        {
            int i = 0;
            foreach (var attr in nft.Metadata.Attributes)
            {
                if(i < CUSTOM_PROPS_GRID_ITEMS_COUNT)
                {
                    customPropsKeys[i].text = attr.Key;
                    customPropsValues[i].text = attr.Value.ToString();
                    customPropsPanels[i].gameObject.SetActive(true);
                    i++;
                }
            }
        }

API Mode

Our SDK allows you to operate on both Mainnet and Testnet. Although Testnet is set as default in our SDK, you should make sure to change the API mode to Mainnet for production builds.

๐Ÿ“˜

Mainnet vs Testnet

Mainnet (main network) and Testnet (test network) are terms used in the blockchain ecosystem to describe blockchain networks with critical functionalities.

The mainnet is responsible for executing actual transactions within the network and storing them on the blockchain for public use.

In contrast, the testnet provides an alternative environment that mimics the mainnetโ€™s functionality, allowing developers to build and test projects without needing to facilitate live transactions or use cryptocurrencies.

Switch the SDK API Mode is simple as call these methods:

client.SetTestNetMode();

or

client.SetMainNetMode();