The benefit of building applications using APIs is that they are platform-independent. It means if your device is internet-enabled and can run API requests you can possibly build another great application.
The applications of NFTs in Gaming are definitely crazy. But one sec, what I’ll say is that you know application dev, but, you need to learn Blockchain concepts, solidity, unity, JS libraries like web3js, and another bunch of stuff if you want to start building an NFT driven game on Unity. Also, when you start building, what about the system design, your app, and blockchain integrations? And the time is taken with the costs to maintain nodes?
It’s a lot to digest. Here is where API services like Onecdot come into the picture. These are the simple APIs that offload the web3 work in their end, and let you mint NFTs and other stuff using simple APIs.
Check out the APIs Swagger docs here 👉 https://api.onec.in/docs/
In this article, we’ll be using Onec-NAAS, the API Suite product of Onecdot and Unity3d to build mild interaction between blockchain and unity using Onec-NAAS APIs. Basically teaching and demonstrating how to use Onec-NAAS APIs in Unity.
And we’ll be doing this our beloved chain @0xPolygon.
There are a few steps to follow along to make your Unity tutorial ready. In this tutorial, we’ll:
To start with 👇
Select Button again, and on the right side pan, rename the button to PostButton
and select "text" under the button to name it whatever you like. We call it "MintNFT"
OutputArea
. This will be the area where the response from the server will be listed. Scroll down the same pane to find “Line Type” convert it to “Multi-Line Newline”.These components can also be considered as gaming interactive components. Who can extend them later, as you learn Unity to make them into more advanced components?
Now since you’ve set up your basic Unity components time is to write a script that interacts with the Onec-NAAS server to serve your requests.
PostRequest.cs
PASTE_YOUR_APIKEY_FROM_DASHBOARD
with the API obtained from the Dashboard. And 0x0000000000000000000000000000000000
(which is a public address of the wallet) with the address you are minting NFT to.Add the following code to the script.
//PostRequest.cs
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System.Collections.Generic;
[System.Serializable]
public class Metadata
{
public string imageURL;
public string twitterHandle;
}
[System.Serializable]
public class MetadataList
{
public string public_address;
public Metadata metadata;
}
[System.Serializable]
public class Root
{
public List<MetadataList> metadata_list;
public string SaveToString()
{
return JsonUtility.ToJson(this);
}
}
public class PostRequest : MonoBehaviour
{
InputField outputArea;
void Start()
{
outputArea = GameObject.Find("OutputArea").GetComponent<InputField>();
GameObject.Find("PostButton").GetComponent<Button>().onClick.AddListener(PostData);
}
void PostData() => StartCoroutine(PostDataCoroutine());
IEnumerator PostDataCoroutine()
{
outputArea.text = "Loading...";
string url = "https://api.onec.in/api/v1/naas/mintNFT/";
var webRequestVar = new UnityWebRequest(url, "POST");
List<MetadataList> metadata_list = new List<MetadataList>();
metadata_list.Add(new MetadataList
{
public_address = "0x0000000000000000000000000000000000",
metadata = new Metadata
{
twitterHandle = "https://twitter.com/amnpandey",
imageURL = "https://pbs.twimg.com/profile_images/1433036427223199748/QiPV4LYC_400x400.jpg"
}
});
Root payload = new Root {
metadata_list = metadata_list
};
string json = JsonUtility.ToJson(payload);
byte[] rawJSON = new System.Text.UTF8Encoding().GetBytes(json);
webRequestVar.uploadHandler = (UploadHandler) new UploadHandlerRaw(rawJSON);
webRequestVar.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
//Set headers for the request
webRequestVar.SetRequestHeader("Content-Type", "application/json");
webRequestVar.SetRequestHeader("NAAS-APIKEY", "PASTE_YOUR_APIKEY_FROM_DASHBOARD");
//Makes request
yield return webRequestVar.SendWebRequest();
if (webRequestVar.isNetworkError || webRequestVar.isHttpError)
outputArea.text = webRequestVar.error + webRequestVar.downloadHandler.text;
else
outputArea.text = webRequestVar.downloadHandler.text;
}
}
Important
One thing very important and to note here is how we’re using the metadata and first converting it to JSON, and then raw data to send through the API POST request.
You have added the script and understand how the script works.
You need to do the following:
Canvas
from the left siddrag-dropPostRequest
script file on the right side pane where the “Add component” button is present.Now if you check it through the check mint status from the Swagger docs(You can use the same API key).
checkMintStatus
> Try it out and add the nft_ids
from the response to the txn_tracker to find your asset minted.Amazingly simple no?
Onecdot aims to provides more solutions like these to ease the development experience for NFT based applications. Other product is Onec-SDK, which currently have the functionality for wallet authentication in 3 lines.
Check out the products here:
Onec-NAAS 👉 https://api.onec.in/docs/
Onec-SDK 👉 https://www.npmjs.com/package/onec-sdk
Onecdot functions as a LAB where the Developers, Advocates, Product Managers, Marketing people etc. are invited to build and ship cool products and write a great thesis.
Follow on Twitter & Join Discord 👇