Connecting to the Living Assets API from Unity
Learn how to query user assets, and all assets for sale, from within Unity
Last updated
Learn how to query user assets, and all assets for sale, from within Unity
Last updated
freeverse.io
If your game is server authorative, it is likely that that vast majority of the interaction with the Living Assets API will be made from the server. However, in certain situations you may wish the game to connect directly to the Living Assets API. In this tutorial we will do exactly that.
Basic familiarity with Unity and GraphQL.
Having followed the first tutorial on creating web3 wallets in Unity. Make sure you have the Newtonsoft JSON package installed, along with the Nethereum plugin, the AESEncryption script, and the KeyStore GameObject and script implemented, as described in the tutorial.
A sandbox universe, its ID, API endpoint, and dashboard access.
Fire up the Unity project from the the web3 wallets tutorial in Unity, run the project, and make a note of the address of the user:
Now let's create a new asset and assign it to the user:
Login to the Freeverse dashboard using the credentials provided by Freeverse
Click on the green '+' icon in the lower left corner and choose Create Asset
Upload an image, and set a name and description for the asset (all three fields are required). Please consider typical formats like the mentioned ones here
Click on the Assign to Web3 Address
checkbox on the right of the screen, and paste the user address (3rd line of the output from Unity).
Click the Create Assets(s)
button to create the asset and assign it immediately to the user.
You may do this multiple times to assign more than one asset to the user, if you wish.
Alternatively, you could modify the code from previous tutorials to have the Unity application send an action code along with the user address to your game server, and have the server create the asset, without you having to use the dashboard.
A GraphQL request is nothing more than a specially formed request over HTTP. To make the request and receive the response in the correct format, we must create some special classes in Unity.
We are going to create a very simple helper class that takes a GraphQL query, and converts them into a UnityWebRequest object which we can launch at a later date. Create a new script called GraphQLClient, delete the boilerplate code, and type the following:
This is pretty much the simplest possible way that we can format a GraphQL query in Unity, using a serializable nested class to store the query (though notably, without any variables.
This tutorial uses a very simple GraphQL parser, for clarity. In a production environment, you might choose to use a more robust solution, of which there are several, for example:
https://assetstore.unity.com/packages/tools/network/graphql-for-unity-199114
We know that this user has at least one asset that belongs to them, so let's query the API to find out information about that asset. We are going to have to follow several steps to do this:
Create a new script to launch queries and receive the responses
Create a series of small C# classes to receive the JSON and deserialize it
Launch the query and parse the response into the classes.
In Unity, create a new script called Queries.cs and associate with the same GameObject in the scene as the KeyStore script. Remove the Update() function, and type the following, making sure to include the dependencies on UnityEngine.Networking and Newtonsoft.Json.
The main interest in this code is the GetUserAssets() function, which constructs a simple GraphQL query to obtain all the assets belonging to this user. For future flexibility, we are passing a callback function to GetUserAssets(), which receives either the response from the server, or prints any error.
Note how the query is launched by calling the LaunchQuery() function. This function can be called in any way you like. For testing purposes, it is convenient to add a simple UI button to the scene and link the button's OnClick event listener to the LaunchQuery function:
Upon running the application now, firing the LaunchQuery function will print the response from the server to the console:
The server response is in the format of a string, which we need to parse into C# variables. While we could do this ourselves, a more convenient way is to deserialize the data into a series of dedicated classes.
To save time writing these classes by hand, we can use the excellent https://json2csharp.com/ application, which takes any given JSON and automatically creates the required classes for deserialization with Newtonsoft's JSON package.
Paste these classes into Queries.cs, just below the dependencies.
Finally we can modify the LaunchQuery function to convert the result to JSON, deserialize it, and then, for informative purposes, print it to console.
In a real-world example, the data from this query could be used to, for example, show the user which assets they own, along with the properties of those assets.
A simple way to obtain the properties of the assets returned by the first query made in the tutorial would be to launch a subsequent query targeting allAssets along with filters
The props of each node will give us the name, description, image URL, and properties of each asset. With this, you can display the assets within the game as you wish.