Skip to main content

Create a command line helper

This section shows how to create a command line helper that launches the project outside the Unity Editor to make testing builds easier.

tip

Using a command line helper script to launch multiple instances of a game build isn’t the only way to test a multiplayer game. You can also use the Unity Editor or the Multiplayer Play Mode package.

  1. Right-click the Assets folder in the Projects tab, then select Create > Folder.
  2. Name the new folder Scripts.
  3. Right-click the Scripts folder you created, then select Create > C# Script.
  4. Name the script NetworkCommandLine.
  5. Right-click on NetworkManager within the Hierarchy list, then select Create Empty.
  6. Name the new GameObject NetworkCommandLine.
  7. With the NetworkCommandLine GameObject selected, select Add Component from the Inspector tab.
  8. For the new component, select Scripts > Network Command Line (the NetworkCommandLine.cs script you created earlier).
  9. Double-click on the NetworkCommandLine.cs script from the Project tab to open it in a text editor.
  10. Edit the NetworkCommandLine.cs script to match the following code snippet:
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;

public class NetworkCommandLine : MonoBehaviour
{
private NetworkManager netManager;

void Start()
{
netManager = GetComponentInParent<NetworkManager>();

if (Application.isEditor) return;

var args = GetCommandlineArgs();

if (args.TryGetValue("-mode", out string mode))
{
switch (mode)
{
case "server":
netManager.StartServer();
break;
case "host":
netManager.StartHost();
break;
case "client":

netManager.StartClient();
break;
}
}
}

private Dictionary<string, string> GetCommandlineArgs()
{
Dictionary<string, string> argDictionary = new Dictionary<string, string>();

var args = System.Environment.GetCommandLineArgs();

for (int i = 0; i < args.Length; ++i)
{
var arg = args[i].ToLower();
if (arg.StartsWith("-"))
{
var value = i < args.Length - 1 ? args[i + 1].ToLower() : null;
value = (value?.StartsWith("-") ?? false) ? null : value;

argDictionary.Add(arg, value);
}
}
return argDictionary;
}
}
  1. Save the file, then return to the Unity Editor.
  2. Open the Build Settings window by selecting File > Build Settings.
  3. Select Player Settings….
  4. Beneath Settings for PC, Mac, & Linux Standalone, select Resolution and Presentation to open the section options.
  5. From Resolution > Fullscreen Mode, change Fullscreen Window to Windowed.
  6. Return to the Editor main window and save your scene.

Test the command line helper

Follow these instructions to test that the command line helper script works.

  1. Select File > Build and Run.
  2. Create a new folder called Build inside your Hello World project folder.
  3. Save As the binary “HelloWorld.”

Saving the project in this way causes the Unity Editor to build and launch the project in a new window. After it launches (and you see the plane), close the window you just launched.

Test on Windows

For Windows you should do the following:

  1. Open the Command Prompt.
  2. Use the following command to launch the server and the client. Make sure to replace the placeholder text within the angle brackets (< >) for all commands.
note

You might get a UAC prompt requesting permission to run the executable. You should allow it.

Command to start the server:

<Path to Project>\Build\HelloWorld.exe -mode server

Command to start the client:

<path to project>\Build\HelloWorld.exe -mode client

To run these commands on a single line:

HelloWorld\Build\HelloWorld.exe -mode server & HelloWorld\Build\HelloWorld.exe -mode client

Here’s an example of what your command might look like when you replace the placeholder text in < >:

C:\Users\sarao>HelloWorld\Build\HelloWorld.exe -mode server & HelloWorld\Build\HelloWorld.exe -mode client
info

There's no standard out stream on Windows by default, so you will need to view the Debug.log file to see the outputs. You can find the Debug.log files in:

C:\Users\username\AppData\LocalLow\CompanyName\ProductName\Player.log

Where the CompanyName should default to DefaultCompany for a new project and ProductName should be equal to the project's name.

Alternatively you can change the Windows commands to create a log.txt file in the same folder as your HelloWorld folder.

Change the commands as follows:

Server command:

<Path to Project>\Build\HelloWorld.exe -logfile log-server.txt -mode server

Client command:

<Path to Project>\Build\HelloWorld.exe  -logfile log-client.txt -mode client

Example (Running as a single command line):

C:\Users\sarao>HelloWorld\Build\HelloWorld.exe -logfile log-server.txt -mode server & HelloWorld\Build\HelloWorld.exe -logfile log-client.txt -mode client

Test on Mac

Use the following instructions if you're using MacOS:

  1. Open the Terminal app.
  2. Use the following command to launch the server and the client. Make sure to replace the placeholder text within the angle brackets (< >) for all commands.

Command to start the server:

<Path to Project>/Build/HelloWorld.app/Contents/MacOS/<Project Name> -mode server -logfile -

Command to start the client:

<Path to Project>/Build/HelloWorld.app/Contents/MacOS/<Project Name> -mode client -logfile -

To run both as a single command:

<Path to Project>/Build/HelloWorld.app/Contents/MacOS/<Project Name> -mode server -logfile - & ; ~ <Path to Project>/Build/HelloWorld.app/Contents/MacOS/<Project Name> -mode client -logfile -