通过命令行在 ubuntu 下构建 Unity Server

Build Unity Server under ubuntu via command line

我创建了一个非常简单的 Unity 服务器,它使用一个简单的脚本(取自 here)。 我尝试使用以下命令通过 ubuntu bash 构建它:

~/Unity/Hub/Editor/2019.4.30f1/Editor/Unity -batchmode -nographics -logfile stdout.log -projectPath /path/to/the/project -buildLinux64Player /project/build/destination -quit

而且成功了!它能够创建一个工作构建。问题是 3D windows 也会显示。我不想与任何游戏对象进行交互。 有没有办法在没有 GUI 的情况下创建或 运行 可执行文件? 如您所见,我使用了“batchmode”和“nographics”标志,这些标志本应阻止用户界面出现。

我犯了什么错误? 谢谢你的时间。

也许您可以尝试将 #define UNITY_SERVER 指令添加到脚本的顶部。这将启用服务器构建并禁用视觉元素 see 'Server Build' option in the 'Platform list' table

As you can see I used "batchmode" and "nographics" flags which were supposed to prevent the user interface to appear.

是的,但事实并非如此!

但是,这两个标志仅适用于执行构建的 UnityEditor 的 this 实例......它们 not 适用于实际生成的构建应用程序 ;)


通常您会转到 BuildSettings 并启用

Server Build
Enable this checkbox to build the Player for server use and with no visual elements (headless) without the need for any command line options. When you enable this option, Unity builds managed scripts with the UNITY_SERVER define, which means you can write server-specific code for your applications. You can also build to the Windows version as a console app so that stdin and stdout are accessible. Unity logs go to stdout by default.

CommandLine Arguments 下,您可以找到如何通过控制台触发脚本构建。您可以使用 -executeMethod 而不是使用 -buildXYZ 并在该方法中定义确切的播放器并在开始构建过程之前构建所需的设置

#if UNITY_EDITOR
using System;
using System.IO;

using UnityEditor;

using UnityEngine;

class ScriptedBuilds
{
    // Invoked via command line only
    static void PerformHeadlessLinuxBuild()
    {
        // As a fallback use <project root>/BUILD as output path
        var buildPath = Path.Combine(Application.dataPath, "BUILD");

        // read in command line arguments e.g. add "-buildPath some/Path" if you want a different output path 
        var args = Environment.GetCommandLineArgs();

        for (var i = 0; i < args.Length; i++)
        {
            if (args[i] == "-buildPath")
            {
                buildPath = args[i + 1];
            }
        }

        // if the output folder doesn't exist create it now
        if (!Directory.Exists(buildPath))
        {
            Directory.CreateDirectory(buildPath);
        }

        BuildPipeline.BuildPlayer(

            // Simply use the scenes from the build settings
            // see https://docs.unity3d.com/ScriptReference/EditorBuildSettings-scenes.html
            EditorBuildSettings.scenes,
            
            // pass on the output folder
            buildPath,

            // Build for Linux 64 bit
            BuildTarget.StandaloneLinux64,

            // Use Headless mode
            // see https://docs.unity3d.com/ScriptReference/BuildOptions.EnableHeadlessMode.html
            // and make the build fail for any error
            // see https://docs.unity3d.com/ScriptReference/BuildOptions.StrictMode.html
            BuildOptions.EnableHeadlessMode | BuildOptions.StrictMode
        );
    }
}
#endif

然后例如

~/Unity/Hub/Editor/2019.4.30f1/Editor/Unity -batchmode -nographics -logfile stdout.log -projectPath /path/to/the/project -executeMethod ScriptedBuilds.PerformHeadlessLinuxBuild -quit

或使用自定义构建输出路径

~/Unity/Hub/Editor/2019.4.30f1/Editor/Unity -batchmode -nographics -logfile stdout.log -projectPath /path/to/the/project -buildPath path/to/build/destination -executeMethod ScriptedBuilds.PerformHeadlessLinuxBuild -quit