Making a game with NEO + Unity: Part 2

Tim Riley
5 min readSep 22, 2018

--

Welcome to part 2 of the ‘A-Z’ series on making a game for the NEO blockchain with the Unity game engine. In part 1 we setup our basic Unity environment and got things ready to connect to our private testnet. In part 2 we’ll be setting this testnet up.

We’ll be keeping each part simple as to demonstrate what the workflow looks like in its *entirety*. We’ll be going quickly over certain aspects, but feel free to comment on anything that isn’t clear and I’ll be happy to edit.

Private Testnet

NEO Lux gives you some handy methods to connect your game to the official testnet or mainnet, but in this tutorial we’ll be creating our own privatenet. It’s fairly straightforward to do and once you get over the initial hurdles (which I’ll walk you through) I found it easier to deal with than the testnet. For example while filling out request forms for test GAS or tokens may not be the most difficult task in the world, I find little bits of friction like that tend to put the brakes on my own enthusiasm + momentum.

You could run your privatenet locally, but for this tutorial we’ll setup our own server. This will make things like working in a team or with builds a lot more straightforward. I’ll be using Digital Ocean as in my experience they fit a good performance + price ratio.

Disclaimer: I’m not much of a backend / server guy, and I guess something like a ‘medium’ level comfortable with the command line. These next bits should be easy breazy for people who know what they’re doing; if you know you’re way around Linux + Docker you can go right ahead to Installing the NEO Private Net from Docker Hub and running NEOSCAN with Docker Compose. For the rest of you who are more or less at my own level these next bits should still be pretty straightforward.

Create an account with Digital Ocean if you don’t already have one. Then create a droplet with the following options.

Things should work fine with 16.x or 18.x Ubuntu
One of the medium tiers should be enough — feel free to experiment on your own with more or less in your configuration and let me know your results in the comments.

SSH into your new droplet and install docker. It’s extremely straightforward if you follow Digital Ocean’s own guide:

https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-18-04

Note: I ran into a slight hiccup on the very first step adding the GPG key for the official Docker repository to the system. I simply ran the command twice and all was well.

Then install docker compose. Again, the instructions are very straightforward. Just make sure you’re following the ones for Linux.

Installing the Private Net and NEOSCAN

The beefy hard part of running and installing our own private net was thankfully taken care of by the City of Zion folks. You can checkout their full set of instructions here. Still SSH’ed into your droplet, we’ll basically only need a few more commands:

First, pull in the docker image from hub…

docker pull cityofzion/neo-privatenet

…download NEOSCAN…

wget https://raw.githubusercontent.com/slipo/neo-scan-docker/master/docker-compose.yml -O docker-compose-neoscan.yml

…and run privnet…

docker-compose -f docker-compose-neoscan.yml up

Let it spin up, then head over to http:your-droplet-ip-address:4000 to watch NEOSCAN in action:

Back to Unity…

I don’t want to get too deep into my own style with this tutorial, but one part I will do this is with importing the UniRx plugin into Unity. UniRx is a reimplementation of the .NET Reactive Extensions optimized for Unity and with fantastic iOS IL2CPP compatibility .Without diving too deep, it’s going to simplify some of the code that manages NEO and our UI.

Under Assets -> Scripts create another script called NEOManager and copy + paste the following code.

using System.Collections;
using UnityEngine;
using System;
using UniRx;
using Neo.Lux.Core;
using Neo.Lux.Cryptography;
using UnityEngine.UI;

public class NEOManager : MonoBehaviour
{
public NeoAPI API;
[SerializeField] private string RpcIP;

public const string AssetSymbol = "GAS";

[HideInInspector]
public KeyPairReactiveProperty PlayerKeyPair = new KeyPairReactiveProperty();

[SerializeField] private Text addressText;
[SerializeField] private Text balanceText;

public Decimal GASBalance;

private void OnEnable()
{
PlayerKeyPair.Value = KeyPair.FromWIF("KxDgvEKzgSBPPfuVfw67oPQBSjidEiqTHURKSDL1R7yGaGYAeYnr");

#if UNITY_EDITOR
//this.API = new LocalRPCNode(30333, "http://localhost");
this.API = new CustomRPC(30333, 4000, "http://" + RpcIP);
#else
//this.API = NeoRPC.ForTestNet();
this.API = new CustomRPC(30333, 4000, "http://" + RpcIP);
#endif

this.PlayerKeyPair.DistinctUntilChanged().Where(kp => kp != null).Subscribe(keyPair =>
{
this.addressText.text = "Address: " + keyPair.address;
this.balanceText.text = "Balance: Please wait, syncing balance...";

StartCoroutine(SyncBalance());
}).AddTo(this);
}

private IEnumerator SyncBalance()
{
yield return null;
var balances = this.API.GetAssetBalancesOf(this.PlayerKeyPair.Value);

this.GASBalance = balances.ContainsKey(AssetSymbol) ? balances[AssetSymbol] : 0;
this.balanceText.text = "Balance: " + GASBalance.Value.ToString() + " " + AssetSymbol;
}
}

[Serializable]
public class KeyPairReactiveProperty : ReactiveProperty<KeyPair>
{
public KeyPairReactiveProperty() { }
public KeyPairReactiveProperty(KeyPair initialValue) : base(initialValue) { }
}

Save the script and go back to the main scene. Create an empty GameObject, name it NEO, then create an empty child GameObject of this and name it NEOManager, and add the NEOManager script to it. Punch in the ip address of your droplet next to the RpcIP field in the inspector.

Next we’ll create some UI elements and wire them up in the inspector so that we can see our balance update when we connect to the mainnet.

Create a UI canvas and add two child text elements to it, then click and drag their references to the Address Text and Balance Text fields in the NEOManager.

Hit Play and watch the address and balance update.

For now, we’re using a test account that was automatically generated when we installed the privatenet. In the next part, we’ll set things up so that our player’s can enter their own private keys and start interacting with the NEO Blockchain.

--

--