unity-compile-fixer
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseUnity Compile Fixer
Unity编译修复工具
Overview
概述
This skill enables automatic detection and resolution of Unity C# compilation errors by leveraging VSCode's diagnostic system. It collects real-time errors from the OmniSharp C# language server, analyzes error patterns against a curated database of common Unity issues, and proposes context-aware solutions for user approval before applying fixes.
本技能借助VSCode的诊断系统,实现Unity C#编译错误的自动检测与修复。它从OmniSharp C#语言服务器收集实时错误,将错误模式与精心整理的常见Unity问题数据库进行比对,并在应用修复前提出上下文感知的解决方案供用户确认。
When to Use This Skill
适用场景
Use this skill when:
- Unity projects report C# compilation errors in VSCode
- Need to diagnose the root cause of Unity compiler errors (CS* error codes)
- Want automated fix suggestions for common Unity scripting issues
- Working with Unity projects that have version control integration (Git, Unity Collaborate, Plastic SCM)
- Need to handle Unity .meta file conflicts
Example user requests:
- "Check Unity compilation errors and help me fix them"
- "My Unity project has compiler errors, can you diagnose and fix?"
- "Unity scripts are not compiling, what's wrong?"
- "Fix the C# errors in my Unity project"
在以下场景中使用本技能:
- Unity项目在VSCode中报出C#编译错误时
- 需要诊断Unity编译器错误(CS*错误代码)的根本原因时
- 希望针对常见Unity脚本问题获取自动修复建议时
- 处理带有版本控制集成的Unity项目(Git、Unity Collaborate、Plastic SCM)时
- 需要处理Unity .meta文件冲突时
用户请求示例:
- "检查Unity编译错误并帮我修复"
- "我的Unity项目有编译器错误,能帮忙诊断并修复吗?"
- "Unity脚本无法编译,出什么问题了?"
- "修复我Unity项目中的C#错误"
Workflow
工作流
Follow this workflow when the skill is invoked:
调用本技能时,请遵循以下工作流:
1. Detect Compilation Errors
1. 检测编译错误
Use the tool to collect errors from VSCode:
mcp__ide__getDiagnosticstypescript
// Collect all project diagnostics
mcp__ide__getDiagnostics()
// Or target specific Unity script files
mcp__ide__getDiagnostics({ uri: "file:///path/to/PlayerController.cs" })Filter the diagnostics to focus on Unity-relevant errors:
- Severity: Only process errors with (ignore warnings)
severity: "Error" - Source: Only process (OmniSharp C# diagnostics)
source: "csharp" - Error Codes: Focus on CS* compiler error codes (e.g., CS0246, CS0029, CS1061)
使用工具从VSCode收集错误:
mcp__ide__getDiagnosticstypescript
// Collect all project diagnostics
mcp__ide__getDiagnostics()
// Or target specific Unity script files
mcp__ide__getDiagnostics({ uri: "file:///path/to/PlayerController.cs" })筛选诊断信息,聚焦与Unity相关的错误:
- 严重程度:仅处理级别的错误(忽略警告)
severity: "Error" - 来源:仅处理的错误(OmniSharp C#诊断信息)
source: "csharp" - 错误代码:重点关注CS*编译器错误代码(如CS0246、CS0029、CS1061)
2. Analyze Error Patterns
2. 分析错误模式
For each detected error:
-
Extract error information:
- File path and line number from and
urirange - Error code from (e.g., "CS0246")
message - Full error message text
- File path and line number from
-
Match against error pattern database:
- Load
references/error-patterns.json - Find the error code entry (e.g., CS0246)
- Retrieve common causes and solutions
- Load
-
Read affected file context:
- Use Read tool to load the file with errors
- Examine surrounding code for context
- Identify missing imports, incorrect types, or API misuse
针对每个检测到的错误:
-
提取错误信息:
- 从和
uri中获取文件路径和行号range - 从中提取错误代码(如"CS0246")
message - 获取完整的错误消息文本
- 从
-
匹配错误模式数据库:
- 加载文件
references/error-patterns.json - 查找对应的错误代码条目(如CS0246)
- 获取常见原因和解决方案
- 加载
-
读取受影响文件的上下文:
- 使用Read工具加载存在错误的文件
- 检查周边代码以获取上下文信息
- 识别缺失的导入语句、错误的类型或API误用情况
3. Generate Solution Proposals
3. 生成解决方案建议
For each error, create a structured fix proposal:
markdown
**Error**: CS0246 at PlayerController.cs:45
**Message**: The type or namespace name 'Rigidbody' could not be found
**Analysis**:
- Missing using directive for UnityEngine namespace
- Common Unity API usage pattern
**Proposed Solution**:
Add `using UnityEngine;` at the top of PlayerController.cs
**Changes Required**:
- File: Assets/Scripts/PlayerController.cs
- Action: Insert using directive at line 1针对每个错误,创建结构化的修复建议:
markdown
**Error**: CS0246 at PlayerController.cs:45
**Message**: The type or namespace name 'Rigidbody' could not be found
**Analysis**:
- Missing using directive for UnityEngine namespace
- Common Unity API usage pattern
**Proposed Solution**:
Add `using UnityEngine;` at the top of PlayerController.cs
**Changes Required**:
- File: Assets/Scripts/PlayerController.cs
- Action: Insert using directive at line 14. User Confirmation
4. 用户确认
Before applying any fixes:
-
Present all proposed solutions in a clear, structured format
-
Use AskUserQuestion tool to get user approval:
- List each error and proposed fix
- Allow user to approve all, select specific fixes, or cancel
-
Wait for explicit confirmation - do not apply fixes automatically
在应用任何修复前:
-
以清晰的结构化格式展示所有建议的解决方案
-
使用AskUserQuestion工具获取用户批准:
- 列出每个错误及对应的修复建议
- 允许用户批准全部修复、选择特定修复或取消操作
-
等待用户明确确认 - 不得自动应用修复
5. Apply Approved Fixes
5. 应用已批准的修复
For each approved fix:
- Use Edit tool to modify the affected file
- Preserve code formatting and existing structure
- Apply minimal changes - only fix the specific error
Example:
typescript
Edit({
file_path: "Assets/Scripts/PlayerController.cs",
old_string: "public class PlayerController : MonoBehaviour",
new_string: "using UnityEngine;\n\npublic class PlayerController : MonoBehaviour"
})针对每个已批准的修复:
- 使用Edit工具修改受影响的文件
- 保留代码格式和现有结构
- 应用最小化修改 - 仅修复特定错误
示例:
typescript
Edit({
file_path: "Assets/Scripts/PlayerController.cs",
old_string: "public class PlayerController : MonoBehaviour",
new_string: "using UnityEngine;\n\npublic class PlayerController : MonoBehaviour"
})6. Verify Version Control Status
6. 验证版本控制状态
After applying fixes:
-
Check for .meta file conflicts:
- Use Grep to search for Unity .meta files
- Verify that script GUID hasn't changed
- Check for merge conflict markers (<<<<<<, ======, >>>>>>)
-
Report VCS status:
- List modified files
- Warn about any .meta file issues
- Suggest git operations if needed
应用修复后:
-
检查.meta文件冲突:
- 使用Grep搜索Unity .meta文件
- 验证脚本GUID未发生变化
- 检查是否存在合并冲突标记(<<<<<<, ======, >>>>>>)
-
报告版本控制系统状态:
- 列出已修改的文件
- 警告任何.meta文件问题
- 如有需要,建议执行Git操作
7. Re-validate Compilation
7. 重新验证编译状态
After fixes are applied:
- Re-run diagnostics using
mcp__ide__getDiagnostics() - Compare error count before and after
- Report results to the user:
- Number of errors fixed
- Remaining errors (if any)
- Success rate
应用修复后:
- 重新运行诊断,使用
mcp__ide__getDiagnostics() - 对比修复前后的错误数量
- 向用户报告结果:
- 已修复的错误数量
- 剩余错误(如有)
- 修复成功率
Error Pattern Database
错误模式数据库
The skill relies on for error analysis. This database contains:
references/error-patterns.json- Error Code: CS* compiler error code
- Description: Human-readable explanation
- Common Causes: Typical reasons this error occurs in Unity
- Solutions: Step-by-step fix instructions
- Unity-Specific Notes: Unity API considerations
To analyze an error, load the database and match the error code:
typescript
Read({ file_path: "references/error-patterns.json" })
// Parse JSON and find errorCode entry本技能依赖进行错误分析。该数据库包含以下内容:
references/error-patterns.json- 错误代码:CS*编译器错误代码
- 描述:通俗易懂的解释
- 常见原因:Unity项目中出现该错误的典型原因
- 解决方案:分步修复说明
- Unity特定说明:Unity API相关注意事项
要分析错误,需加载数据库并匹配错误代码:
typescript
Read({ file_path: "references/error-patterns.json" })
// Parse JSON and find errorCode entryAnalysis Scripts
分析脚本
The skill includes Node.js scripts in for complex error analysis:
scripts/本技能在目录下包含用于复杂错误分析的Node.js脚本:
scripts/scripts/analyze-diagnostics.js
scripts/analyze-diagnostics.js
Processes VSCode diagnostics JSON output and extracts Unity-relevant errors.
Usage:
bash
node scripts/analyze-diagnostics.js <diagnostics-json-file>Output:
- Filtered list of Unity C# compilation errors
- Error classification by type (missing imports, type errors, API issues)
- Severity and file location information
This script can be run independently or invoked from the SKILL.md workflow when detailed analysis is needed.
处理VSCode诊断JSON输出,提取与Unity相关的错误。
使用方法:
bash
node scripts/analyze-diagnostics.js <diagnostics-json-file>输出内容:
- 筛选后的Unity C#编译错误列表
- 按类型分类的错误(缺失导入语句、类型错误、API问题等)
- 错误严重程度和文件位置信息
该脚本可独立运行,或在需要详细分析时从SKILL.md的工作流中调用。
Best Practices
最佳实践
When using this skill:
- Start with full project diagnostics - use without parameters to get complete error picture
mcp__ide__getDiagnostics() - Prioritize errors by severity and dependency - fix foundational errors (missing imports) before downstream errors
- Batch related fixes - group errors from the same file for efficient editing
- Always verify VCS status - Unity .meta files are critical for version control
- Re-validate after fixes - ensure errors are actually resolved
使用本技能时,请遵循以下最佳实践:
- 先获取完整的项目诊断信息 - 不带参数使用,以获取完整的错误情况
mcp__ide__getDiagnostics() - 按严重程度和依赖关系优先处理错误 - 先修复基础错误(如缺失导入语句),再处理下游错误
- 批量处理相关修复 - 将同一文件中的错误分组,以提高编辑效率
- 始终验证版本控制系统状态 - Unity .meta文件对版本控制至关重要
- 修复后重新验证 - 确保错误已实际解决
Resources
资源
scripts/analyze-diagnostics.js
scripts/analyze-diagnostics.js
Node.js script for processing VSCode diagnostics and filtering Unity-specific C# errors.
用于处理VSCode诊断信息并筛选Unity特定C#错误的Node.js脚本。
references/error-patterns.json
references/error-patterns.json
Curated database of common Unity C# compilation errors with solutions and Unity-specific guidance.
精心整理的常见Unity C#编译错误数据库,包含解决方案和Unity特定指导。