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.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Unity and Ethereum: Why and How
30 mins
- Why Ethereum Is Interesting
- The Game Ecosystem Today
- The Game Ecosystem With Ethereum
- Opportunities In The Game Ecosystem With Ethereum
- Starter Project
- How Ethereum Works
- May You Live In Interesting Times
- The Trustless Network
- Fees
- Some Terminology
- Installing Nethereum
- Ethereum Accounts and Transactions
- Getting An Ethereum Account
- Getting Some Ether Into Your Ethereum Account
- Reading An Account Balance
- Preparing To Interact With The High Score Contract
- Retrieve High Scores From Ethereum
- Write High Score to Ethereum
- Examining Ethereum High Score Transactions
- Challenges Facing Ethereum in Games
- Where to Go From Here?
- Learn More About Ethereum and Solidity
- Learn More About Nethereum
- Get Inspired
Write High Score to Ethereum
Return to your code editor, find the method SubmitHighScoreCoroutine
, and replace its body with:
public IEnumerator SubmitHighScoreCoroutine()
{
// Create the transaction input with encoded values for the function
var transactionInput = scoreContractService.CreateSetTopScoreTransactionInput(playerEthereumAccount, contractOwnerAddress, contractOwnerPK, aliveTimeMilliSeconds, gasLimit);
// Create Unity Request with the private key, url and user address
var transactionSignedRequest = new TransactionSignedUnityRequest(networkUrl, playerEthereumAccountPK, playerEthereumAccount); // 1
// Send request and wait
yield return transactionSignedRequest.SignAndSendTransaction(transactionInput);
if (transactionSignedRequest.Exception == null)
{
// Get transaction receipt
Debug.Log("RW: Top score submitted tx: " + transactionSignedRequest.Result);
}
else
{
Debug.Log("RW: Error submitted tx: " + transactionSignedRequest.Exception.Message);
}
}
The above code is similar to what you’ve seen before. It prepares a request, sends it and interprets the results. The comment // 1
shows the notable difference where you use the player private key and address to authorize the sending of a transaction. This spends some of the player’s Ether to pay the miners.
Save and play the scene. Press space to start playing and try to get a good score — at least enough to get you into the top 40. When your elephant dies, wait a few seconds. Then, press the refresh button and wait a few more seconds. If your high score made it into the top 40 it should appear in the list:
You’ll also notice your Ether balance decreased slightly with the cost of the transaction to submit the high score.
Examining Ethereum High Score Transactions
You can see the status of a transaction by using a block explorer. When the game submits a high score, the console shows a message starting RW: Top score submitted tx:
followed by a transaction number:
You can create a URL with the pattern:
https://rinkeby.etherscan.io/tx/[transaction ID here]
And enter it in a browser to see the transaction status like this.
Whew! That’s been quite a tour! You have now finished the practical part. The next sections wrap up this tutorial, starting with some opinions about the challenges facing Ethereum in games today.
Challenges Facing Ethereum in Games
Steve Wozniak, one of the founders of Apple, said recently that Ethereum could be as influential as Apple in the long term. Remember this technology is just starting!
The main challenges facing Ethereum as it relates to game development include those listed below.
- Resistance. You will understand that the encumbents who currently store your gameplay data may be reluctant to see other services get involved.
- Scaling. Ethereum can cope with around 10 transactions a second. This needs to go higher to allow widespread adoption. Casper aims to introduce proof-of-stake to the Ethereum networking. This could make sharding a reality, helping with the scaling problem.
- Fees. At the time of writing, fees are around a few cents. This is too expensive for some uses.
- Public Bugs. Using smart contracts on a public blockchain means that bugs are visible to all but you cannot fix them quickly.
Where to Go From Here?
Download the completed project from the link below. The art in the project is by Kenny.nl.
Learn More About Ethereum and Solidity
Once you’re done playing with the demo project, you may like to explore deeper into Ethereum.
A good place to start learning about Ethereum and Solidity is its official site. A writer and speaker that I recommend is Andreas Antonopoulos who has a book coming out about Ethereum later in 2018.
Learn More About Nethereum
The Nethereum project is open source. Thanks to Juan Blanco whose advice and demo code inspired the samples here. You can join their Gitter and chat with others about the project.
Get Inspired
Take a look at what others have already done with games and Ethereum for more inspiration:
Games
- CryptoKitties is like a card trading game where you collect and breed digital cats. This game received lots of press because it was at one time so popular it affected the performance of the whole Ethereum network.
- CryptoCuddles is a card battle game built on top of the CryptoKitties game. You can battle with kitties that you’ve bred in CryptoKitties. This is because the CryptoKitties smart contracts are open for any other developer to use. Very cool!
- ChainMonsters is very interesting, it is a bit like Pokémon on Ethereum with smart contracts publicly visible.
Services
- uPort is a project for managing people’s Ethereum identities. uPort will make it easier to use the network without having to enter a long private key.
- GamerToken wants to be a common in-game currency that any game can use.
- Ownage wants to be an in-game item trading platform.
We hope you have enjoyed the tutorial, if you have any questions or comments, please join the discussion below.