从命令行构建 Unity 项目

要在命令行界面 (CLI) 上构建用于嵌入式 Linux 系统的 Unity 项目,您必须在构建主机上安装 Unity 编辑器。构建主机可以是 Linux、Windows 或 macOS 机器。

以下示例使用可执行文件 (Unity) 的 Linux 变体,但您可以将其替换为您首选构建主机的操作系统的等效可执行文件。

命令行参数

要指示 Unity 在 CLI 模式下启动并在<path-to-unity-project-root> 为嵌入式 Linux 构建项目,请运行以下命令

Unity -quit -batchmode -nographics -buildTarget EmbeddedLinux -executeMethod Builder.Build -projectPath <path-to-unity-project-root>

构建过程还会调用函数Builder.Build 以继续构建设置过程。

构建脚本

您可以将示例构建脚本Assets/Editor/ 放入要从命令行构建的项目中。使用-executeMethod 选项调用此类的Build() 方法,该方法设置构建选项并触发构建。

有关更多信息,请参阅BuildPipeline.BuildPlayer 文档

using UnityEditor;
using UnityEditor.Build.Reporting;
using UnityEngine;

public class Builder
{
    private static void BuildEmbeddedLinux(EmbeddedLinuxArchitecture architecture)
    {
        // Set architecture in BuildSettings
        EditorUserBuildSettings.selectedEmbeddedLinuxArchitecture = architecture;

        // Setup build options (e.g. scenes, build output location)
        var options = new BuildPlayerOptions
        {
            // Change to scenes from your project
            scenes = new[]
            {
                "Assets/Scenes/SampleScene.unity",
            },
            // Change to location the output should go
            locationPathName = "../EmbeddedLinuxPlayer/",
            options =  BuildOptions.CleanBuildCache | BuildOptions.StrictMode,
            target = BuildTarget.EmbeddedLinux
        };

        var report = BuildPipeline.BuildPlayer(options);

        if (report.summary.result == BuildResult.Succeeded)
        {
            Debug.Log($"Build successful - Build written to {options.locationPathName}");
        }
        else if (report.summary.result == BuildResult.Failed)
        {
            Debug.LogError($"Build failed");
        }
    }

    // This function will be called from the build process
    public static void Build()
    {
        // Build EmbeddedLinux ARM64 Unity player
        BuildEmbeddedLinux(EmbeddedLinuxArchitecture.Arm64);
    }
}

其他资源: