Mint NFT From Your Game | Unity

Furkan Akgün
Coinmonks
5 min readJan 6, 2023

--

Hello Strangers,

Today I’m describing how to interact with Ethereum in your unity game. The steps will be very clear and straightforward.

Before I start, I would like to talk about which tools and languages I will use and briefly explain what these tools do.

ChainSafe: ChainSafe is a global leader in blockchain protocol and infrastructure development for Web 3.0. Chainsafe has a very powerful unity SDK. You can interact with the blockchain and send Ethereum transactions using these SDK methods.

Remix IDE: Remix is a powerful open-source IDE that we can write our smart contract with solidity and deploy to the blockchain. Here is the Online IDE https://remix.ethereum.org/

OpenZeppelin: OpenZeppelin is an open-source framework to build smart contracts. It is very useful for beginners.

Steps

  1. Install and import ChainSafe SDK to your Unity Project.
  2. Write your smart contract with OpenZeppelin and REMIX IDE.
  3. Interact with your smart contract in Unity.

Let’s Get Started

First of all, we have to install and import ChainSafe SDK. After that build and run your project with the WebGL platform. Here is the link for the installation steps for your Unity project https://docs.gaming.chainsafe.io/installation

After we set up our unity project. The structure will look like the above. We will use Goerli testnet so go to network.js file under Assets>WebGL Templates > Web3GL-2020x set chainId as 5.
We will use the SendContractExampleCanvas prefab select it and add it to the scene.

Scene

This is our scene. It’s time to look at scripts. Inside the Prefabs there is an empty object which holds the WebGLSendContractExample scripts. You can find it Web3Unity>Scripts>Prefabs>WebGL>WebGLSendContractExample.cs

WebGLSendContractExample.cs

Now our interaction part is almost ready we will make some configurations after we write our mint contract.

Smart Contract

Secondly, let's write our smart contract. Don’t worry! It will be as simple a mint contract as possible. Even if you don’t know anything about smart contracts and solidity you can still follow.

Now go to the link https://docs.openzeppelin.com/contracts/4.x/wizard this is an OpenZeppelin framework wizard that helps you to create your mint contract.

You need to make some configurations inside the wizard. Firstly navigate to the ERC721 section by using the navbar. ERC721 is an Ethereum standard for representing non-fungible tokens. Set the Name and Symbol of your token and select Mintable as a feature. Access Control is Ownable then click to Open in Remix.

Remix IDE

This is what our smart contract should look like. We have one simple method called safeMint takes two parameters address and tokenId.

Now compile it and deploy it to the blockchain. For the deployment select the Injected-Provider-MetaMask as ENVIRONMENT and click Deploy.

When deploy complete at the deployed contracts section copy the contract address and paste it to the https://goerli.etherscan.io/

Congrats! You deployed your mint smart contract.

Now It’s time to interact with Unity!

Interact With Unity

Let’s back to our code sample. There are a couple of parameters I will explain one by one.

The first parameter is the method name that we want to trigger in our smart contract we set it to safeMint

The second parameter is contract abi. Simply abi is the interface between two software modules, most commonly between user programs and operating systems. abi’s are generated whenever your contract has complied.

After you complied your contract in REMIX copy the abi at the left bottom side of IDE then paste it to the empty page inside VS Code.

After you paste Ctr+F to find and replace “ to \”. This is compulsory for abi format. After you replace all, select all the codes and set Join Lines using Command Palette. Copy the line and paste it to the abi parameter.

Set the other parameters as shown.

contract parameter is the contract address that we deployed recently. You can set tokenID for 1. You can change it to whatever your project fits.

Final look

We finished almost finished. Go to your Build settings and add a WebLogin scene as the first scene you can find it under Assets>Web3Unity>Scenes>

Build and run your unity project.

After you log in with your MetaMask click the SendContract button.

CONGRATS!

After you confirm you get your NFT. You can check it on your https://testnets.opensea.io/ profile.

New to trading? Try crypto trading bots or copy trading on best crypto exchanges

--

--

Furkan Akgün
Coinmonks

Hello stranger! I'm a creative software developer.