Unity Multiplayer—How to Develop Interactive Games in VR, AR, mobile
It’s fun to play alone but interacting with other players can be much more entertaining. Studies have shown that games can bring friends together and enhance socializing—roughly 70% of all users play with friends at least some of the time (source). As a backend developer, I had a chance to do a thorough research of how it can be done using Unity.
There are several application levels where we can introduce the multiplayer. First of all, we can add it by ‘direct’ interaction between players. We can visualize it with a typical shooting game (FPS) or by solving puzzles with a friend in VR. Secondly, we can make our app more sociable by adding a chat or leaderboards. Of course, it’s common to meet a combination of both.
Multiplayer Features in Unity
Unity has mechanisms that allow you to add a multiplayer feature to the game. For instance, you can use HLAPI (High Level API), which works in a way that one of the users becomes the server and others are joining as clients. Implementation is simple because we mainly operate on NetworkManager, which manages the whole networking process for us, so we can focus on building a high-level logic. As the guarantee of fair gaming and a single source of truth, it’s possible to write remote methods (RPC) which are called by clients but run on the server. We can create one of our methods by adding to the above declaration
[Command] and starting its name with
Cmd (for example
void CmdGiveObject()). Also,
NetworkServer gives us a method to Spawn (create) new objects, so the amount of created bullets, for example, will be controlled by the server.
Unity also gives a more advanced method for networking - Transport Layer API. Here in your own script you set up a networking system on the low level and use one of two protocols: UDP or WebSockets.
Multiplayer with Firebase SDK
As a backend developer, I couldn’t wait to check how Firebase works with Unity. Firebase is Google’s Backend-as-a-Service (on Google Cloud) which is often used by mobile developers to create basic components. Using Firebase SDK in Unity is significantly helping with introducing the multiplayer feature in an application. It’s easy to set up because we create the application in Firebase console and download credential files for mobile systems (
google-services.json for Android and
GoogleService-Info.plist for iOS). Then we download SDK, import it as assets and choose the functionalities we need.
Firebase has set of tools, which helps us with the key components. For example, adding Authentication module goes really fast, which makes it easy to add OAuth with multiple options (Google, Facebook, Twitter, Github, phone) or simple Login-Password method. At Polidea we often Firebase for handling Push Notifications—it’s also possible in Unity apps.
I found the Realtime Database feature to be the most fascinating. It’s the noSQL database which synchronizes a state of the app between devices immediately.
Let’s imagine we’ve got an AR app in which we go shopping with our friends. With Realtime Database we can synchronize what we have in our common virtual basket so we won’t buy the same products.
For the multiplayer conjunction games (such as board games, eg. chess or tic-tac-toe) sharing the moves is a great solution. It’s also a good place to keep the current player state after
save to resume the game later.
Sometimes you can encounter some configuration problems. Most of them are well-known and it’s easy to find the solution on the internet (Myself, I had problems with unavailable Schemas with simulators in Xcode). Unfortunately, some feature issues are caused when certain platforms are not supported. For example, when I tested my authorization script it was returning errors in play mode. That was the moment when I learned that some Firebase plugins won’t work everywhere and I started testing it on an iOS simulator directly, which of course slowed down the whole development process. It’s currently impossible to run desktop Unity apps and it’s not clear if it’s going to be supported any time soon.
There are also other ways of handling the multiplayer feature. First of all, it’s possible to write your own server or example in NodeJS. It requires a thorough understanding of writing games, but in terms of language preferences and freedom of writing performance algorithms, it might be worth considering. Secondly, there are some tools available in the Unity Asset Store. DarkRift is known as the fastest networking library. Unfortunately, it works only with Unity 4, that’s why I can’t wait for DarkRift 2—which will work with Unity 5—to be released (on website creators mention that it’s in beta phase). The most popular networking framework is PUN (Photon Unity Networking) which also works with Unity 5. Additionally, there’s PUN+ that supports Unity 4. Both DarkRift and PUN are free for up to 20 users in one app.
As you can see, there are many options available for adding multiplayer features to your game or application, depending on your needs. Adding a social element to the app and bringing together its users is something worth considering—especially from the business point of view. If you’re looking into adding a multiplayer function to your app—get in touch, we have some experts at Polidea that can make it happen;)
Lead Software Engineer