Connecting a Unity Game to the Game Server
How to connect to your game server to communicate actions that affect asset properties
Previously in the tutorial series, we created a game server that received an 'action' from a dummy javascript client. In this tutorial, we will continue from where we left off having created an Ethereum compatible web3 account in a Unity project, and use this to connect to our server.
Requirements
Basic knowledge of game development in Unity.
A development machine with the latest version of Unity installed
Having followed and completed two previous tutorials, Web3 Wallets in Unity, and Basic Game/Server Authentication, or downloaded the example project from the repositories for those tutorials.
Setup
Either create a new Unity project or continue from where you left off with the Web3 Wallets in Unity tutorial (this is not necessary as we will not be using the Web3 wallet just yet).
Make sure the Game Server is running.
Now, create a new empty GameObject called ServerManager in the scene.
Create a new C# script called ServerManager, and add it as a component of the ServerManager GameObject.
Connecting Unity to the server
We will use the UnityWebRequest POST functionality to send a message to the server.
Let's modify the example from the Unity documentation to make a test connection to the server. Now would also be a good time to add all of the dependencies that we will need:
Save the scene. You can hit the play button if you like, but unless you added some error handling to the server built in the previous tutorial, the server will crash. This is because we are sending junk data.
Encrypting a message correctly
In the previous tutorial, we created RSA keys for our game server. We can embed the public key string into the code of the Unity binary very easily, but parsing this text format correctly, in order to use the default RSA Encryption classes of .NET requires a bit more work.
Fortunately, there is an active open source community in the cryptography space, and we can use the Bouncy Castle package for C# (MIT license) to do much of the heavy lifting. Install Bouncy Castle in your Unity project via NuGet, alternatively you can download the package directly from the NuGet gallery (this tutorial uses release 2.2.1), then unzip the package (rename the extension fo the package file to .zip if necessary), and drag and drop the lib/netstandard2.0/BouncyCastle-Cryptography.dll file to the Unity Assets folder.
a Let's now modify our ServerManager class to add two utility functions to convert byte arrays to hex strings and vice-versa (with later versions of .NET, these functions are not required, but as we are using Standard 2.1, we must implement them). Let's also add a string with the server public key, the data we will send to the server, and the Bouncy Castle dependencies that we will be using:
Now we can create a single new function which will parse the public key, import its parameters into an RSACryptoServiceProvider instance, and encrypt some given data.
This function will return our data as a hex string, in the exact format that our server is expecting. So all we need to do is to assemble the JSON string with the encrypted data, and POST it to the server.
Modify the Start function to the following:
You'll notice that our Upload coroutine is now accepting a parameter, which is the JSON string. So let's update the coroutine to accept that string and send it in the POST request.
Now hit play! (Make sure the server is running!)
If all goes well, check the Unity console to see confirmation from the server that the asset has been modified:
Congratulations! You now have connected your game to the server, and can send actions that will trigger a connection to the Living Assets API.
Last updated