别再手动点 Unity 了:Unity CLI 从安装到 CI 的完整指南
写给 Unity 开发者的命令行自动化入门
如果你每天的工作流是:打开 Hub、等 Editor、点菜单、切窗口看 Console、再手动打包——这篇文章就是写给你的。
Unity CLI 1.0 正式发布后,Unity 开发终于有了一套官方、统一的命令行入口。它不能替代 Editor,但能把 Editor 的操作写成脚本、接进自动化流程,也能被 AI Agent 调用。
版本说明:本文基于 Unity CLI 1.0.x(2026 年 11 月发布)与 Unity Pipeline 1.0.x。Editor 版本以项目
ProjectVersion.txt为准。

先说结论
- Unity CLI 是 Editor 的“遥控器”,不是 Editor 或游戏引擎的替代品。
- 项目仍然需要安装匹配版本的 Unity Editor,并具备有效许可证。
- Unity Hub 不是必须的;CLI 可以安装 Editor、管理项目和许可证,Hub 更适合图形化管理。
- 想驱动正在运行的 Editor,需要给项目安装
com.unity.pipeline。 - CLI 的用处在于自动化、可重复的环境、结构化输出、CI 和 AI Agent 集成。
- 跟UnityMCP相比,CLI对AI Agent更加便捷操作,少了一步中间交互层。
如果你只做纯手动开发,CLI 不是必需品。但只要你想减少重复操作、接 CI、让 Agent 参与开发,它值得花半小时入门。
01 先搞懂 Unity CLI 是什么
可以先记住这张关系图:
Unity CLI
├─ 管理 Editor、模块、许可证、项目
├─ 启动 GUI Editor 或 batch Editor
├─ 运行测试、构建、项目检查
└─ 通过 com.unity.pipeline 驱动运行中的 Editor
三者的分工也很简单:
- Unity Hub:图形化启动器,适合装 Editor、管理模块、登录和看项目。
- Unity Editor:真正编译 C#、导入资源、运行游戏和打包的程序。
- Unity CLI:用命令完成 Hub 和 Editor 的部分操作,并提供 JSON 输出、退出码和 Agent 集成。

CLI 不能替代 GUI。
关卡搭建、灯光、动画、材质调参、Shader Graph、复杂 UI 布局,仍然更适合在 Editor 里完成。CLI 擅长的是重复操作、状态查询、批量处理、测试、构建和自动化。
02 安装:一个二进制,三种平台
Windows 在 PowerShell 里执行:
irm https://unity.com/install.ps1 | iex
macOS / Linux:
curl -fsSL https://unity.com/install.sh | bash
安装后打开一个新终端,确认版本:
unity --version
如果命令找不到,先检查安装目录是否在 PATH 中。Windows 常见位置是:
%LOCALAPPDATA%\Unity\bin
也可以运行诊断:
unity doctor
unity env
更新和卸载:
unity self-update --check
unity self-update
unity self-uninstall
较新的 Unity Hub 也可能自带 CLI。无论通过 Hub 还是独立安装脚本安装,最终使用的都是同一个 unity 命令。
03 第一次配置:别急着敲命令
安装 CLI 只是第一步。要打开、测试和构建项目,还需要 Editor 和许可证。
登录与许可证
unity auth login
unity auth status
unity license status
如果需要激活许可证:
unity license activate
个人许可证也可以显式激活:
unity license activate --personal --accept-eula
也可以继续用 Unity Hub 完成登录和许可证管理。CLI 和 Hub 管理的是同一套 Unity 账号与许可证体系。
安装指定版本的 Editor
项目版本由 ProjectSettings/ProjectVersion.txt 决定。先查看可用版本:
unity releases --lts
unity install 6000.0.59f2
需要 Android 构建时:
unity install 6000.0.59f2 -m android -m android-sdk-ndk-tools --accept-eula
模块 ID 可以查询:
unity install 6000.0.59f2 --list-modules
打开项目
unity projects info /path/to/MyGame
unity projects require /path/to/MyGame
unity open /path/to/MyGame --until-ready --timeout 300
projects require 会检查项目需要的 Editor 是否已安装。unity open 会读取项目版本并启动对应 Editor。
安装 Pipeline 包
如果希望 CLI 驱动正在运行的 Editor,例如读取场景、创建 GameObject、执行编译检查、进入 Play Mode,需要安装 Pipeline 包:
unity pipeline install --project-path /path/to/MyGame
com.unity.pipeline 面向 Unity 6.0 及以上版本。这个操作会修改项目的 Packages/manifest.json 和 Packages/packages-lock.json,记得纳入版本控制并单独 review。
安装后验证:
unity pipeline editors --project-path /path/to/MyGame
unity status --project-path /path/to/MyGame --format json
04 命令地图:先学会找命令
Unity CLI 命令很多,但不需要背。先记住入口:
| 目标 | 入口 |
|---|---|
| Editor 与模块 | unity editors、unity install、unity modules |
| 项目 | unity projects、unity open、unity close |
| 运行中的 Editor | unity status、unity command、unity recompile |
| 测试与构建 | unity test、unity build |
| 账号与许可证 | unity auth、unity license |
| 诊断与文档 | unity doctor、unity logs、unity docs |
需要某条命令时,用 CLI 自带的搜索:
unity commands --grep scene
unity command --query scene
unity command scene
unity command 会列出当前 Editor 实际支持的命令和参数,比凭印象拼命令可靠得多。
05 日常开发:建立 warm Editor 循环
推荐的本地开发循环:
- 打开项目,确认 Editor 就绪。
- 修改 C# 源码。
- 执行编译检查。
- 读取 Console 错误。
- 通过 Pipeline 命令操作场景、Prefab 或组件。
- 保存场景和资源。
- 复查 Git diff,再决定是否提交。

