This page may contain legacy content

To get our most up-to-date content please access our documentation

May 21, 2022

Create an NFT On-Chain Inventory System for Gaming

Table of Contents

For some time, programmers struggled to implement certain features and elements into their blockchain games. Specifically, programming elements that sync with a network’s chain, such as an on-chain inventory for NFTs (non-fungible tokens). However, developers can now implement Web3 components easily with just a few lines of code, thanks to Moralis and its documentation. For example, developers can quickly and easily create an NFT on-chain inventory system for gaming and, in turn, take their GameFi and play-to-earn (P2E) project to the next level. If this sounds interesting, read on and pay close attention to the content in this tutorial as we explore how to create an NFT on-chain inventory system for Web3 gaming!

As we move forward, we will take on an example project where we will create a simple Unity Web3 game. Further, you’ll be able to use your Unity proficiency to create an NFT on-chain inventory system. Of course, Unity will take care of the frontend. However, for the backend, we will use Solidity, Remix, and Moralis to tie it all together. The first two tools will enable us to create and deploy a proper smart contract. Although, to index the blockchain, which will be the core of our NFT on-chain inventory system, we will use Moralis. This “Firebase for crypto” operating system is the pinnacle of the current Web3 tech stack and the ultimate Web3 backend platform. It is a single workflow tool to create decentralized applications (dapps) in minutes. Fortunately, you can access the power of Moralis’ SDK and Moralis’ Metaverse SDK with a free Moralis account.      

On-Chain Inventory System Example – Demo

We want you to see the final results before inviting you to roll up your sleeves and create an NFT on-chain inventory system. After all, a demo of our example Web3 game will enable you to determine if you want to build your own version of this excellent dapp. 

Since Web3 authentication is the gateway to the crypto realm, our game’s initial screen neatly covers that aspect: 

By scanning the above QR code using their Web3 wallets, players get to complete a Web3 login. Since MetaMask is the most popular crypto wallet, let’s assume that an example player decides to authenticate with MetaMask. As such, the player needs to confirm his login by clicking on the “Sign” button inside the wallet:

After successfully authenticating, the player can move his avatar in our 3D environment. Hence, the player can walk to the counter that enables him to buy NFTs in-game:

When the player is close enough to the seller, he can interact with the shop. As such, the player can view the items (in-game NFTs) that can be added to his NFT on-chain inventory:

Pre-Minted On-Chain Inventory – Buying In-Game NFTs 

Looking at the screenshot above, you can see that the current NFT shop’s inventory offers six items. Moreover, players can view the details of the available items by clicking on them. Let’s say the example player clicks on the “sword”:

As you can see in the screenshot above, the player can inspect the item’s name and description by clicking on it. Also, players can buy the items using the “buy” button that overlaps the items’ images. When a user decides to click on the buy button, it triggers a cascade of events. First, the game’s backend creates the item’s metadata. The latter is saved to IPFS. Then, the game informs the player to confirm the transaction. For that purpose, he must use his Web3 wallet and confirm the transaction:

After the item is purchased, it will no longer be available and displayed inside the NFT shop’s inventory:

Player’s On-Chain Inventory and Viewing NFTs on OpenSea

On the other hand, the player’s NFT on-chain inventory now displays the sword. If a player wants to view their acquired NFTs, the user needs to hit the “I” key:

The above screenshot indicates the player’s on-chain inventory – the “sword” image. However, to show you that this is not just an image but an actual NFT, our example game contains another feature. If players click on an item in their inventory, it will open that NFT’s details in OpenSea:

Create Your NFT On-Chain Inventory System with Unity and Moralis – Example Project

We’ve reached the point where you have a chance to get your hands dirty and create your instance of the above-presented Web3 game. If you remember, we will use Moralis and, in turn, avoid the limitations of RPC nodes. Furthermore, you should also know that Moralis is all about cross-chain and cross-platform interoperability. As a result, you can use the same code for various chains and use your preferred platforms. However, we will focus on Unity and Polygon’s testnet, Mumbai, as we move forward. Nonetheless, create your free Moralis account using the “free Moralis account” link in the introduction. To get additional support building example projects like this one, join Moralis Projects. By doing so, you also get a chance to earn some special Moralis NFTs.

