How to create a Loom Game in Unity

--

Loom Network’s Basechain is a solid way to integrate Blockchain capabilities into your game, and with their LoomSDK it is easy to build Blockchain features into your next Unity project. This tutorial will show you what you need to get started and lead you step by step to your first Loom Dapp. It is part of a series and builds on our CoinCollector contract that we created and deployed in this article. If you don’t know why you would need Blockchain Technology in you game have a look at this article.

Unity Project Set-up

To get started, we need to install the latest version of Unity. This is straight forward downloading an installer and creating a Unity account. Here is a guide showing you exactly what to do.

Once you installed Unity open the Unity Hub and create a new 3D project. Download the LoomSDK and add the folders /LoomSDK and /LoomSDKBuild inside the /UnityProject folder to your Unity Assets folder. Additionally, you need to adjust some settings in Unity. Access File -> Build Settings -> Player Settings -> Other Settings -> Configuration and set the API Compatibility Level to .NET 4.x. With this you are ready to use LoomSDK features in your Unity project.

Deploying Smart Contract to Loom

Our project is setup, next we need something to talk with on the Loom Basechain. For this we need to develop a smart contract in Solidity and deploy it to the Basechain. We put this topic in its own article. Either deploy your own contract and then come back here to continue creating your game, or if you already know how to deploy contracts you can use the contract we deployed at 0xfC4ce0E01e8fdd309af77564bF5bE8b7dE79E3e9.

Creating a Game in Unity

We based our example Dapp on the great tutorials provided by Unity. They created a whole series of great video tutorials with example projects you can download. Check them out here.We used the Roll-a-ball Tutorial to start with, turning it into a Blockchain-based coin collector game. If you are already familiar with Unity you can download the final Scene from the Asset Store. When you finished try the game to see how it works.

Now that we have a simple game with a character we can control, coins we can collect and a counter that shows how many coins we collected, let’s connect it to our contract on the Loom Basechain.

Connecting to Loom Basechain

First create a new Empty GameObject in your scene and call it “LoomManager”. Select the object and Add Component -> new Script called LoomManager.cs. This is where all our Loom setup code will go.

To be able to create a connection to the Basechain we need to create an account first. This is done by generating a new private and public key pair. Inside the LoomManager.cs, so open the file and add this code to Start().

Then we can use this account to connect to the Basechain and access our CoinCollector contract we deployed earlier. Using the following GetContract() method in Start() after the key creation.

GetContract() creates a reader and writer for Basechain which are used to setup a client that can connect to Basechain. This client is unlocked with your key pair. Then we can use this client, our CoinCollector contract address and ABI (that we saved earlier when deploying the contract) to initialize a Loom.Client.EVMContract Object which will be our handle to access the remote contract.

This EVMContract Object will be used in a wrapper that we call CoinCollectorContract.cs. This wrapper provides a convenient C# interface to interact with our contract from code.

The CoinCollectorContract.cs is simply wrapping each of the CoinCollector.sol public functions in asynchronous C# Tasks that we can await.

Now let’s use the contract in our game logic. First of all we need to start a game with coinCollectorContract.StartGame() in LoomManager.Start() after initializing our Blockchain client. startGame() emits an event.

We need to access our contract in other parts of our game too. Therefore, we need to inject our LoomManager Object into each Unity Component that wants to use it. By adding a public LoomManager property to the Component and drag and drop the LoomManager Object in your Scene on to the slot in the PlayerController Component in the Player Objects.

For the game to wait for the start we must stop the input update in our PlayerController Object. This is done by checking if the game has already started.

Our player must collect coins. This crucial task has to be secured — by Blockchain of course. Therefore, we inject the LoomManger into the PlayerController Object and call CollectedCoin() when the collision with a coin on the board is triggered.

Now the game is automatically started when we enter the game and we can collect coins. But when does the game end? This information is stored in our contract, and it will even inform us with an onEndGame() event when we have collected enough coins. So we need to listen to this event in the LoomManager. After receiving this event the game will stop.

We are nearly done. The game starts and ends. Now it would be nice to know how many coins we already collected. Our UI needs to be updated. We solved this very simple with calling GetCoins() each time the UI is updated. A more sophisticated solution would be to listen to the onCoinCollected() event emitted by the contract. I leave this as an exercise for you. 😉

I put the complete LoomManager.cs, CoinCollectorContract.cs and PlayerController.cs code below for reference.

Play test, Iterate and Grow

Congratulations, you created your fully decentralized Blockchain game. Now your Dapp should be playable from everywhere you have Internet!

From here you can play test, iterate and grow this simple game. Or you start from scratch with your own ideas. Let me give you some thoughts about the architecture of your Dapp in bigger projects. You noticed, that we created a stand-alone LoomManager GameObject. This helps us decouple all the Blockchain related things from the rest of our game. You could turn this LoomManager easily into a Prefab and re-use it through out other projects or publish it even as Unity Asset Package.

When you game grows more complex you might not have just one contract but many. To hide this complex infrastructure from the rest of your game developers and make their life easier, you could implement a command-based façade. This façade stays the same while you can rework and change your whole smart contract infrastructure behind it.

To make you game more efficient instead of polling the Loom Basechain constantly utilize events in your smart contracts. Then you can implement listeners to those events in your LoomManager that trigger updates on components that need this new information.

For further discussion of an architecture for games utilizing Loom and Unity see this article I wrote a few month ago.

Who are we?

Our vision at Calystral is to unleash the potential of gamers. We are focusing on building games where the time and effort put in is rewarded with real value. Enhancing the players’ experience and empowering gamers to achieve more. In the process we strive to overcome technical limitations of Blockchain Technology and use its benefits to lift games to the next level. We would love to share these solutions with the community and other developers. Towards a better future of Gaming!

________________________________________

Join our journey:

Calystral Discord Twitter Facebook Medium Telegram

--

--

Marcel Danz - Minimalist Multiplier

Minimalist Multiplier | 1% better everyday | Entrepreneur | Digital Nomad | Business Mentor | Environment Designer | Essentialist | Acrobat