先确认 Editor 状态:
unity status --project-path /path/to/MyGame --format json
unity command editor status --project-path /path/to/MyGame
重点看 state 是否为 ready、compiling 是否为 false、playMode 是否为 stopped。
当 Editor 失去焦点时,Unity 可能降低刷新频率。可以开启自动 tick:
unity command editor set_auto_tick --enabled true --project-path /path/to/MyGame
编译检查:
unity recompile --project-path /path/to/MyGame --timeout 180
只需要验证 C# 是否能编译时,不要跑完整构建。unity recompile 是更快的反馈方式。
读取 Console 时,先记录游标:
unity command console status --project-path /path/to/MyGame --format json
操作后读取新增错误:
unity command console get --since <cursor> --since-session <session> --level error --project-path /path/to/MyGame --format json
如果只想看最近错误:
unity command console get --tail 50 --level error --project-path /path/to/MyGame
不要使用 console clear,它会同时清掉用户在 Editor 里的 Console 内容。
场景操作完成后记得保存:
unity command scene list_open --project-path /path/to/MyGame
unity command scene get_hierarchy --project-path /path/to/MyGame
unity command scene save --project-path /path/to/MyGame
unity command editor save_all --project-path /path/to/MyGame
editor save_all 保存所有有改动的打开场景。Prefab 的保存应遵循项目自己的流程,不要假定 CLI 会自动处理。
06 驱动运行中的 Editor
Pipeline 包安装后,CLI 可以控制当前打开的 Editor:
- 查询场景层级和 GameObject
- 创建、删除、移动对象
- 添加组件、修改序列化字段
- 打开和保存场景
- 读取 Console
- 进入和退出 Play Mode
- 执行 C# 脚本
- 截图和读取 Editor 窗口
读取当前场景:
unity command scene list_open --project-path /path/to/MyGame
unity command scene get_hierarchy --project-path /path/to/MyGame
执行一段只读 C#:
unity command script eval "return UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene().name;" --project-path /path/to/MyGame
script eval 会以当前用户权限在 Editor 中执行任意 C#,因此只应该运行可信内容。涉及修改时,先记录 Console 游标,操作后检查错误,并明确保存策略。
07 测试与构建
Editor 已打开时,在当前 Editor 中运行:
unity command test run --mode editor --filter MyNamespace.MyTests --project-path /path/to/MyGame
PlayMode 测试需要异步执行:
unity command test run --mode playmode --async-tests --project-path /path/to/MyGame
Editor 关闭时,使用 batch 模式:
unity test /path/to/MyGame --mode EditMode --report-format nunit,junit --output TestResults/editmode.xml --junit-output TestResults/editmode-junit.xml
unity test 会启动自己的 batch Editor,因此会拒绝已经在 Editor 中打开的同一工程。不要为了跑测试而强行关闭用户正在使用的 Editor;优先使用 Pipeline 的测试命令,或先确认并保存。
构建前先看项目配置:
unity build --list-targets
unity build --list-profiles --project-path /path/to/MyGame
按目标构建:
unity build /path/to/MyGame --target Android --output Builds/Android/MyGame.apk --log-file Logs/build-android.log
如果项目有自己的构建脚本:
unity build /path/to/MyGame --target Android --execute-method BuildTools.PerformAndroidBuild --output Builds/Android/MyGame.apk
08 CI/CD:让 Unity 变成可流水线工具

