Add Unity Pipeline package support

This commit is contained in:
ud18010
2026-07-31 17:34:04 +08:00
parent d1a526094e
commit 786b7ffff9
590 changed files with 51995 additions and 2 deletions
+1
View File
@@ -0,0 +1 @@
进行 Unity 开发时,必须阅读并遵循UNITY-GUIDE.md。
+52
View File
@@ -0,0 +1,52 @@
# Unity 操作规范
本文规定 Agent 进行 Unity 开发时的工具选择、内容操作边界与验证要求。
## 1. 核心工具
- 统一通过 Unity CLI/Pipeline 与 Unity 交互,不使用 Unity MCP。
- 与 Unity 交互时必须使用 `unity-pipeline` 技能,并遵循其中的命令、参数、状态轮询和操作流程说明。
- 存在多个可连接的 Unity Editor 或开发版 Player 时,必须显式指定目标,避免误操作。
- 使用 `unity command` 查看当前实例支持的命令,不假定所有实例或 Pipeline 版本能力相同。
## 2. 操作边界
### 可直接编辑
- 普通 `.cs` 源码由 Agent 直接创建或修改。
- Shader、CSV、JSON、Markdown 等纯文本也可直接编辑。
- 涉及多个脚本或文本文件时,先批量完成修改,再统一让 Unity 刷新和编译,避免逐文件导入及重复的编译与重载。
- Pipeline 不可达时仅可进行上述纯文本编辑,不得修改序列化资源。若未能完成 Unity 编译、测试或运行验证,须明确说明。
### 必须使用 Pipeline
- 创建或修改场景、Prefab、Material、ScriptableObject、AnimationClip、AnimatorController、Timeline 等由 Unity 管理的序列化内容。
- 导入图片、模型、音频、插件、DLL 等外部资产。
- 移动、重命名、复制或删除 `Assets/` 下的既有文件和文件夹。
- 场景对象操作,包括 GameObject 的创建、删除、层级、Transform、组件及其序列化字段。
- Unity 工程设置,包括 Build Settings、Tags/Layers、Input、Quality、Graphics、Player Settings 等。
- 场景、Prefab、材质和视觉效果等项目内容,应在编辑器中制作并持久化,提供必要的可调参数。不得以运行时代码临时创建来替代资产制作。
### 禁止直接操作
- 禁止手动创建、复制、修改或删除 `.meta` 文件,尤其不得改写其中的 GUID。
- 禁止使用文件系统工具复制、移动、重命名或删除 `Assets/` 下的既有资源。
- 禁止手动修改 Unity 序列化资源文件,如 `.unity``.prefab``.mat``.asset``.controller``.anim` 等。
- 禁止手动修改 Unity 生成内容或本地状态文件,如 `Library/``Temp/``Logs/``UserSettings/``*.csproj``*.sln` 等。
- 禁止以字节方式改写二进制资产。
## 3. 操作原则
- 操作 Unity 对象前先查询并确认目标,仅执行任务所需的最小操作,完成后回读确认。
- 更改序列化字段、组件类或程序集结构时,必须同步维护相关资源的兼容与脚本绑定。
- 大范围重新导入、切换构建目标、构建 Player 或批量改写资源必须获得明确授权。
- 优先使用专用操作及其提供的类型校验、Undo、预览和确认机制。
- 仅在缺少合适专用操作时,才使用 `eval` 类命令在编辑器中执行 C# 代码。代码应限于任务所需的最小范围,并按需处理 Undo、SerializedObject、AssetDatabase、脏标记和保存流程。
## 4. 验证与收尾
- 修改代码或资源后,必须通过相关测试或最小场景验证;涉及视觉改动时,还须检查实际画面。
- 验证时区分历史 Console 日志与本轮新增错误,避免将历史错误归因于当前改动。
- Scene、Prefab 编辑和 Play Mode 等临时状态结束后,必须按意图保存或放弃修改,并恢复 Editor 状态。
+8
View File
@@ -0,0 +1,8 @@
---
name: default
description: Project default instructions for Unity development. Use whenever working on Unity code, Unity assets, Unity packages, Unity Editor automation, build configuration, tests, or any other Unity project changes in this repository.
---
# Default
进行 Unity 开发时,必须阅读并遵循 `.codex\UNITY-GUIDE.md`
+4
View File
@@ -0,0 +1,4 @@
interface:
display_name: "Default"
short_description: "Read and follow the project Unity guide for Unity development."
default_prompt: "Follow the project Unity development guide."
+116
View File
@@ -0,0 +1,116 @@
---
name: unity-pipeline
description: Drive a running Unity Editor or development Player from the command line via the unity-pipeline package — install the package, keep the editor ticking while unfocused, run the edit→recompile→run_tests loop, evaluate C#, and hot-reload files at runtime. Use when an agent needs to control a live Unity instance, automate Unity tests, recompile scripts headlessly, or apply runtime hot reloads. Assumes the `unity` CLI is on PATH.
---
# Unity Pipeline (agent control)
Invoke commands with `unity command <name> [args]`. Run `unity command` with no name to
list what an instance exposes. Two servers exist: **Editor** (`7800-7849`, auto-starts with
the editor) and **Runtime** (`7900-7949`, only in a dev Player build). See the package
`README.md` "Commands" reference for the full list and parameters.
## 1. Install & verify
```bash
unity pipeline install # install into current project (or --project-path)
unity pipeline list # confirm the editor instance + server are reachable
unity command editor_status # confirm the editor server answers
```
## 2. Autonomous edit loop (Editor)
This is the core agent workflow: keep the editor alive, change code, recompile, test.
```bash
# 1. Keep the editor ticking even when unfocused/minimized. REQUIRED before headless work —
# Unity otherwise throttles or stalls update/compile when it isn't the active app.
unity command set_autotick --enable true
# 2. Edit C# source files on disk normally.
# 3. Recompile (async: triggers a domain reload, then poll until done).
unity command recompile
unity command recompile_status # repeat until "completed" or "up_to_date"
# Tolerate connection errors while the domain reload is in flight — that is expected.
# If recompile_status reports failed=true, read its "errors" array and fix before testing.
# 4. (Optional) List available tests without running them.
unity command list_tests --mode editor # mode: all | editor | playmode
# 5. Run tests (filter to keep it fast).
unity command run_tests --mode editor --filter MyFixture.MyTest
```
`run_tests` modes: `all` | `editor` | `playmode`. `filter_type`: `testName` | `assembly` |
`category`. For long runs use `--async_tests true` and poll `unity command test_status`
(abort with `unity command cancel_tests`).
> **Known caveat:** when any selected test *fails*, `run_tests` may surface an opaque
> result instead of the failure details. Re-run a narrower `--filter`, or inspect the
> editor's Test Runner / logs to get the real failure.
## 3. Runtime hot reload
Change gameplay code in a **running** game with no domain reload. The game must be live:
enter Editor Play Mode (`unity command editor_play`) or run a dev Player. A
`RuntimePipelineManager` in the scene auto-discovers tagged methods on `Awake` (no manual
registration). Mono only — Editor Play Mode and Mono desktop dev builds, not IL2CPP. The token
is auto-injected for local requests.
### Prefer: in-place — `reload_file`
Edit the method body directly; no separate file, no boilerplate.
1. Tag the method `[HotReload]` on the MonoBehaviour.
2. Edit its body on disk.
3. Apply (re-run to iterate):
```bash
unity command reload_file --filename Assets/Spinner.cs
```
Add `--pdb` to make it debuggable — emits a portable PDB mapped to your source so breakpoints in
the original file bind (attach the IDE + enable Editor Attaching; compiles unoptimized):
```bash
unity command reload_file --filename Assets/Spinner.cs --pdb
```
Constraints: `void` instance methods only; **public** members only; debugging requires `--pdb`
(the default emits no symbols).
### Alternative: with helper — `reload_file_override`
Keep the original method; put the tweak in a **separate** override file.
1. Tag the method `[HotReloadWithOverrides]` and route it via
`HotReloadHelper.ExecuteWithHotReload(this, "Update", OriginalUpdate)` (original body in `OriginalUpdate`).
2. In a **separate file that does not redeclare the target type**, add a `public static`
method tagged `[HotReloadOverrideMethod("BossController.Update")]` taking the instance first.
3. Apply: `unity command reload_file_override --filename Assets/HotReload/BossOverrides.cs`
Constraints: **public** members only; you cannot break into the override.
`unity command hotreload_status` shows active overrides. `reload_file_override*` options: `--timeout <ms>`
(default 30000), `--assemblyDir <dir>` (persist DLLs instead of in-memory);
`cleanup_hotreload --assemblyDir <dir>` clears old DLLs. Both commands validate the file up front
and return a clear error on misconfiguration (no override found, target type redeclared, bad signature).
## 4. Quick C# eval (Runtime)
```bash
unity command eval "return 2 + 2;"
# Or evaluate a .cs file on disk (contents run through the same path):
unity command eval_file Assets/Scratch.cs
```
## Gotchas
- **`set_autotick` first.** Without it, recompile and tests can hang while the editor is
unfocused. The package's watchdog relies on the tick loop staying alive.
- **Hot reload needs the game running.** `reload_file_override` / `reload_file` apply to a live
game — enter Editor Play Mode (`unity command editor_play`) first, or run a dev Player.
- **Player-only commands need a dev Player.** `log`, `set_timescale`, `runtime_status`, etc. hit
the Runtime server, which does not run in the Editor.
- **Async commands poll.** `recompile`→`recompile_status`, `run_tests --async_tests`→
`test_status`. Never assume completion from the trigger call's response.
- **Target a specific instance** with `--instance host:port` or `--project-path <path>`
when more than one is running.