paint-brush
Build NFT Games "Play to Earn" With Unity in 2022by@hemendrasingh
1,618 reads
1,618 reads

Build NFT Games "Play to Earn" With Unity in 2022

by Hemendra SinghJuly 27th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In this blog, we'll show you how to use the Unity game engine to create a simple "play to earn" game that uses non-fungible tokens (NFTs).
featured image - Build NFT Games "Play to Earn" With Unity in 2022
Hemendra Singh HackerNoon profile picture

In this blog, we'll show you how to use the Unity game engine to create a simple "play to earn" game that uses non-fungible tokens (NFTs). We'll walk through all the steps necessary to create NFT game, from setting up the project in Unity to implementing the gameplay. By the end of this tutorial, you'll have created a basic "play to earn" game that allows players to collect and trade NFTs.


Before we get started, there are a few things you'll need:


  • A computer with Unity installed. You can download Unity for free from their website.
  • An Ethereum wallet. We recommend using MetaMask, which is a browser extension that allows you to interact with Ethereum dapps.
  • Some ETH in your wallet to pay for gas fees.

1. Create A New Unity Game Project And Import The Necessary Assets

First, we'll create a new Unity project. Open up Unity and click "New Project". Give your project a name and select a location to save it. For this tutorial, we'll be using the 2D template.


Once your project has been created, open up the Asset Store (Window > Asset Store) and search for "nft". Install the "NFTUnity" asset by dragging it into your Assets folder. This asset will allow us to create NFT games and interact with NFTs in Unity.

2. Set Up The Scene

Now that we have the necessary assets, we can set up our scene. Create a new Scene (File > New Scene) and name it "Main". Then, open up the "NFTUnity" folder in your Assets directory and drag the "NFTManager" prefab into your scene. This prefab will handle all of the interactions with our NFTs.


Next, we'll need some way to display our NFTs. For this tutorial, we'll just use a simple SpriteRenderer. Create a new Empty GameObject (GameObject > Create Empty) and name it "NFTDisplay". Then, add a SpriteRenderer component to it (Component > Sprites > Sprite Renderer).


Now we need to set up our user interface. Create a new Canvas (GameObject > UI > Canvas) and make sure the "Render Mode" is set to "Screen Space - Overlay". Then, create a new Button (GameObject > UI > Button) and parent it to the canvas. Set the "Button Type" to "Image Only" and select a sprite for the "Target Graphic". We'll use this button to trigger our NFT interactions.

3. Write The Code

Now that our scene is set up, we can start writing some code. We'll begin by creating a new C# script and attaching it to our "NFTDisplay" GameObject. Name this script "NFTDisplay.cs".


Open up the script and add the following using statements:


using UnityEngine; using NFTUnity; using System.Collections.Generic; public class NFTDisplay : 
MonoBehaviour { }


This will give us access to the necessary classes and methods we'll need for this tutorial.


Next, we'll add a few member variables to our class:


private NFTManager _nftManager; private List<NFTSprite> _nftSprites = new List<NFTSprite>(); 
private int _currentIndex = 0;


The _nftManager variable will store a reference to our NFTManager prefab. The _nftSprites list will store all of the NFTSprite components we create. And the _currentIndex variable will keep track of which NFTSprite we're currently displaying.


Now we need to write a method to initialize our variables:


private void Start() { _nftManager = 
GameObject.Find("NFTManager").GetComponent<NFTManager>(); }


This method simply finds the NFTManager prefab in our scene and stores a reference to it in our _nftManager variable.


Next, we'll write a method to load our NFTs:


public async void LoadNFTs() { var nfts = await _nftManager.GetNFTS(); foreach (var nft in nfts) { 
Debug.Log("Found NFT: " + nft.id); CreateNFTSprite(nft); } UpdateDisplay(); }


This method uses the GetNFTS() method from our NFTManager to get a list of all the NFTs associated with our wallet address. We then loop through this list and create an NFTSprite component for each NFT. Finally, we call the UpdateDisplay() method to display the first

NFTSprite in our list.


Now let's write the UpdateDisplay() method:


private void UpdateDisplay() { if (_nftSprites.Count > 0) { var nftSprite = 
_nftSprites[_currentIndex]; GetComponent<SpriteRenderer>().sprite = nftSprite.sprite; } }


This method simply checks to see if we have any NFTSprites in our list. If we do, it gets the sprite associated with the NFTSprite at the current index and sets it as the SpriteRenderer's sprite.


Now let's write the CreateNFTSprite() method:


private NFTSprite CreateNFTSprite(NFT nft) { var nftSprite = _nftManager.CreateNFTSprite(nft); 
_nftSprites.Add(nftSprite); return nftSprite; }


This method uses the CreateNFTSprite() method from our NFTManager to create an NFTSprite component for the given NFT. It then adds this component to our _nftSprites list and returns it.


Finally, we'll write a method to handle our button press:


public void HandleButtonPress() { _currentIndex++; if (_currentIndex >= _nftSprites.Count) { 
_currentIndex = 0; } UpdateDisplay(); }


This method simply increments our _currentIndex variable. If this variable is greater than or equal to the number of NFTSprites in our list, it resets it to 0. It then calls the UpdateDisplay() method to update the SpriteRenderer's sprite.


Now let's open up our "NFTButton" script and add the following using statement:


using NFTUnity;


This will give us access to the NFTManager class.


Next, we'll add a member variable to our class:


private NFTManager _nftManager;


This variable will store a reference to our NFTManager prefab.


Now we need to write a method to initialize our variables:


private void Start() { _nftManager = 
GameObject.Find("NFTManager").GetComponent<NFTManager>(); }


This method simply finds the NFTManager prefab in our scene and stores a reference to it in our _nftManager variable.


Finally, we'll write a method to handle our button press:


public async void HandleButtonPress() { Debug.Log("Button pressed!"); var nfts = await 
_nftManager.GetNFTS(); foreach (var nft in nfts) { Debug.Log("Found NFT: " + nft.id); } }


This method uses the GetNFTS() method from our NFTManager to get a list of all the NFTs associated with our wallet address. It then loops through this list and prints out the id of each NFT to the console.


And that's it! If you build and run your project, you should now see your NFTs displayed on the screen:

Conclusion

In this tutorial, you've learned how to create an NFT game with unity, Use the NFTManager class to load and display your NFTs in Unity. You've also learned how to write a "play to earn" game that uses NFTs as rewards.

Now that you know how to use the NFTManager class, there's a lot more you can do with it. For example, you could add support for displaying NFT metadata, transferring NFTs, or burning NFTs. You could also use NFTSprites to create an inventory system for your game. The possibilities are endless!