|
| 1 | +# Unity CLI commands |
| 2 | + |
| 3 | +Drive Netcode for GameObjects from the command line, a CI job, or an AI agent with the [Unity CLI](https://docs.unity.com/en-us/unity-cli/unity-cli). You can use commands to set up a NetworkManager and network prefabs, start and stop sessions, inspect connected clients and spawned objects, simulate poor network conditions, and sample network traffic. |
| 4 | + |
| 5 | +To use Unity CLI commands with Netcode for GameObjects, you must install the [Unity Pipeline package](https://docs.unity.com/en-us/unity-cli/unity-pipeline/unity-pipeline-package) (version 0.9.0-exp.1 or later). Without it, the commands aren't compiled. Run them against an open Unity Editor as `unity command <group> <name> [arguments]`. `unity command --list` shows every command with its arguments. |
| 6 | + |
| 7 | +## Typical workflow |
| 8 | + |
| 9 | +``` |
| 10 | +unity command netcode.gameobjects create_network_manager |
| 11 | +unity command netcode.gameobjects make_prefab --prefab Assets/Prefabs/Player.prefab |
| 12 | +unity command netcode.gameobjects.config set --field player_prefab --value Assets/Prefabs/Player.prefab |
| 13 | +unity command editor.playmode start |
| 14 | +unity command netcode.gameobjects.session start --mode Host |
| 15 | +unity command netcode.gameobjects.session wait_ready |
| 16 | +unity command netcode.gameobjects list_objects |
| 17 | +``` |
| 18 | + |
| 19 | +To test with several players, use [Multiplayer Play Mode](https://docs.unity3d.com/Packages/com.unity.multiplayer.playmode@latest) scenarios (`editor.playmode start_scenario`) and run the commands against each player's Editor with `unity command --project-path <player project path>`. |
| 20 | + |
| 21 | +## Commands |
| 22 | + |
| 23 | +### netcode.gameobjects |
| 24 | + |
| 25 | +| **Command** | **Description** | |
| 26 | +| :--- | :--- | |
| 27 | +| `status` | Each NetworkManager: role (host, server, client), connection state, local client id, topology, session owner, client and object counts, ticks, transport address, and whether the session runs hybrid prefabs. | |
| 28 | +| `list_clients` | Connected clients with their player object, owned object count and round trip time. | |
| 29 | +| `list_objects` | Spawned NetworkObjects as this peer sees them, filtered by name or owner. | |
| 30 | +| `get_object` | One NetworkObject: owner, authority, ownership flags and who can change them, observers and NetworkBehaviours. | |
| 31 | +| `spawn` | Instantiate and spawn a registered network prefab at a position, optionally for an owner or as a player object. | |
| 32 | +| `despawn` | Despawn a NetworkObject, optionally keeping the GameObject. | |
| 33 | +| `change_owner` | Give a NetworkObject to another client, following the topology's ownership rules. | |
| 34 | +| `create_network_manager` | Add a NetworkManager with a UnityTransport to the active scene. | |
| 35 | +| `list_prefabs` | Network prefabs lists with their prefabs, and the lists and player prefab each NetworkManager uses. | |
| 36 | +| `register_prefab` | Add a network prefab to a network prefabs list and save the list. | |
| 37 | +| `make_prefab` | Add a NetworkObject to a prefab's root and report its prefab id hash. | |
| 38 | +| `audit` | Find setup problems that otherwise show when a session starts: in NetworkManagers (transport, topology, prefab lists, player prefab, hybrid prefab requirements) and in network prefabs (NetworkBehaviours without a NetworkObject, nested NetworkObjects, duplicate prefab ids, unregistered prefabs). `--scope config` or `--scope prefabs` runs one half. | |
| 39 | + |
| 40 | +### netcode.gameobjects.profiler |
| 41 | + |
| 42 | +| **Command** | **Description** | |
| 43 | +| :--- | :--- | |
| 44 | +| `get_stats` | Sample network traffic over a number of frames: bytes and messages sent and received, per message type. | |
| 45 | + |
| 46 | +The total bytes sent and received are exact. Per message type, the bytes received can be overstated by up to 6 bytes per message, because Netcode for GameObjects reports received messages with their header's unpacked size. |
| 47 | + |
| 48 | +### netcode.gameobjects.session |
| 49 | + |
| 50 | +| **Command** | **Description** | |
| 51 | +| :--- | :--- | |
| 52 | +| `start` | Start the NetworkManager as host, server or client, optionally with another address and port. | |
| 53 | +| `stop` | Shut the NetworkManager down. | |
| 54 | +| `kick` | Disconnect a client from the server or host. | |
| 55 | +| `reconnect` | Disconnect a client and connect it again, then wait for it to be connected. A client that was already disconnected, for example kicked or timed out, just connects again. | |
| 56 | +| `wait_ready` | Wait until a client is connected, or a server has a number of clients. | |
| 57 | + |
| 58 | +### netcode.gameobjects.simulator |
| 59 | + |
| 60 | +The simulator applies packet delay, jitter and loss to UnityTransport through its simulator pipeline stage, which is available in the Editor and development builds. Settings last for the Editor session and also apply to sessions started later. Once the simulator, `timeout` and `lag_spike` are all off, the transport gets its own simulator settings back. While the simulator is on, the Console shows a warning in each play session it affects, and again when its settings change. |
| 61 | + |
| 62 | +| **Command** | **Description** | |
| 63 | +| :--- | :--- | |
| 64 | +| `get` | The current simulated conditions. | |
| 65 | +| `set` | Set a preset and/or packet delay, jitter and loss. | |
| 66 | +| `list_presets` | Network condition presets. They come from the Multiplayer Tools package (2.0 or later). | |
| 67 | +| `lag_spike` | Drop every packet for a short time. | |
| 68 | +| `timeout` | Drop every packet until turned off, so connections time out. | |
| 69 | + |
| 70 | +### netcode.gameobjects.config |
| 71 | + |
| 72 | +| **Command** | **Description** | |
| 73 | +| :--- | :--- | |
| 74 | +| `get` | The NetworkManager's configuration and the names `set` accepts. | |
| 75 | +| `set` | Set a NetworkConfig field (for example `tick_rate`), the transport `address`, `port` or `listen_address`, the `player_prefab`, or the `log_level`. The log level also changes a running session. | |
| 76 | + |
| 77 | +Commands that change scenes or assets are undoable where Unity supports it and don't save the scene; save it with `unity command scene save`. Commands that change a running session apply to the Editor they run in. |
| 78 | + |
| 79 | +## Hybrid sessions |
| 80 | + |
| 81 | +A session whose network prefabs include `GhostObject` prefabs runs on a Netcode for Entities world. Its traffic goes through Netcode for Entities on port 7979, not through the NetworkManager's UnityTransport. In a hybrid session: |
| 82 | + |
| 83 | +- The `netcode.gameobjects.simulator` commands don't affect the session's traffic. `timeout` and `lag_spike` refuse to run. |
| 84 | +- The transport `address` and `port` set with `netcode.gameobjects.config set` or `netcode.gameobjects.session start` don't apply. |
| 85 | +- `get_stats` counts Netcode for GameObjects messages only, not `GhostObject` replication. |
| 86 | + |
| 87 | +The commands say so in their notes when this applies. For the Netcode for Entities side of the session, use the Netcode for Entities commands, for example `netcode.simulator` and `netcode.profiler get_snapshot_stats`. |
0 commit comments