Class Netcode
Main point of access to Netcode APIs. All global calls and configuration should be available from here. When writing gameplay code, this is the main class you should remember when dealing with Netcode. For an unmanaged version that Burst compatible, please use Unmanaged.
Inherited Members
Namespace: Unity.Netcode
Assembly: Unity.NetCode.dll
Syntax
public class Netcode
Fields
RPC
Access the Remotes API
Declaration
public static RPCHandler RPC
Field Value
| Type | Description |
|---|---|
| RPCHandler |
Properties
AllConnections
All connections of the active world. Empty if there is no active world. See AllConnections.
Declaration
public static List<Connection> AllConnections { get; }
Property Value
| Type | Description |
|---|---|
| List<Connection> |
ClientServerTickRate
The ClientServerTickRate singleton is used to configure the client and server simulation time step, server packet send rate and other related settings. The singleton entity is automatically created for the clients in the NetworkStreamReceiveSystem first update if not present. On the server, by contrast, the entity is never automatically created and it is up to the user to create the singletong instance if they need to. This behaviour is asymmetric because the client need to have this singleton data synced with the server one. It is like this for compatibility reason and It may be changed in the future. In order to configure these settings you can either:
- Create the entity in a custom Unity.Netcode.ClientServerBootstrap after the worlds has been created.
- On a system, in either the OnCreate or OnUpdate.
- the maximum number of tick per frame
- the maximum number of tick per frame
- tick batching (<`MaxSimulationStepBatchSize`) and others.
Declaration
public static ClientServerTickRate ClientServerTickRate { get; }
Property Value
| Type | Description |
|---|---|
| ClientServerTickRate |
ClientTickRate
Create a ClientTickRate singleton in the client world (either at runtime or by loading it from sub-scene) to configure all the network time synchronization, interpolation delay, prediction batching and other setting for the client. See the individual fields for more information about the individual properties.
Declaration
public static ClientTickRate ClientTickRate { get; }
Property Value
| Type | Description |
|---|---|
| ClientTickRate |
DeltaTime
The delta time in seconds of the active netcode world. Only valid once netcode is initialized.
Declaration
public static float DeltaTime { get; }
Property Value
| Type | Description |
|---|---|
| float |
InterpolationTick
The current interpolated tick (integral part). Always less than the ServerTick on the Client (and equal to ServerTick on the server).
Declaration
public static NetworkTick InterpolationTick { get; }
Property Value
| Type | Description |
|---|---|
| NetworkTick |
IsActive
Whether there's any online functionality available
Declaration
public static bool IsActive { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsClientBuildOnly
Returns true if the build is a client build, with server content stripped
Declaration
public static bool IsClientBuildOnly { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsClientRole
Returns true if the game has a client role and is connected IsClientRole and IsServerRole can both be true at the same time.
Declaration
public static bool IsClientRole { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsClientServerBuild
Returns true if the build is a standalone build, with all content available This is similar to what a default singleplayer build would be.
Declaration
public static bool IsClientServerBuild { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsHostRole
Returns true if the game has both a client and server role at the same time. If you're a client in a standalone build, you could have IsHostRole == false and IsClientServerBuild == true. You can have a client role, still in a host or client/server build.
Declaration
public static bool IsHostRole { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsServerBuildOnly
Returns true if the build is a dedicated server build, with client content stripped
Declaration
public static bool IsServerBuildOnly { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsServerRole
Returns true if the game has a server role and is listening for connections IsClientRole and IsServerRole can both be true at the same time.
Declaration
public static bool IsServerRole { get; }
Property Value
| Type | Description |
|---|---|
| bool |
LocalConnection
The local connection of the client world, or a default invalid connection if there is no client world.
Declaration
public static Connection LocalConnection { get; }
Property Value
| Type | Description |
|---|---|
| Connection |
Tick
The current simulated server tick the server will run this frame. Always start from 1. 0 is considered an invalid value. The ServerTick value behaves differently on client vs the server. On the server:
- it is always a "full" tick
- i.e. it is strict and monotone, and only increments on the server DGS render (i.e. UnityEngine) frame rate (which you can configure to be the same as this).
- is therefore the same inside or outside the prediction loop On the client:
- it is the tick the client is currently predicting the server should simulate on this frame. I.e. Depends on current EstimatedRTT and TargetCommandSlack values.
- can be either a full or partial (see details on client partial ticks in docs).
- it is not monotone:
- in some rare/recovery situations, it may rollback, or jump forward (due to time/lag adjustments).
- during the prediction loop, the ServerTick value is changed to match either the last full simulated tick or, in case of a rollback (because a snapshot has been received), to the oldest received tick among all entities.
- in both cases, this value will be reset to current predicted server tick at the end of the prediction loop.
Declaration
public static NetworkTick Tick { get; }
Property Value
| Type | Description |
|---|---|
| NetworkTick |
Remarks
Use InputTargetTick (not ServerTick!) when assigning a tick value to your command data values for AddCommandData<T>(DynamicBuffer<T>, T).
Time
The NetworkTime of the active netcode world. Only valid once netcode is initialized.
Declaration
public static NetworkTime Time { get; }
Property Value
| Type | Description |
|---|---|
| NetworkTime |
Unmanaged
Gives access to parts of the APIs that are unmanaged. This is useful when your code needs to run with Burst.
Declaration
public static ref NetcodeUnmanaged Unmanaged { get; }
Property Value
| Type | Description |
|---|---|
| NetcodeUnmanaged |
Methods
Connect(NetworkEndpoint)
Connect to specified server/host endpoint. Will create appropriate client NetcodeWorld. You can specify the endpoint like this
NetworkEndpoint.LoopbackIpv4.WithPort(8888); // will connect to 127.0.0.1:8888 (local connection)
NetworkEndpoint.Parse("123.123.123.123", 1234); // will connect to 123.123.123.123:1234
To specify connection timeouts, see MaxConnectAttempts and ConnectTimeoutMS
Declaration
public static Connection Connect(NetworkEndpoint endpoint)
Parameters
| Type | Name | Description |
|---|---|---|
| NetworkEndpoint | endpoint | The server or host endpoint to connect to. |
Returns
| Type | Description |
|---|---|
| Connection | The local connection of the client world. |
Remarks
You can override driver creation using INetworkStreamDriverConstructor and assign it to DriverConstructor.
Listen(NetworkEndpoint)
Starts listening for connections. Will create the appropriate NetcodeWorld in the background. Accepts a NetworkEndpoint which can be created like this
NetworkEndpoint.AnyIpv4.WithPort(8888); // will listen on 0.0.0.0:8888
NetworkEndpoint.Parse("123.123.123.123", 1234); // will listen on 123.123.123.123:1234
Declaration
public static bool Listen(NetworkEndpoint endpoint)
Parameters
| Type | Name | Description |
|---|---|---|
| NetworkEndpoint | endpoint | The endpoint to listen on. |
Returns
| Type | Description |
|---|---|
| bool | true if successfully listen on specified endpoint. Failure can happen if the port is already used for example. |
Remarks
You can override driver creation using INetworkStreamDriverConstructor and assign it to DriverConstructor.
Listening()
Checks if this server is listening for connections.
Declaration
public static bool Listening()
Returns
| Type | Description |
|---|---|
| bool | True if the server world exists and is listening for connections. |
RegisterPrefab(GameObject)
Registers a prefab with all netcode worlds and also queue for later registration for worlds which are created later.
Declaration
public static void RegisterPrefab(GameObject prefab)
Parameters
| Type | Name | Description |
|---|---|---|
| GameObject | prefab | GameObject prefab to register |
RegisterPrefab(GameObject, NetcodeWorld)
Creates the entity prefab for a given GameObject based prefab. A prefab must be registered before it can be spawned (or pre-spawned), it can be done right before the prefab is instantiates, as long as it's happens on both server and client.
Declaration
public static void RegisterPrefab(GameObject prefab, NetcodeWorld forWorld)
Parameters
| Type | Name | Description |
|---|---|---|
| GameObject | prefab | GameObject prefab to register |
| NetcodeWorld | forWorld | World to link the registration with |
Remarks
This creates the prefab entity in the given world. The registration happens later in ECS systems.
RequestDisconnectAllClients()
Request a disconnect on all client connections from this server. This is done asynchronously. Clients won't be disconnected after this call.
Declaration
public static void RequestDisconnectAllClients()
RequestDisconnectFromServer()
Request a disconnect from the server for this client. This is done asynchronously and won't be disconnected after this call.
Declaration
public static void RequestDisconnectFromServer()
ResolveGhostObject(SpawnedGhost, NetcodeWorld)
Finds the GhostObject instance of a spawned ghost in a world.
Declaration
public static GhostObject ResolveGhostObject(SpawnedGhost ghost, NetcodeWorld forWorld = null)
Parameters
| Type | Name | Description |
|---|---|---|
| SpawnedGhost | ghost | The ghost to find, for example built from GhostInfo. |
| NetcodeWorld | forWorld | The world to search. Uses the active world when null. |
Returns
| Type | Description |
|---|---|
| GhostObject | The GhostObject, or null if it isn't found. |
Examples
var ghost = new SpawnedGhost(serverGhostObject.GhostInfo);
var clientGhostObject = Netcode.ResolveGhostObject(ghost, ClientServerBootstrap.ClientWorld);
if (clientGhostObject != null)
Debug.DrawLine(serverGhostObject.Position, clientGhostObject.Position);
Shutdown(bool, bool)
Goes through all netcode worlds and shuts them down. Shutdown(). Optionally allows keeping the underlying netcode world for reuse. In this case, expect connections to be shutdown asynchronously in the next frame. Note that trying to reuse a world for a different role isn't allowed. For example keeping a client world to serve as a host world. This is because this world contains filtered client systems only and would need to be recreated with additional server systems to work as expected.
Declaration
public static void Shutdown(bool disposeWorld = true, bool reset = false)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | disposeWorld | By default, will dispose all the worlds, destroying the associated ghosts with them. |
| bool | reset | Also clears registered prefabs and OnConnectionEvent callbacks, as if Netcode was never used. Requires |
StartAsHost(NetworkEndpoint, HostWorldMode)
Starts a server for client hosting. This will start listening for connections while also having a local client. The default is a single world, but this can be configured to use a BinaryWorlds setup. Accepts a NetworkEndpoint which can be created like this
NetworkEndpoint.AnyIpv4.WithPort(8888); // will listen on 0.0.0.0:8888
NetworkEndpoint.Parse("123.123.123.123", 1234); // will listen on 123.123.123.123:1234
Declaration
public static Connection StartAsHost(NetworkEndpoint endpoint, NetcodeConfig.HostWorldMode hostWorldMode = HostWorldMode.SingleWorld)
Parameters
| Type | Name | Description |
|---|---|---|
| NetworkEndpoint | endpoint | Endpoint to listen on. |
| NetcodeConfig.HostWorldMode | hostWorldMode | Creates a single world when in SingleWorld mode. |
Returns
| Type | Description |
|---|---|
| Connection | true if successfully listen on specified endpoint. Failure can happen if the port is already used for example. |
Remarks
You can override driver creation using INetworkStreamDriverConstructor and assign it to DriverConstructor.
Events
OnConnectionEvent
Event called for any connection event in any netcode world, including worlds created later. See OnConnectionEvent.
Declaration
public static event OnConnectionEventDelegate OnConnectionEvent
Event Type
| Type | Description |
|---|---|
| OnConnectionEventDelegate |