Here’s an overview of the upcoming sections and steps that awaits you:

  1. Ensuring that you understand the structure of this example project.
  2. Helping you to get started with Moralis.
  3. Preparing your pre-minted NFT on-chain inventory.
  4. Deploy our smart contract. 
  5. Taking our metadata and images from off-chain data to on-chain inventory – minting NFTs. 
  6. Enabling the viewing of NFT on-chain inventory. 

Note: From a bird’s eye view, we will look at the above six steps in the following sections. We encourage you to use the video at the end of this article for more details. Also, the entire code, including Unity assets and prefabs, awaits you at our project’s GitHub repository. 

Our Unity Project Structure 

A Unity expert from Moralis will first present you with the structure of our project in the video below, starting at 4:25. He’ll show you how to navigate the folders and locate the assets and scripts. You can locate most of the latter in “_Project”:

If you are looking for the “Game” scene, you can find it in the “Scenes” folder. As the name indicates, the third-party assets are in the “ThirdParty” folder. Then, you will also go through the hierarchy of our on-chain inventory system for gaming (5:37):

As far as Web3 authentication goes, the “MoralisStandaloneAuth” prefab will get things done once you connect the Moralis SDK to Unity. To trigger proper events and handle game stages, you must use “GameManager”. To handle the in-game cameras properly, you use “cameraManager”. Fortunately, Unity Cinemachine makes things pretty straightforward and offers multiple advanced options. Going through the rest of the hierarchy, you’ll learn more about the “World”, “EventSystems”, “UI elements”, and “Player” assets. Among these, “World” will show you the details of the visual aspects of our game:

Looking at the above screenshot, you can see the NFT shop counter’s collider. The latter plays an important part in triggering the “NFT shop inventory” screen. As such, it is an essential part of our on-chain inventory system. The “ShopInventory” and “PlayerInventory” assets, located in “UI Elements”, enable players to interact with the game. Nonetheless, you’ll also have a chance to learn more about the “GameInputAction” asset (the new game input system).

Getting Started with Moralis

Getting started with Moralis is incredibly easy. For starters, you need to create your free Moralis account, which you’ve probably done already. Hence, you can simply log in to your Moralis admin area. Next, click on “+ Create a new Server”. Of course, you can use the video tutorial below, starting at 10:00, for more hands-on guidance.

Since this is an example project, make sure to select the “Testnet Server” option. Then, enter your server’s details. Moreover, choose Polygon’s testnet (Mumbai) (see the image below). You get to spin up the server with the “Add Instance” button:

As soon as your server is up and running, you can use the “View Details” button to access its details:

Then, you’ll need to use your server’s details and paste them into Unity:

This is the interface where you paste your server URL and application ID:

Preparing Your Pre-Minted NFT On-Chain Inventory

Properly managing shop items is the core of our NFT on-chain inventory system. Starting at 12:30, you’ll get a code walkthrough of the “ShopInventory” script. Hence, you’ll get familiar with querying data from the Moralis dashboard (database): 

The leading role inside the Moralis dashboard plays the “ItemData” class. It contains columns for items’ names, descriptions, and image URLs. These pieces of information make up our NFTs’ metadata. Nonetheless, starting at 15:10, you will see how easy it is to create your own “ItemData” class.

Deploy Our Smart Contract

At this point, you already have your own Moralis server running on the Mumbai testnet. However, unless you’ve used MetaMask to interact with this network before, you don’t have it in your MetaMask extension. Fortunately, you can do this with a click of a button via Moralis Speedy Nodes:

In addition, you need some “play” MATIC to deploy your instance of our smart contract. Fortunately, there’s the Mumbai testnet faucet, which will help you out with that. For more details, use the video below, starting at 30:21:

