Skip to main content

Display player inventories in Unity

In this tutorial step, you will learn how to display a player's NFT inventory using the Unity zkEVM API package.

This allows players to view their NFT collections within the game.
Display player inventories in UnityDisplay player inventories in Unity
💡Prerequisite step
Before continuing with this step, ensure you have followed all the instructions in the prerequisites step of this tutorial series.

Overview

Displaying a player's inventory is a key feature of any game that incorporates NFTs. Generally you want to show the player a list of NFTs they own, and give them the ability to view more detailed information about each NFT they are interested in. Below we'll show you how to fetch and display the player's NFT inventory and access the attributes of each NFT to show in the detailed view.

Displaying Player NFT Inventories

To display a player's NFT inventory, you can use the Search NFTs endpoint.

Here's an example of how to implement this:

using System.Collections.Generic;
using Immutable.Api.ZkEvm.Api;
using Immutable.Api.ZkEvm.Client;

private readonly MetadataSearchApi m_MetadataSearchApi =
new(new Configuration { BasePath = "https://api.sandbox.immutable.com" }); // Or "https://api.immutable.com"

var result = await m_MetadataSearchApi.SearchNFTsAsync(
"imtbl-zkevm-testnet", // Or "imtbl-zkevm-mainnet"
contractAddress: new List<string> { "NFT_CONTRACT_ADDRESS" }, // Replace with the NFT contract address
accountAddress: "GAMER_WALLET_ADDRESS", // Replace with the gamer's wallet address
onlyIncludeOwnerListings: true // Set to true to only include listings by the owner
);

You can see an example of how this is implemented in the Unity Sample Game Inventory Screen, which also uses the Search NFTs endpoint.

Asset Inventory

Displaying NFT Details

In addition to displaying the player's inventory, you can create an Asset Details screen that showcases detailed information about each NFT, including the image, name, collection address, contract type, and other relevant attributes.

To show the details of a selected NFT, use the result obtained from the Search NFTs endpoint in the previous section.

Example of accessing the details from the result:

using Immutable.Api.ZkEvm.Model;

// `result` is the SearchNFTsResult from SearchNFTsAsync()
foreach (var nftBundle in result.Result)
{
var nft = nftBundle.NFTWithStack; // Access the NFTWithStack object

// Access the necessary details
string tokenId = nft.TokenId;
string contractAddress = nft.ContractAddress;
string name = nft.Name;
string description = nft.Description;
string imageUrl = nft.Image; // Image URL for displaying the NFT

// Access NFT attributes
foreach (var attribute in nft.Attributes)
{
string traitType = attribute.TraitType;
var value = attribute.Value;

// Display the attribute details
}

// Display the details in your Asset Details UI
}

Asset Details

You can see an example of how to implement the Asset Details View and the Attributes View in the sample game.

Next steps

Now you can display the player's NFT inventory and showcase detailed information about each NFT in your game. Click next below to learn how to display marketplace listings and enable searching for NFTs based on their attributes.