Unity and Ethereum: Why and How

Ethereum has great potential for gaming. In this tutorial you will learn why Ethereum is interesting for you as a game developer. By Kevin Small.

4 (4) · 1 Review

Download materials
Save for later
Share
You are currently viewing page 2 of 4 of this article. Click here to view the first page.

May You Live In Interesting Times

Nothing happens for free of course. The execution of programs costs the person who wants to execute it a tiny fee. These fees get distributed amongst the people running the nodes. The people who run nodes are called miners and earn fees in return for providing their computing power to the network. This is how the Ethereum network sustains itself.

The nodes on the network are all copies of each other. Each node contains the same programs and the same data as every other node. When a program runs, it runs on every node during that heartbeat.

This makes the network remarkably resilient. If multiple nodes go offline, as long as some survived, the network can continue running as usual. However, the most significant benefit of having every node identical relates to trust.

The Trustless Network

The Ethereum network is not owned or run by any one individual or company. The software behind it is open source. Anyone can contribute to the source code, and can download the software and set up a node.

Does this mean you have to trust everyone who joins the network to behave nicely and not to tamper with your program code or program results? The answer is an emphatic “No!”. The Ethereum network guarantees that your programs will run as you wrote them, with no possibility of tampering. It also guarantees that the program results will get stored exactly as your program produced them. This is the killer feature that Ethereum offers!

The detail behind how this trust feature works is where Ethereum becomes complex and is outside the scope of this tutorial. However, the basic idea is that it uses techniques from a branch of mathematics called cryptography, along with some clever algorithms. Part of the activity miners do is to validate that interactions with the network follow the Ethereum rules.

The amazing result is that there is a network of computers run by strangers with no central authority, yet you can trust the network to run your programs exactly as you coded them with no possibility of tampering!

Fees

The final piece to understand is fees. Simple reads of the network are free. Anything that causes a change to the network’s programs or data costs a fee. This fee must get paid for by whoever (or whatever) wants the change. Executing a program costs a fee broadly related to how many CPU cycles it requires and how much persistent data storage it changes.

The fee varies and depends on many factors, but at the time of this writing, it’s a few cents. The network fees that get given to miners are not paid in dollars. Fees get paid in a virtual currency (think “in-game” currency) called Ether. Ether is an integral part of the Ethereum network, and it gets traded for dollars on many online exchanges.

Some Terminology

The above explanation avoided using technical terminology. When you read other material about Ethereum, you’ll need to know these technical terms.

  • The Ethereum is a decentralised network because it has no central server controlling the network.
  • The method by which the miners agree on what the ouput of programs should be is the consensus mechanism.
  • The network stores all of its data and programs in a blockchain which is a special data structure that prevents tampering.
  • Ether is part of a family of virtual currencies called cryptocurrencies.
  • The programs that run on the network are smart contracts or dApps (short for “distributed applications”).
  • The 15 second network “heartbeat” is the block time.

Enough theory! Time to get your hands dirty adding the Nethereum library to the demo project.

Installing Nethereum

Nethereum is an open source .NET library for Ethereum. It allows Unity to communicate with the Ethereum network.

Go to the Releases page of the Nethereum GitHub project and find the section titled 2.2.1 Unity3d. Download the file named Nethereum.Unity.zip

In your project, under Assets\RW\, create a new folder named Plugins. Unzip the zip file, and copy the contents into the newly created folder. Your project should now look like this:

screen02.jpg 

Now you’ll learn about accounts and transactions, and you’ll get yourself some test Ether.

Ethereum Accounts and Transactions

It’s time to go deeper inside the EVMs you saw back in diagram 4. Inside an EVM are lots of objects called accounts. Accounts can execute code, transfer Ether, maintain an internal data store and talk to each other. All accounts have an address, which is a long hexadecimal number uniquely identifying it. All accounts have a private key, which is also a long hexadecimal number. This private key is the password to access that account.

By default, the EVM is lifeless. Nothing happens unless someone sends a transaction. A transaction is like a message to an account to do something, like transfer Ether or execute code. When the message is received, the Ethereum network comes to life and processes that transaction. A long hexadecimal number uniquely identifies transactions.

Getting An Ethereum Account

The easiest way to get set up is to use a browser plugin named MetaMask. MetaMask works with Chrome, Firefox and Opera. It lets you use the web browser to access the Ethereum network. Unfortunately, there is no MetaMask release for Safari, so if necessary, first install Chrome. To install MetaMask, follow the steps at the MetaMask site.

First, choose the browser extension:

metainstall_screen01.jpg

Then, add it to Chrome:

metainstall_screen02.jpg

Next, confirm adding to Chrome:

metainstall_screen03.jpg

A small fox icon will appear in your browser toolbar; click that icon to start MetaMask:

metainstall_screen04.jpg

Decline the chance to try the new MetaMask beta:

Accept the terms and conditions and privacy notices:

metainstall_screen05.jpg

MetaMask uses the word DEN to mean your collection of Ethereum accounts. On the next screen, enter a new password and confirm it:

metainstall_screen06.jpg

If you ever forget the password you just entered, you can enter your seed phrase to restore your accounts. So if you can’t keep your password safe, at a minimum, keep the seed phrase somewhere safe:

metainstall_screen07.jpg

MetaMask is now ready to use, however, it defaults to the live Main Network. There are several test networks; in this tutorial, you’ll use Rinkeby.

Click the network name at the top:

metainstall_screen08.jpg

Choose the Rinkeby network from the list:

metainstall_screen09.jpg

MetaMask creates your first account for you and shows a balance of zero Ether. If you want to rename the account, hover your mouse over the account name and click edit:

metainstall_screen10.jpg

Woo hoo! You now have MetaMask set up and an Ethereum account on the Rinkeby network. It’s time to tell Unity this good news.

Copy your address from MetaMask by clicking on the triple ellipsis and choosing Copy Address to Clipboard:

screen14a.jpg 

In the main scene, find the game object HighScoreController. In the inspector, change Player Ethereum Account to the value you just copied from MetaMask.

screen13a.jpg 

Copy your private key (sometimes abbreviated to PK) from MetaMask by clicking on the triple elipses and choosing Export Private Key:

screen14b.jpg 

MetaMask asks you to confirm your password. At this point, verify you’re on the Rinkeby test network and not the live network:

screen15a.jpg 

Then, copy the private key:

screen16.jpg 

In the main scene, find the GameObject HighScoreController. In the inspector, change Player Ethereum Account PK to the value you just copied from MetaMask.

screen13b.jpg 

Are you ready to get some Ether into your account?