At this point, you should have your MetaMask extension connected to Mumbai. Also, you have some “play” MATIC coins in your wallet. As such, you can open up Remix. Then, simply copy the “ShopContract” code and paste it into Remix. If you want to learn more details about the smart contract’s code, use the video below starting at 31:13. In addition, we must point out that this contract focuses on the ERC-1155 token standard, which tends to be the best option for in-game NFTs. Although, in some instances, the ERC-721 token standard can be a better fit. Finally, to deploy your smart contract, use the “Injected Web3” environment:

From Off-Chain to On-Chain Inventory – Minting NFTs

Recalling the above demo, you probably remember that players mint our in-game NFTs by clicking the “buy” button. You may also recall that players need to confirm the transaction using their Web3 wallets. To set this functionality in place inside Unity, we use the “ShopItem” script. You can get a more detailed code walkthrough of that script starting at 36:45. In short, when players select any particular item from the NFT shop inventory, the “ActivateItemPanel” event handler is triggered. Then, when the players click on the “buy” button, the latter triggers the “PurchaseItem” function:

The above sequence is the key to mint game assets as NFTs. We strongly encourage you to use the video below and view the scripts in detail. As a result, you will see the power of Moralis’ SDK, which only uses short snippets of codes to do most of the heavy lifting. Furthermore, using the Moralis IPFS integration, you can effortlessly create metadata (JSON files) for your NFTs. The code accesses those JSON files when players mint NFTs via the above-deployed smart contract.

Viewing an NFT On-Chain Inventory

You almost fully understand how our example on-chain inventory system for gaming works now. All that’s left to cover is how players can access their inventory inside the game and on OpenSea (49:46). This is where “PlayerInventory” covers the logic behind the functionality. The “LoadPurchadedItems” method enables the game to get players’ NFTs. The same method ensures that our game properly displays obtained NFTs inside the players’ inventory. On the other hand, to display NFTs on OpenSea, the “CheckNftOnOpenSea” method does the trick. This is where you get to rely on Unity’s “openURL” method.

Create an NFT On-Chain Inventory System for Gaming – Summary

Today, you first had a chance to go through our NFT on-chain inventory system demo. You saw that we could use Unity to create our Web3 game. Also, you had a chance to clone our code and create your instance. Furthermore, using our overview and the above video tutorial, you had a chance to obtain a proper understanding of the code behind the scenes. In addition, you interacted with Moralis and Remix, which are prominent tools and can save you a lot of time. 

Nonetheless, if you enjoyed this example project, we invite you to visit the Moralis blog and the Moralis YouTube channel. These outlets offer a ton of free content that can help you become a Web3 developer. For instance, some of the latest topics include a guide to building a decentralized Twitter, an EIP 1559 example, fractional NFTs, building a decentralized autonomous organization (DAO), an SPL vs ERC20 tokens comparison, building a Solana token dashboard, how to create your own metaverse, how to create a BNB NFT, and much more.

However, if you are determined to become a blockchain developer sooner rather than later, you need to consider taking a more professional approach. This is where Moralis Academy might be the answer you’ve been searching for. This online academy offers much more than just top-tier blockchain development courses. Moralis Academy is the place to get a personalized study path, expert mentorship, and membership in one of the most advancing and welcoming communities in the Web3 industry.     

Unlock the full potential of your NFT projects with this industry-leading NFT API! Fast, easy, and free.
Related Articles
September 28, 2023

Full BSC Node Guide – How to Run BNB Smart Chain Nodes

November 17, 2022

Exploring the Optimism Network – A Next-Gen L2 Blockchain for Ethereum

November 25, 2022

Moralis Webhooks – An Introduction to Web3 Webhooks

December 9, 2022

How to Listen to the Blockchain with Ethers.js

November 23, 2022

Deploying Lambda Functions – How to Deploy AWS Lambda Function Tutorial

October 1, 2022

How to Clone Zapper in Less Than 1 Hour

December 8, 2022

How to Get NFT Collections Using Python and React

October 26, 2022

How to Create an NFT Project – Get Started and Launch Successfully