Headless Server

Often times you will want to run a server that uses less resources. To run a Unity application like this you will want to launch it with arguments -batchmode -nographics. You can do this in command line, for example myGame.exe -batchmode -nographics, and inside your game you need to check if it is running headless.

You can take as base the BoltInit.cs script that comes with the Bolt samples, and add or update the Start function like shown below:

using UnityEngine;
using System;
using UdpKit;
using UnityEngine.SceneManagement;

public class BoltInit : Bolt.GlobalEventListener
// ...

    string[] args;
    int arg_map;
    bool batchmode = false;

    void Start()
        args = System.Environment.GetCommandLineArgs();

        if (System.Array.IndexOf(args, "-batchmode") >= 0)
            batchmode = true;


            Console.WriteLine("\nStarted in batchmode");

            arg_map = Array.IndexOf(args, "-map");

            if (arg_map >= 0)
                Console.WriteLine("Loading map:" + args[arg_map + 1]);

                map = args[arg_map + 1];
                BoltLauncher.StartServer(new UdpEndPoint(UdpIPv4Address.Any, (ushort)serverPort));
                Console.WriteLine("You have to specify a map");

// ...

With this script update, you can enter myGame.exe -batchmode -nographics -map Level1 and it will launch a server for your scene "Level1". Of course you can customize this to use a port you enter, a different game mode, etc. by making more argument checks.

You will also want to disable things in the game when running headless such as locking the cursor, spawning a player, and music. For the tutorial there is Screen.lockCursor = true in "PlayerCamera.cs", you can modify the script to only lock the cursor if not in headless mode. The same goes for Player.serverPlayer.InstantiateEntity() in "ServerCallbacks.cs"

 To Document Top