paint-brush
Unity Realtime Multiplayer, Part 8: Exploring Ready-Made Networking Solutionsby@dmitrii
5,654 reads
5,654 reads

Unity Realtime Multiplayer, Part 8: Exploring Ready-Made Networking Solutions

by Dmitrii IvashchenkoAugust 31st, 2023
Read on Terminal Reader
Read this story w/o Javascript

Too Long; Didn't Read

In multiplayer games, clients need to be synced. Let's look at some ready-made networking solutions for all sorts of use cases.
featured image - Unity Realtime Multiplayer, Part 8: Exploring Ready-Made Networking Solutions
Dmitrii Ivashchenko HackerNoon profile picture


In multiplayer games, clients need to be synced. It's possible to exchange data packets directly from your transport, but this can be complex for devs with little experience. So, let's look at some ready-made networking solutions for all sorts of use cases.


Hello! I'm Dmitrii Ivashchenko, a Lead Software Engineer at MY.GAMES. Our series of articles about the Unity realtime multiplayer landscape in 2023 continues! Today's topic concerns the ready-made solutions for real-time multiplayer that are available for us. Let's get started.

A quick note on transports

In multiplayer games, servers and clients exchange data by sending packets over the network. In order to create a shared virtual space for players who are connecting from different locations, events in the game process (like character movements or object creation) are synchronized with other clients by sending them data packets. The responsible party for sending and receiving packets over the network is called the transport layer.


It's theoretically possible to send these packets by directly calling the send functions of your transport, this approach can quickly become inconvenient for developers who have little experience working with multiplayer games. So, it's a better idea to use one of those listed below, stead of writing your own from scratch. Let's look at those solutions.

Unity Relay & Netcode

Unity offers two Netcode packages: Netcode for GameObjects (in the preview release stage), Netcode for Entities (in experimental mode), and the deprecated UNET. It also provides the Unity Relay service to connect game clients — let's touch on that now.

Unity Relay

Unity Relay is a way for game developers to provide enhanced connectivity between players via a join code mechanism, without the need for investing in third-party solutions, maintaining dedicated game servers (DGS), or worrying about network complexities in multiplayer games. Instead of using DGS, the Relay service provides connectivity through a universal Relay server, acting as a proxy.

Unity Relay


The Relay service allows players to communicate over several different protocols, including UDP, DTLS, and secure WebSocket (WSS). After selecting a Relay server, clients communicate directly with the Relay server using one of the aforementioned protocols. WebSocket connections allow for multiplayer connectivity in browsers using WebGL.


Relay supports working with any game engine. If you are using Relay with Unity, it's a best practice to use the Relay SDK, which is integrated with the Unity Transport Package (UTP).

Unity Transport Package

Unity Transport is a low-level networking library for developing multiplayer games. It underpins Netcode for GameObjects and Netcode for Entities, but you can also use it with your solution.


Unity Transport effortlessly supports all of the platforms supported by Unity Engine, thanks to a connection-based abstraction layer (built-in network driver) provided on top of UDP and WebSockets. You can configure UDP and WebSockets with or without encryption. You can also use pipelines to get additional functionality such as reliability, packet ordering, and packet fragmentation.


The main features of Transport 2.0 include added support for WebGL; this allows the Unity Transport package to be used on all supported platforms of Unity Engine. Transport users now have access to our implementation of Websocket transport, with or without TLS. Players on the move can now take advantage of transparent network migration, typically between cellular towers. This feature is currently limited to the client side and for UDP transport.


To work with Unity Transport, you need to install Unity Editor version 2022.2 or later and also install com.unity.transport package.


Unity Transport Package


Relay works with Unity Transport (UTP), which allows clients to connect that otherwise wouldn't be able to communicate due to routing restrictions, such as strict firewalls.

Unity Relay Limitations

Relay has the following limitations:


  1. There is currently no region-locking functionality; anyone can request allocation in any region if there is enough capacity to serve the request.
  2. The Relay service routes all traffic through one selected host region. As a result, inter-regional communication may not provide optimal latency.
  3. A maximum of 100 players can join a host within a single game session.
  4. The Relay service uses rate limiting to control network traffic by limiting the number of requests received by the API within any given second. For "Create allocation", "Create join code", "Join a Relay" and "List regions" requests, a limit of 60 requests per minute is set. The limit is applied to each authenticated player.


Despite the limitations, the Relay service is a powerful tool for simplifying player connectivity and providing a smooth multiplayer gaming experience.

Netcode for GameObjects

Two of the most popular solutions for Relay's main game network code include Netcode for GameObjects (NGO) and API Mirror Networking.


