Stratis and Unity Integration

Unity is a cross-platform game engine with a built-in IDE developed by Unity Technologies. It is used to develop video games for web plugins, desktop platforms, consoles and mobile devices. It is arguably, one of the most popular gaming engines currently available along with Unreal and GameMaker.

Scripts for Unity are primarily written in C#, and hence it attracted a lot of attention from the Stratis development team. Stratis envisages gaming to become one of the first real use cases for blockchain integration, making games more immersive, private and decentralized. It can blur boundaries between games and push power into the user’s hands.

Stratis platform features smart contracts which can be used in games. Using smart contracts will enable some exciting features in games that may have not been explored before or were highly centralized. One of the most common examples of it would be tokenization. Gamers are very much familiar with this concept. In-game gold tokens can now be purchased with real fiat currency. However, these tokens are highly centralized and cannot be efficiently utilized across games.

Stratis Platform has published unity specific packages that target .NETStandard 2.0 Framework allowing them to be used for various application development platforms such as Unity, Xamarin, ASP.NET etc. The packages currently available are:

More packages will be made available as required. These packages should allow developers to utilize most of the features of the Stratis Platform, including Smart Contracts in C#.

Stratis’ Unity SDK will be unveiled in the new year, incorporating full documentation and additional API’s specifically targeted at those utilizing Stratis Technologies within Unity Game development. Also, Stratis is excited to announce that their upcoming hackathon will focus on building games within Unity using the Stratis Unity SDK. Stratis has already created a gaming development focused channel within its Discord Server to ignite thought-provoking conversations to prepare for the official hackathon announcement.

Environment Setup

This section will give you a quick overview of how Stratis packages can be used for Unity game development. Most of the unity developers should not have any issues to utilise packages listed above.

Register and Install Unity IDE

  1. If you do not have unity account, start by registering unity ID: https://id.unity.com/
  2. Once registered download and install Unity Hub: https://unity3d.com/get-unity/download
  3. Make sure you have Microsoft Visual Studio installed (or any other IDE of your choice for writing C# code like Rider, Visual Studio Code etc.).
  4. Create a new Unity project. You can select a blank project or choose one of the provided templates, such as simple 2D Adventure Game or a more advanced Lego Racing 3D game as an example.

5. Navigate to Scripts section and add a new script

 

This should open your new C# script in an IDE of your choice, such as Visual Studio.

Now it is time to bring Stratis packages in to add some blockchain functionality to our game. Right-click on the solution in the solution explorer and select Manage NuGet Packages. This should open NuGet Package Manager.

Make sure preview packages option is ticked (currently NETStandard Stratis packages are in preview only).

Now you can code using standard Stratis Features. Once you are done with your script, you can close IDE, and the script will be available in your Unity project. If you have any errors, please follow this article to resolve NuGet package issues https://jonfoust.medium.com/using-gcp-nuget-packages-with-unity-8dbd29c42cc4.

Alternatively, packages can be imported using a Unity plugin, which is available here: https://github.com/GlitchEnzo/NuGetForUnity. It should automatically download all the required dependencies and place libraries in the correct folders.

Lets, now create some basic code such as generating private/public key combination. Ensure your newly created script is open in Visual Studio (if it is not, double click on it inside the unity project explorer).

Add new NuGet package: Stratis.Sidechains.Networks.NETStandard as per the instructions above. Once the package is installed, add the following code.

public class BlockchainService
{
    private readonly Network network = Networks.Cirrus.Testnet();

    public KeySet GenerateKeySet()
    {
        var privateKey = new Key();
        var wif = privateKey.GetWif(network);
        var address = privateKey.PubKey.GetAddress(network);
        return new KeySet(address.ToString(), wif.ToString());
    }

    public string SignMessage(string message, string privateKey)
    {
        var sourcePrivateKey = new BitcoinSecret(privateKey, network);
        var signedMessage = sourcePrivateKey.PrivateKey.SignMessage(message);
        return signedMessage;
    }

    public bool VerifySignature(string signature, string publicKey, string originalMessage)
    {
        var address = (BitcoinPubKeyAddress)network.CreateBitcoinAddress(publicKey);
        return address.VerifyMessage(originalMessage, signature);
    }
}

Now you have a basic service that will allow you to generate a private/public key pair, sign a message using your private key and verify a message.

You could use this to generate a wallet address for a user that can then be funded with tokens.

Running External Node

In most cases, you will need to operate a node to interact with Stratis or Cirrus network. It is improbable that developers would be running a node inside a game. It is recommended to host an external node that exposes specific API endpoints that the game would call. You could have a node that allows interaction with a specific smart contract.

Even though the game may not necessarily have a node running, you could still generate all the addresses, fund them outside the game and use offline transactions to propagate to a node.

Here is an example of the basic gaming setup, where a game can use basic blockchain functionality locally and interact with an external node via API to get more advanced functionality.