Network Runner
Overview
The NetworkRunner
is Fusion’s central Unity component which represents a single networked Peer. All messaging, matchmaking, connecting, spawning, simulation and state replication are orchestrated by this component.
Multiple NetworkRunner
instances can be run in a single Unity instance, with each one representing an individual peer. See Multi-Peer Mode.
Usage
Creation
The Network Runner GameObject can be created in three ways:
- Instantiated from a prefab at runtime
- Loaded as a Scene Object
- Dynamically created at runtime by adding a
NetworkRunner
component to a GameObject
Startup and Connecting
Once created, a NetworkRunner
instance can connect to matchmaking, or it can create/join a Room.
Creating or Joining a Room
Calling StartGame()
from a NetworkRunner
instance creates a Peer, which joins or creates a room as specified by the StartGameArgs
argument. However, in Single Player Mode no connection to the Photon servers is made and no Room is created.
In Multi-Peer Mode, all Scene Objects and Spawned Objects are made children of a dedicated GameObject, and are added to the PhysicsScene/PhysicsScene2D associated with that Runner.
IMPORTANT: You can only use a NetworkRunner
once. Once that NetworkRunner disconnects from a game session or fails to connect it should be destroyed, and a new Network Runner instance should be created to start any new game sessions.
Runner Support Components
On startup, the NetworkRunner
component will find and register all child SimulationBehaviour
components. These components will receive FixedUpdateNetwork()
and Render()
callbacks.
The NetworkRunner
also finds all child components which implement the INetworkRunnerCallbacks
interface and registers them for callbacks.
Built-In Fusion Runner Components
There are a number of Fusion components that can be added to the Network Runner game object which extend the Runner's functionality.
RunnerAOIGizmos
- Add this component to enable Area Of Interest gizmos.HitboxManager
- Automatically added at runtime and manages Hitboxes and Hitbox history. See Lag Compensation.RunnerLagCompensationGizmos
- Add this component to enable Lag Compensation gizmos.RunnerEnableVisibility
- When added, Runner Visibility is enabled for Multi-Peer mode, and all Scene and Spawned GameObjects are registered for Visibility handling. Needed for the Runner Visibility Controls window to work. See Multi-Peer and FusionRunnerVisibilityControlWindow.
Custom SimulationBehaviours Runner Components
You can make your own Runner-specific support components by adding a component which inherits from SimulationBehaviour
or implements INetworkRunnerCallbacks
(or both) to your Network Runner game object.
These components will automatically be found by the NetworkRunner
instance when NetworkRunner.StartGame()
is called, and the associated callbacks will be called when applicable.
Custom INetworkRunnerCallbacks
On startup, the NetworkRunner
component will find and register all child components with INetworkRunnerCallbacks
. See INetworkRunnerCallbacks API Reference.
NOTE: Some callbacks may not be applicable and will not be called, due to execution order. For example, OnPlayerJoined()
will not fire for previously joined players.
Player / PlayerRef Struct
Except in the cases of the Dedicated Server or the Shared Mode Game Server, all peers are assumed to represent human player(s) who are providing inputs. As such, each NetworkRunner
has an associated PlayerRef
struct value.
The local PlayerRef
value is returned by Runner.LocalPlayer
. For the cases where there is no Player (Dedicated Server or Shared Mode Game Server), the value is PlayerRef.None
.
The PlayerRef
is used to indicate which Peer has Input and State Authority of Network Objects and is used for targeting Peers with Remote Procedure Calls.
Network Connection
Connections and transport handling for the peer is wrapped by the NetworkRunner
. It handles connections to the Matchmaking Servers, Room Servers and Game Server.
Tick Management
The NetworkRunner is responsible for determining when ticks need to be simulated and re-simulated. Every Unity Update, the NetworkRunner
determines how many ticks need to be simulated, based on the amount of time that has passed since the previous tick simulation.
Additionally, Clients in a Room continuously receive telemetry from the Server to regulate how far ahead of the server they should be ticking. Clients speed up and slow their tick rate as needed over time, to ensure sent tick updates reach the server prior to it needing them.
When a tick is simulated, Fusion calls all FixedUpdateNetwork()
callbacks. As well as a number of other Event Callbacks related to simulation.