Building a Dedicated Server
Instructions for how to build a dedicated FishNet server.
FishNet supports running your project as a dedicated server, meaning Unity runs without rendering graphics and focuses purely on network logic. This is the recommended way to host multiplayer games at scale. You can deploy this build to a server hosting service, your own physical servers, or even give it to your players to allow them to host their own servers for your game.
Learn more about Unity's Dedicated Server Build Profiles here.
Required Unity modules
Before building your server, ensure you have the correct Unity modules installed. You will need the Dedicated Server Build Support module for your server's operating system. This is usually Linux, but it can be Windows or MacOS too if needed.
To install the modules through the Unity Hub:
Open the Unity Hub
Go to the Installs tab
Find your desired Unity version
Click " Manage" and then Add Modules
Tick the relevant build targets
Click Continue and complete the installation.

Ensuring FishNet's server starts
With the default settings, FishNet detects when the game is built as a dedicated server and will start the FishNet server as soon as the NetworkManager is loaded. You can change this setting by adding the ServerManager component to your NetworkManager and adjusting the Start on Headless option. You may want to do this when you are starting the server yourself, for example if you want to dynamically set a custom port or server bind address.
If you disable this functionality you may want to implement it yourself; in the script below, we use the UNITY_SERVER
preprocessor directive to only include the server starting line of code when built as a Dedicated Server. You can find more options such as this here: https://docs.unity3d.com/Manual/platform-dependent-compilation.html
using FishNet.Managing;
using UnityEngine;
[RequireComponent(typeof(NetworkManager))]
public class NetworkServerStarter : MonoBehaviour
{
private void Start()
{
#if UNITY_SERVER
GetComponent<NetworkManager>().ServerManager.StartConnection();
#endif
}
}
Configuring the transport
It's important to set up your chosen transport's settings for your dedicated server. The default transport Tugboat should in most cases have the Reuse Address option enabled.

In addition to that, you may want to set your Port and Server Bind Address. We'll take a look at how we can set these dynamically next.
Using command line arguments
If you find yourself needing to set the port from command line arguments, you can use a simple script like the following on your NetworkManager:
using UnityEngine;
using FishNet.Managing;
using System.Diagnostics;
[RequireComponent(typeof(NetworkManager))]
public class NetworkCommandLineArgs : MonoBehaviour
{
private NetworkManager networkManager;
private void Start()
{
networkManager = GetComponent<NetworkManager>();
ushort? port = GetPortFromCommandLine();
if (port.HasValue)
{
networkManager.TransportManager.Transport.SetPort(port.Value);
networkManager.Log($"Port set to {port} via command line.");
}
else
{
networkManager.Log("No valid port found in command line args. Using default.");
}
StartDedicatedServer();
}
[Conditional("UNITY_SERVER")]
private void StartDedicatedServer()
{
networkManager.ServerManager.StartConnection();
}
private ushort? GetPortFromCommandLine()
{
string[] args = System.Environment.GetCommandLineArgs();
for (int i = 0; i < args.Length; i++)
{
if ((args[i] == "-port") && i + 1 < args.Length)
{
if (ushort.TryParse(args[i + 1], out ushort port))
return port;
}
}
return null;
}
}
This code will look for command arguments in this format: -port <number>
. For example, running: game.exe -port 7777
will the set the transport to use port 7777.
On line 28 you will find the use of the UNITY_SERVER
conditional added to the StartDedicatedServer
method. We run this after setting the port to start the FishNet server, but only if it's a dedicated server.
Configuring the server's logging
For your dedicated server, logs are essential for tracking errors, monitoring game states, and resolving issues. Consider customizing the following settings to enhance logging efficiency:
FishNet's logging level
FishNet's NetworkManager has a Logging field which can be used to select a Level Logging Configuration asset to customize the FishNet logging settings. You can create such an asset from the (Assets → Create → FishNet → Logging → Level Logging Configuration) menu. If you set the Headless Logging option to "Common", you will be able to see regular debug logs, including connection events such as clients joining and leaving and the server starting, which can be quite helpful. You may also want to enable timestamps or the local tick in the logs.

Shader errors
When you run your server you may notice a lot of errors and warnings related to shaders, this is normal as Unity is running without graphics. Fortunately, Unity has added an option to fully strip out shaders and fonts, thereby removing most of these errors. You can enable it in your project's settings.
Go to Edit → Project Settings → Player → Other Settings
Under Optimization, enable "Dedicated Server Optimizations"

Excluding files from the server
With server builds you will often want to exclude assets that aren't completely necessary on the server, such as audio files and textures. While Unity can handle stripping of a lot of these itself, you sometimes will want to manually force stripping of others. You can easily do this for code through using Assembly Definitions and choosing to exclude (or include) the dedicated server platforms.
If you want to do this for other asset file types it's recommended to use one of the following tools:
Creating a server build
With the needed build profile is installed, you can switch to it and build the server.
Go to File → Build Profiles and select your target server platform.
Include your game scenes carefully. You can have your server begin in its own scene, a bootstrap scene, or the same scene as client builds. Ensure the NetworkManager is present in the starting scene, a scene loaded via code, or instantiated at runtime.
Click Build and choose an output folder for your server build. Once it's done you can upload it to your server or server hosting service and then launch your game servers.
Last updated