Skip to main content

Configure a Player to simulate different network connections example

Without configuration, tags don't do anything. You must target tags in your game scripts with CurrentPlayer.ReadOnlyTags(). You can place these scripts where you want, but you must attach the scripts to a NetworkObject (such as the Player).

The logic in the following example checks if the tag has a string rather than if it’s equal to a string to allow for more flexibility with the name of the tag. You can change this logic so that it checks for an exact match.

Simulate network conditions

This example script uses the Network Simulation Presets from the Multiplayer Tools package to automatically simulate a fast or slow network based on the tag. A Player with the FastNetwork tag simulates a common Home Broadband connection, and a Player with the SlowNetwork tag simulates a Mobile 2.5G connection.

note

This example uses the Network Simulator tool from the com.unity.multiplayer.tools package.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Multiplayer.Playmode;
using Unity.Multiplayer.Tools.NetworkSimulator.Runtime;

public class NetworkSimulation : MonoBehaviour
{
void Start()
{
var networkSimulator = GetComponent<NetworkSimulator>() ??
gameObject.AddComponent<NetworkSimulator>();

var connectionPreset = NetworkSimulatorPresets.None;
if (CurrentPlayer.ReadOnlyTags() == "FastNetwork")
{
connectionPreset = NetworkSimulatorPresets.HomeBroadband;
}
else if (CurrentPlayer.ReadOnlyTags() == "SlowNetwork")
{
connectionPreset = NetworkSimulatorPresets.Mobile2_5G;
}

networkSimulator.ConnectionPreset = connectionPreset;
}
}