Dedicated Game Server (DGS) | Unity Multiplayer Networking

The Dedicated Server build target is a sub-target of the three Desktop Platforms (Linux, macOS, and Windows) that’s optimized for running as a dedicated server.

Server builds often contain unnecessary assets and compiled code for headless server processes. This data might include artifacts such as audio files, textures, meshes, and shaders. In a multiplayer context, rendering and asset management processes occur unnecessarily when building and executing server runtimes.

The goal of the Dedicated Server build target is to reduce the resource demand of server builds, including the disk size, the size in memory, and the CPU usage. It achieves most of the optimizations through stripping code and assets that aren’t necessary for a server build.

By using the Dedicated Server build target, you can cut back on your server builds’ CPU, memory, and disk space requirements.

Requirements

You must meet the following requirements to use the Dedicated Server build target.

  1. Download and install Unity Editor version 2021.3 LTS or later
  2. Install the Dedicated Server module for the platform you’re targeting. For example, enable Linux Dedicated Server Build Support if you plan to build for Linux.

Get started

You can create a Dedicated Server build through the Unity Editor user interface, with scripts, or through the command line.

Unity Editor

To create a Dedicated Server build through the Unity Editor:

  1. From the Unity Editor, select File > Build Settings…
  2. Select Dedicated Server.

Tip: You can further configure the Dedicated Server build through the Player Settings.

Scripting

To create a Dedicated Server build through a script:

Set buildPlayerOptions.subtarget to (int)StandaloneBuildSubtarget.Server. See the following code snippet for an example.

buildPlayerOptions.target = BuildTarget.StandaloneWindows;

buildPlayerOptions.subtarget = (int)StandaloneBuildSubtarget.Server;

See Creating and Using Scripts to learn more.

Command line

To create a Dedicated Server build through the command line, use the -standaloneBuildSubtarget Server argument. See the following code snippet for an example.

-buildTarget Linux64 -standaloneBuildSubtarget Server

Optimizations

This section describes the optimizations of the Dedicated Server build target for networked applications. The Dedicated Server build target applies some optimizations automatically by default, while others are optional because their impact depends on the game’s implementation.

Automatic optimizations

By default, the Dedicated Server build target performs the following optimizations:

Audio Subsystem

The Dedicated Server build target disables the Audio Subsystem because builds don’t need audio support when running as a dedicated server. Disabling the Audio Subsystem reduces CPU load.

Lighting threads

The Dedicated Server build target removes process threads related to lighting because there’s no need to render lighting on a server build.

Player loop callbacks

The Dedicated Server build target disables the following PlayerLoop callbacks because they aren’t necessary for a server build.

GPU-only assets

The Dedicated Server build target removes GPU-only assets that the server doesn’t need, such as texture pixel data for textures and mesh vertex data, while preserving assets with CPU Read/Write access and assets in the Resource Folders.

Textures and meshes imported with CPU Read/Write access disabled are only accessible by the GPU; the CPU can’t access them. Because the Dedicated Server build target doesn’t initialize a graphics device, there’s no need to include this data. Excluding this data reduces the memory usage of the executable.

See the following lists to learn more about which assets the Dedicated Server build target removes and preserves.

Removed:

  • Textures with CPU Read/Write access disabled.
  • Vertex data for meshes with CPU Read/Write access disabled.

Preserved:

  • Textures with CPU Read/Write access enabled.
  • Vertex data for meshes with CPU Read/Write access enabled.
  • Assets in the protected Resource Folders.
  • Texture metadata (such as the texture size value).
  • Mesh data that internal systems that run on the CPU require (such as physics), even if CPU Read/Write is disabled.

More optimizations

In addition to the automatic optimizations applied through the Dedicated Server build target, you can apply the following implementation-specific optimizations manually.

  • Use conditional compilation to selectively include and exclude code depending on the build target.
  • Separation of player-specific and server-specific code through class implementations.
  • Remove additional items from the PlayerLoop in server builds. See PlayerLoop and PlayerLoopSystem.

See Understanding optimization in Unity to learn more.

Dedicated Server Player Settings

The player settings for the Dedicated Server player are a subset of the Desktop target player settings. That said, due to its headless and server application nature, the following options don’t apply:

  • Icon
  • Resolution and Presentation
  • Splash Image
  • Publishing Settings

To customize the Dedicated Server Player Settings:

  1. From the Unity Editor, select File > Build Settings…
  2. Select Player Settings…

AssetBundles

You can also apply the Dedicated Server optimizations to AssetBundle starting in Unity Editor version 2023.1.0a21. You can old build an AssetBundle through scripting; see the section on AssetBundle for more information on building AssetBundle in general.

To build an AssetBundle to undergo the same Dedicated Server optimizations as discussed for the player, specify the subtarget field in the BuildAssetBundlesParameters struct to be StandaloneBuildSubtarget.Server when calling the BuildAssetBundle method. See the following example:

BuildAssetBundlesParameters serverAssetBundleParameters =
{
outputPath = ,
options = BuildAssetBundleOptions.None,
targetPlatform = BuildTarget.StandaloneWindows64,
subtarget = StandaloneBuildSubtarget.Server
};
BuildPipeline.BuildAssetBundles(serverAssetBundleParameters);

After you build the AssetBundle has, you can load it by a player at runtime (see Using AssetBundles Natively).

Known issue: While the AssetBundle loading process checks if the AssetBundle target platform matches the target platform of the player, it doesn’t check the AssetBundle subtarget. Take care to not load an AssetBundle that was built for a non-server standalone player doesn’t try to load an AssetBundle that targets the dedicated server subtarget (or vice-versa).

Desktop Headless Mode

The Dedicated Server build target is similar to the Desktop Headless Mode build target, except it’s optimized to increase memory and CPU performance when running as a networked application. Desktop Headless Mode is a sub-target for any Desktop Platform target that doesn’t initialize a graphics device by passing the -batchmode and -nographics command line arguments when executing the player. That said, it doesn’t perform any optimizations for running a build as a dedicated server.

Although the Dedicated Server build option performs additional optimizations, you might still want to use Desktop Headless Mode for other purposes, such as automated testing on CI/CD platforms. You can do so by passing the -batchmode and -nographics command line arguments when executing the player or selecting Headless Mode as a build target in the Unity Editor.

You can’t select Headless Mode from the Unity Editor Build Settings, but you can add the -batchmode and -nographics command line arguments to effectively create a headless build.

Next steps

Visit the following resources to learn more about performance optimization in Unity:

https://docs-multiplayer.unity3d.com/netcode/current/reference/dedicated-server/

Related Posts