Fusion 2 Introduction
Overview
For a list of changes from Fusion 1.1 to 2.0 check this page
Fusion is a new high performance state synchronization networking library for Unity. Fusion is built with simplicity in mind to integrate naturally into the common Unity workflow, while also offering advanced features like data compression, client-side prediction and lag compensation out of the box.
Under the hood, Fusion relies on a state-of-the-art compression algorithm to reduce bandwidth requirements with minimal CPU overhead. Data is transferred as partial chunks with eventual consistency. A fully configurable area-of-interest system is supplied to allow support for very high player counts.
The Fusion API is designed to be similar to regular Unity MonoBehaviour code. For example, RPCs and network state is defined with attributes on methods and properties of MonoBehaviours with no need for explicit serialization code, and network objects can be defined as prefabs using all of Unity's most recent prefab features like nesting and variants.
Inputs, Networked Properties and RPCs provide the foundation for writing gameplay code with Fusion.
![overview of the core fusion apis](/docs/img/fusion/v2/getting-started/fusion-intro-api-overview.png)
Using a Networked Property in Fusion:
C#
[Networked] public int lives { get; set; }
Using Network Input for client-side predicted movement:
C#
public override void FixedUpdateNetwork
{
if (GetInput(out NetworkInputData data))
{
data.direction.Normalize(); // normalize to prevent cheating with impossible inputs
_characterController.Move(5 * data.direction * Runner.DeltaTime);
}
}
Declaring a Remote Procedure Call (RPC) in Fusion:
C#
[Rpc(RpcSources.InputAuthority, RpcTargets.StateAuthority)]
public void RPC_Configure(string name, Color color)
{
playerName = name;
playerColor = color;
}
Choosing the Right Mode
Fusion supports two fundamentally different network topologies with the same API as well as a single player mode with no network connection.
The first step when starting with Fusion is to chose between Server/Host and Shared mode.
The Quadrant provides a good starting point for deciding what mode is right for your application.
![the quadrant](/docs/img/quadrant.jpg)
Tutorials
If you already have a good idea for what mode to use you can get started with the tutorials:
If you're not sure yet, keep reading!
Topology Differences
![fusion network topologies](/docs/img/fusion/manual/fusion-topologies.jpg)
Server Mode
In Server Mode the server has full and exclusive State Authority over all objects, no exceptions.
Clients can only modify networked objects by sending their input to the server (and having the server react to that input) or by requesting a change using an RPC.
The server application is built from the Unity project and runs a full headless Unity build. This headless build needs to be hosted on a server machine or a cloud hosted server. Photon does not provide servers for hosting a dedicated Fusion server application.
Client Side Prediction
Client Side Prediction is a popular multiplayer architecture in which clients use their inputs to predict their own movement while awaiting confirmation from the server. This hides latency and allows the gameplay to feel snappy.
In Fusion's Server Mode, any changes a client makes directly to the networked state are only a local prediction, which will be overridden with actual authoritative snapshots from the server once those are received. This is known as reconciliation, as the client is rolled back to the server-provided state and re-simulated forward to the local (predicted) tick.
If previous predictions were accurate, this process is seamless. If not, the state will be updated, and because the network state is separate from the rendering state, the rendering may either snap to this new state or use various forms of interpolation, error correction and smoothing to reduce the visual artifacts caused by the correction.
Host Mode
In Host Mode, the host acts as both a server and a client. The host has a local player, polls input for it and interpolates the rendering as expected of a client.
Overall, this mode is equivalent to the Server Mode but is much cheaper to run as no dedicated server hosting costs are incurred. However, this comes at the price of the state authority's trustworthiness—in other words, a rogue host can cheat.
When running hosted mode from behind a firewall or a router, the Photon cloud transparently provides UDP punch through or package relay as needed.
Since the session is owned by the host, it will be lost if the host disconnects. Fusion does provide a host migration mechanism to allow transferring of state authority to a new client in situations like this, however this is not automatic in Host Mode (unlike in Shared Mode) and requires special handling in the client code.
Shared Mode
In Shared Mode, authority over network objects is distributed among all clients. Specifically, each client initially has State Authority over objects they spawn, but are free to release that State Authority to other clients. Optionally, clients may be allowed to take State Authority at will.
Features such as client side prediction and rollback are not available in this mode. The simulation always moves forward at the same tick rate on all clients.
The Shared Mode network session is owned by the Photon cloud and remains alive as long as any client is connected to it. The Photon cloud serves as a package relay and has full access to the network state with no need to run Unity, allowing for lightweight server logic and data validation (e.g. cheat protection) to be implemented without the need to spin up dedicated server hardware.
For those coming from Photon Unity Networking (PUN), Shared Mode is similar in many ways, albeit more feature complete, faster, and with no run-time allocation overhead.
Cost
The same CCU costs apply for all modes. The server and clients all have to be connected to the Photon Cloud at all times for connection management. In Server Mode there are additional costs for hosting the dedicated server on a cloud service or your own hardware.
Why Fusion?
Still not convinced about using Fusion? Read this page to learn more about why you should use Fusion over Bolt and PUN.
Back to top