【问题标题】:Build Unity Server under ubuntu via command line通过命令行在ubuntu下搭建Unity Server
【发布时间】:2021-11-22 07:42:08
【问题描述】:

我创建了一个非常简单的 Unity Server,它使用一个简单的脚本(取自 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 窗口。我不想与任何游戏对象进行交互。 有没有办法在没有 GUI 的情况下创建或运行可执行文件? 如您所见,我使用了“batchmode”和“nographics”标志,它们本应阻止用户界面出现。

我犯了什么错误? 感谢您的宝贵时间。

【问题讨论】:

    标签: unity3d build ubuntu-20.04


    【解决方案1】:

    如您所见,我使用了“batchmode”和“nographics”标志,它们本应阻止用户界面出现。

    是的,但没有!

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


    通常你会去BuildSettings并启用

    服务器构建
    启用此复选框以构建播放器供服务器使用,并且没有可视元素(无头),无需任何命令行选项。启用此选项时,Unity 会构建托管脚本 使用UNITY_SERVER 定义,这意味着您可以为您的应用程序编写特定于服务器的代码。您还可以将 Windows 版本构建为控制台应用程序,以便可以访问 stdinstdout。 Unity 日志默认转到stdout

    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
    

    【讨论】:

    • 是的,谢谢你的工作!抱歉这么晚才回复。我有点难以理解,在-projectPath 中,最后一个/project 参数是可执行文件名。没有那个项目就不会建成
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 2015-05-05
      • 2016-03-13
      • 2020-09-21
      • 1970-01-01
      相关资源
      最近更新 更多