The recommended best practice is to use NGO (in most cases) because it offers a stable set of core features such as network variables, scene management, remote procedure calls (RPCs), and messaging. However, with its simplicity and ease of use, API Mirror Networking is also great for games that don't require the complete set of features provided by NGO.

Photon Realtime

Photon Realtime Logo


Photon Realtime is the core layer for multiplayer games (and more complex network solutions from Photon). It deals with problems like player matchmaking and fast communication using a scalable approach. Photon Realtime is used both in games and in more specific multiplayer solutions.


Photon Realtime does not include game state and simulation synchronization mechanisms like those found in Fusion or Quantum solutions, but instead focuses on message transport over the network.


The term Photon Realtime also encompasses our extensive framework of APIs, tools, and services, defining the interaction between clients and servers.


All Photon Realtime clients connect to a sequence of dedicated servers, divided into three distinct tasks: authentication and regional distribution (Name Server), player matching (Master Server), and gameplay (Game Server). All of these servers are managed through the Realtime API, so you don't need to worry about them — although knowledge of them can certainly be helpful.


Photon Cloud is a fully-managed service that provides global hosting for Photon Realtime clients. The game code communicates with Photon Cloud, connecting to the cloud, and using its API to perform actions such as connection, joining a random room, or raising events.


In Photon Realtime, room data can be easily saved and loaded, and webhooks can be set up to connect Photon Cloud with an external web server.

Photon Fusion

Photon Fusion


Photon Fusion is a new high-performance library for network state synchronization for Unity. It supports two fundamentally different network topologies and a mode for one player without a network connection using a single API.


Fusion is designed with simplicity in mind for integration into the Unity workflow, and offers advanced features such as data compression, client-side prediction, and delay compensation "out of the box".


In Fusion, for example, RPC and network state are defined by attributes on MonoBehaviour methods and properties, without the need for explicit serialization code, and network objects can be defined as prefabs, using all the latest Unity prefab features.


Fusion uses a modern compression algorithm to reduce bandwidth requirements with minimal overhead on the processor. Data is sent either as full compressed snapshots or as partial blocks with subsequent consistency achieved. In the latter case, a fully customizable interest area system is provided, allowing for very large player counts.


Fusion implements tick-based simulation and operates either in Shared mode or Host mode. The main difference is who has authority over network objects, but this in turn determines which other SDK features are available.


Fusion was developed to replace two existing Photon products for Unity (Bolt and PUN). The important core components of Fusion are NetworkRunner and NetworkObject. NetworkRunner can be considered as the core of Fusion - there is one runner in the scene that manages network operations and simulation.


Fusion offers various pre-built NetworkBehaviours for fast game or prototype creation.

Fusion separates input processing into two steps: collecting input from local hardware and placing it into a structure, then reading that input to change the game state (advancing the simulation).


Fusion supports RPC (Remote Procedure Calls) for cases where standard Fusion input or use of [Networked] properties is not the most practical solution. To get started with Fusion, we recommend studying the Fusion Getting Started Guide.

Photon Quantum

Photon Quantum


Photon Quantum is a fully deterministic engine for multiplayer games. It's based on the prediction/rollback approach, which is perfect for online games that are sensitive to delays, such as action RPGs, sports games, fighting games, FPS, and so on.


When using this engine, there is no need for netcode; all game elements are networked by default and 100% synchronized. You just need to create one simulation with multiple connected players, as when developing a local multiplayer experience. Quantum's deterministic subsystems ensure that simulations on each client are always synchronized and without delays, including: physics, bots, pathfinding, animation.


Photon Quantum is a Fully Deterministic Engine


Deterministic games are inherently resistant to cheating. The fight against cheaters is carried out by checking replays or server-judge simulations (the most effective anti-cheating measure).


Player input is sent to the Photon Cloud servers and is then distributed among the other players. Webhooks can be used to connect your own backend and plugins for executing custom code on the server side.


Photon Quantum is built on an ECS architecture, and high performance is claimed for running even physically intensive multiplayer games on PC, consoles, VR, and mobile phones.


Simulations encoded in Quantum have no dependencies on Unity and can run anywhere. All local actions are performed without delay, and remote inputs are predicted and rolled back. Quantum has the ability to view replays. Replays can be saved on the backend or used in-game. To get started, you can check out the Quantum 100 series.

Normcore

Normcore


Normcore is a high-performance tool for adding a multiplayer mode to any Unity-based project. Normcore includes network physics, persistent spaces, voice chat, and XR support.


Normcore automatically synchronizes all object transformations when you add the RealtimeTransform component to it. This doesn't require any coding. Additionally, Normcore offers state-dependent interpolation and reliable network physics for perfect movement in any connection.


