Making a game with NEO + Unity: Part 1

Tim Riley
4 min readSep 22, 2018

--

Welcome to the ‘A-Z’ multi-part series on making a game for the NEO blockchain with the Unity game engine. We’ll be keeping each part simple as to demonstrate what the workflow looks like in its *entirety*. Each part will deal with and tie together a different step, like setting up the basic gameplay, setting up a simple test server to connect the Unity editor to, setting up your dev environment to work with NEO smart contracts, and writing and testing a simple smart contract. 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.

I’ll be working on a Mac, which had some gotchas I’ll (hopefully remember) to cover in this tutorial. For you Windows users the process should be a bit more straightforward and I’ll tend to point you to the existing samples. By the end of the tutorial, you should be armed with enough know-how and stepped over the pitfalls I fell into to power on with your own projects :)

Survival Shooter

We’ll be leveraging the Unity Survival Shooter tutorial and example project as it’s a great starting place for beginners. You can download the sample project and just get going with adding the extra blockchain bits, and if you want to then dig back more into Unity there’s a good set of tutorials built around this already. And that people who are already familiar with Unity can just focus on the working sample.

So, head on over to https://unity3d.com/ and download the latest version of the editor. Run it and create a new project — we’ll just call it SurvivalShooterTutorial. Don’t worry about any of the fancy setup options, as we’ll be importing a package that will overwrite all these settings anyway.

Click on the menu options Window -> General -> Asset Store and search Survival Shooter Tutorial. Select it and click Download and then Import.

Wow, you made a game! In the Project Window open the main scene in Assets -> _Complete-Game -> _Complete-Game (scene), then the Play button to see it in action. You can use the WASD keys on your keyboard to move around and the mouse to aim and shoot.

Double-click _Complete-Game scene object to open it…
… then hit the ‘Play’ button to test it out.

Setting up NEO

To start working with NEO we’ll need to do a little bit more setup. The plugin we’ll be using to interact with the NEO blockchain requires a newer version of the Microsoft .NET framework than is setup by default in Unity. Go to Edit -> Project Settings -> Player and under Other Settings set the Scripting Runtime Version to .NET 4.x Equivalent. You’ll get a popup requesting a restart of the editor, go ahead and accept.

Then grab the NEO Lux Unity Plugin from the team at City of Zion, an independent, international group of open source developers working on the smart economy who puts together *fantastic* easy to use tools for interacting with the NEO blockchain. Once you’ve downloaded the package, you can simply double-click it to import it into your currently open Unity project. Yay, our initial Unity setup is now complete!

Connecting to NEO

Next we’ll need to get our Unity app talking to the NEO blockchain. For this tutorial, we’ll be running our own private server as in my own experience, dealing with the official testnet was in some ways more difficult than running your own (filling out request forms for test GAS, for example).

Right-click under Assets -> Scripts and select Create -> C# Script. Name it CustomRPC, then double-click to open it and Copy + Paste the following code:

using Neo.Lux.Core;
using Neo.Lux.Utils;
using UnityEngine;

public class CustomRPC : NeoRPC
{
private string nodeURL;

public CustomRPC(int port, int neoScanPort, string neoscanPortURL) : base(port, neoscanPortURL + ":" + neoScanPort.ToString())
{
this.nodeURL = neoscanPortURL;
}

protected override string GetRPCEndpoint()
{
return $"{nodeURL}:{port}";
}

public override byte[] GetStorage(string scriptHash, byte[] key)
{
var response = QueryRPC("getstorage", new object[] { key.ByteToHex() });
if (response == null)
{
response = QueryRPC("getstorage", new object[] { scriptHash, key.ByteToHex() });
if (response == null)
{
Debug.Log("Failed the QueryRPC");
return null;
}
}
var result = response.GetString("result");
if (string.IsNullOrEmpty(result))
{
return null;
}
return result.HexToBytes();
}
}

You don’t need to worry too much about what’s going on here. It’s a simple class that inherits from the NEO Lux base RPC class and will allow us to more easily work with our own private net. Which leads us to Part 2: Setting Up Your NEO Private Net.

--

--