CI 环境不应该依赖人工点击和终端交互。建议:
- 使用
--non-interactive关闭交互提示。 - 使用
--format json或--format ndjson输出机器可读结果。 - 使用
--no-banner、--no-pager保持日志干净。 - 根据 exit code 判断结果,不要匹配本地化错误文本。
- 使用服务账号或浮动许可证,不共享个人账号。
- 把测试报告、构建日志和构建产物作为 CI artifact 上传。
常见退出码:
| 退出码 | 含义 |
|---|---|
0 | 成功 |
6 | 命令失败或无法得到有效结果 |
7 | 网络不可达 |
8 | 测试执行完成但有失败 |
12 | 没有可连接的 Editor |
14 | 超时 |
一个最小的 CI 流程:
unity projects require /path/to/MyGame
unity test /path/to/MyGame --mode EditMode --non-interactive --format json --report-format junit --output TestResults/editmode.xml
unity build /path/to/MyGame --target Android --non-interactive --format json --no-tail --output Builds/Android/MyGame.apk
CI 中要固定 Editor 版本和模块,避免“今天能构建、明天换一台机器就失败”。
09 Agent 与 Skills:让 AI 不靠猜
Agent 集成是 Unity CLI 1.0 的重点之一。
unity setup codex -y
unity setup claude
unity setup antigravity
也可以给机器上所有支持的客户端安装 skills:
unity skill install --all
unity skill sync
Skills 是 Unity 官方维护的操作指南,告诉 Agent 在特定任务中应该使用哪些 CLI/Pipeline 命令、遵循哪些流程和边界。例如:
unity-cli:Editor、项目、测试、构建unity-pipeline:驱动运行中的 Editorui-ugui、ui-uitk:UI 框架urp-postprocessing:URP 后处理optimize-audio:音频优化
Skill 不是可执行程序,也不是“一键功能”。它能让 Agent 少猜命令和流程,但最终执行仍然依赖 CLI 和 Editor。
当 Agent 能运行 shell 时,优先使用 Unity CLI;MCP 更适合不能直接运行 shell 的客户端。不建议让两套自动化通道同时修改同一个场景或 Prefab。
10 安全边界:CLI 不是“无害的小工具”
Unity CLI 可以安装 Editor、修改项目、读取文件、执行 C#、构建和发布,因此应该把它当作一个有权限的自动化身份来管理。
- 所有项目改动先进入版本控制。
- 让 Agent 在独立分支上工作,操作前提交或 stash。
- 操作结束后 review
git diff,不要盲目接受全部改动。 - 不要让 Agent 直接 push 到受保护分支。
- CI 使用专用服务账号和最小权限角色。
- 共享机器上使用完执行
unity auth logout。 - 不要把 Pipeline 的本地端口暴露到不可信网络。
- 只对可信任务使用
script eval,它执行的是任意 C#。 - 不随意升级 Editor 或 Pipeline 包;升级应作为独立任务验证。
11 常见坑
unity: command not found
打开新终端,或把 CLI 安装目录加入 PATH。
STATUS_NO_INSTANCES
没有可连接的 Editor,或项目没有安装 Pipeline 包。先打开项目,再执行:
unity pipeline install --project-path /path/to/MyGame
unity status --project-path /path/to/MyGame --until-ready
STATUS_PIPELINE_LOAD_PENDING
包已加入项目,但 Editor 还没加载 Pipeline 服务。等待编译完成,或切回 Editor 窗口后重试。
Editor 处于 Safe Mode
Pipeline 服务不会加载。先修复编译错误,再重新运行 unity status。
unity test 提示工程已打开
改用:
unity command test run --mode editor --project-path /path/to/MyGame
或者先保存并关闭交互式 Editor。
版本不匹配
不要用其他 Editor 随意打开项目。使用项目 ProjectVersion.txt 指定的版本,或明确执行升级流程。
关闭 Editor 丢改动
unity close 不负责保存。关闭前先执行:
unity command editor save_all --project-path /path/to/MyGame
或使用会保存的退出命令:
unity command editor quit --save --project-path /path/to/MyGame
12 最后:从三个命令开始
Unity CLI 没有把 Editor 搬进终端。它给 Unity 开发加了一层控制接口:可以写进脚本、接进自动化,也能被 Agent 调用。
对个人开发者,它可以减少重复操作、加快编译和测试反馈;对团队,它能把 Editor 安装、项目检查、测试和构建变成可复现的流水线;对 AI Agent,它提供了比“模拟点击”更可靠的 Unity 操作入口。
不要一上来把所有功能都用上。先跑通三个命令:
unity status
unity recompile
unity command console get --tail 50 --level error
先把“状态、编译、错误”这条反馈链路跑通,再逐步加入场景操作、测试、构建和 CI。
配图为 AI 生成的示意图,仅用于表达概念。
参考
- Unity CLI 文档:https://docs.unity.com/en-us/unity-cli
- Unity CLI 使用指南:https://docs.unity.com/en-us/unity-cli/use-unity-cli
- Unity CLI 安全建议:https://docs.unity.com/en-us/unity-cli/unity-cli-security
- 输出格式与退出码:https://docs.unity.com/en-us/unity-cli/reference/output-formats
- Pipeline 包文档:https://docs.unity.com/en-us/unity-cli/unity-pipeline/unity-pipeline-package
- Unity CLI 1.0 发布说明:https://unity.com/the-unity-cli-1.0-is-here
- Unity skills 仓库:https://github.com/Unity-Technologies/skills
- Unity agent plugin:https://github.com/Unity-Technologies/unity-agent-plugin
转载自 CSDN-专业IT技术社区
原文链接:https://blog.csdn.net/zhangay1998/article/details/171049287