One of the key advantages of Normcore is its fast data transport based on WebRTC. It uses the maximum packet size that does not cause fragmentation during transmission, which speeds up the data transfer process. All data packets in Normcore are encrypted by default, ensuring the security and confidentiality of user data.


Normcore also uses a delta update system. This means that it tracks all changes since the last packet was sent, and when it's time to send a new packet, it already knows what to include in it. This saves resources and improves performance. Data serialization features in Normcore allow for CPU usage optimization. All serialization code is automatically generated prior to project compilation, ensuring fast and efficient resource usage.


Normcore servers operate in regions worldwide and are connected through a private fiber optic network, which provides low latency. You can host Normcore on your own servers or allow Normcore to host a private copy of our cloud infrastructure for you.

Mirror

Mirror Logo


Mirror Networking is a high-level network library for Unity, optimized for ease of use and reliability. This library is designed to simplify the process of working with network connections, allowing developers to focus on creating their projects.


Mirror Networking is compatible with over a dozen low-level protocols and constantly evolves and improves. It includes features for remote procedure calls and context management over the network and also supports working with physics in networked applications.


The library offers over a dozen built-in network adapters and five network management system options, allowing developers to create custom versions. Several complete usage examples are also included to facilitate the learning and coding process.

Nakama

Nakama Logo


Nakama by Heroic Labs is a popular open-source game server that allows you to own your entire infrastructure in one open package. Nakama includes all the necessary real-time gaming APIs and social and competitive features.


Equipped with all the necessary features for your game, such as real-time multiplayer and social and competitive functions, Nakama allows you to customize all aspects both on the client and server-side using Go, TypeScript, and Lua. With Nakama, you can create real-time multiplayer competitive games, customize matchmaking algorithms, add daily rewards, create leaderboards, implement in-game currencies, and provide real-time chat.

FishNet

Fish Networking Logo


Fish-Networking is a free, open-source library for networking solutions in Unity, developed by an experienced game designer. It offers an extensive set of features that are usually only available with paid solutions.


The main advantages of Fish-Networking are bandwidth and resource optimization (which can save on server costs), support for a large number of players (from dozens to hundreds), and built-in features such as client prediction, latency compensation, server load balancing, and support for nested network objects.

An overview of the solutions

The following table provides an overview of various network solutions for game development on Unity, comparing them by the protocols used, supported topologies, maximum number of players per session, the minimum supported Unity version and current status as of mid-2023.

Solution

Protocols

Topologies

Max. players per session

Min Unity version

Status

Netcode for Entities

UDP, WebSockets

Unity Relay, P2P, Client hosted, DGS

100 (Limitation of Unity Relay)

2022.3

🧪 Experimental

Netcode for GameObjects

UDP, WebSockets

Relay (over Unity Relay), P2P, Client hosted, DGS

100 (Limitation of Unity Relay)

2021.3

ℹ️ Pre-Release

Photon Realtime

Reliable UDP, TCP, HTTP or WebSockets

Relay (over Photon Cloud), DGS

32

2018.4

✅ Production Ready

Photon Fusion

TCP, RUDP, WebSockets

Relay (over Photon Cloud), P2P, Client hosted, DGS

200

2020.3

✅ Production Ready

Photon Quantum

RUDP

Relay (over Photon Cloud)

32

2018.4

✅ Production Ready

Normcore

WebRTC over UDP and TCP

Relay (over Normcore Cloud)

100

2020 LTS

✅ Production Ready

Mirror Networking

KCP, WebSockets

Client hosted, DGS, P2P, Relay

200

2020 LTS

✅ Production Ready

Nakama

RUDP

Relay, Authoritative, Client hosted

Limited by server performance

2018.4

✅ Production Ready

FishNet

RUDP, WebSockets

Relay, P2P, DGS

200

2019.4

✅ Production Ready

Photon PUN

UDP, TCP, WebSockets

DGS, Relay, P2P, Client hosted

32

2019.4

⚠️ Deprecated

Photon BOLT

UDP, TCP

P2P, DGS

50

2019.4

⚠️ Deprecated

UNET

TCP, UDP

Client hosted, DGS

Limited by server performance

5.1

⚠️ Deprecated


The variety of the available solutions allow developers to choose a tool that fits their specific needs. Many solutions support different types of topologies, which allows them to be adapted to the specific requirements of a game. Some of the old network solutions, such as Photon PUN, Photon BOLT, and UNET, are outdated and not recommended for use in new projects, while Netcode for Entities and Netcode for GameObjects are still in the experimental or pre-release stage, which also does not yet allow them to be used for production.