unity-compile-fixer

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Unity 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
mcp__ide__getDiagnostics
tool to collect errors from VSCode:
typescript
// 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
    severity: "Error"
    (ignore warnings)
  • Source: Only process
    source: "csharp"
    (OmniSharp C# diagnostics)
  • Error Codes: Focus on CS* compiler error codes (e.g., CS0246, CS0029, CS1061)
使用
mcp__ide__getDiagnostics
工具从VSCode收集错误:
typescript
// 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"
    级别的错误(忽略警告)
  • 来源:仅处理
    source: "csharp"
    的错误(OmniSharp C#诊断信息)
  • 错误代码:重点关注CS*编译器错误代码(如CS0246、CS0029、CS1061)

2. Analyze Error Patterns

2. 分析错误模式

For each detected error:
  1. Extract error information:
    • File path and line number from
      uri
      and
      range
    • Error code from
      message
      (e.g., "CS0246")
    • Full error message text
  2. Match against error pattern database:
    • Load
      references/error-patterns.json
    • Find the error code entry (e.g., CS0246)
    • Retrieve common causes and solutions
  3. 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
针对每个检测到的错误:
  1. 提取错误信息
    • uri
      range
      中获取文件路径和行号
    • message
      中提取错误代码(如"CS0246")
    • 获取完整的错误消息文本
  2. 匹配错误模式数据库
    • 加载
      references/error-patterns.json
      文件
    • 查找对应的错误代码条目(如CS0246)
    • 获取常见原因和解决方案
  3. 读取受影响文件的上下文
    • 使用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 1

4. User Confirmation

4. 用户确认

Before applying any fixes:
  1. Present all proposed solutions in a clear, structured format
  2. Use AskUserQuestion tool to get user approval:
    • List each error and proposed fix
    • Allow user to approve all, select specific fixes, or cancel
  3. Wait for explicit confirmation - do not apply fixes automatically
在应用任何修复前:
  1. 以清晰的结构化格式展示所有建议的解决方案
  2. 使用AskUserQuestion工具获取用户批准
    • 列出每个错误及对应的修复建议
    • 允许用户批准全部修复、选择特定修复或取消操作
  3. 等待用户明确确认 - 不得自动应用修复

5. Apply Approved Fixes

5. 应用已批准的修复

For each approved fix:
  1. Use Edit tool to modify the affected file
  2. Preserve code formatting and existing structure
  3. 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"
})
针对每个已批准的修复:
  1. 使用Edit工具修改受影响的文件
  2. 保留代码格式和现有结构
  3. 应用最小化修改 - 仅修复特定错误
示例:
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:
  1. 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 (<<<<<<, ======, >>>>>>)
  2. Report VCS status:
    • List modified files
    • Warn about any .meta file issues
    • Suggest git operations if needed
应用修复后:
  1. 检查.meta文件冲突
    • 使用Grep搜索Unity .meta文件
    • 验证脚本GUID未发生变化
    • 检查是否存在合并冲突标记(<<<<<<, ======, >>>>>>)
  2. 报告版本控制系统状态
    • 列出已修改的文件
    • 警告任何.meta文件问题
    • 如有需要,建议执行Git操作

7. Re-validate Compilation

7. 重新验证编译状态

After fixes are applied:
  1. Re-run diagnostics using
    mcp__ide__getDiagnostics()
  2. Compare error count before and after
  3. Report results to the user:
    • Number of errors fixed
    • Remaining errors (if any)
    • Success rate
应用修复后:
  1. 重新运行诊断,使用
    mcp__ide__getDiagnostics()
  2. 对比修复前后的错误数量
  3. 向用户报告结果
    • 已修复的错误数量
    • 剩余错误(如有)
    • 修复成功率

Error Pattern Database

错误模式数据库

The skill relies on
references/error-patterns.json
for error analysis. This database contains:
  • 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 entry

Analysis Scripts

分析脚本

The skill includes Node.js scripts in
scripts/
for complex error analysis:
本技能在
scripts/
目录下包含用于复杂错误分析的Node.js脚本:

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:
  1. Start with full project diagnostics - use
    mcp__ide__getDiagnostics()
    without parameters to get complete error picture
  2. Prioritize errors by severity and dependency - fix foundational errors (missing imports) before downstream errors
  3. Batch related fixes - group errors from the same file for efficient editing
  4. Always verify VCS status - Unity .meta files are critical for version control
  5. Re-validate after fixes - ensure errors are actually resolved
使用本技能时,请遵循以下最佳实践:
  1. 先获取完整的项目诊断信息 - 不带参数使用
    mcp__ide__getDiagnostics()
    ,以获取完整的错误情况
  2. 按严重程度和依赖关系优先处理错误 - 先修复基础错误(如缺失导入语句),再处理下游错误
  3. 批量处理相关修复 - 将同一文件中的错误分组,以提高编辑效率
  4. 始终验证版本控制系统状态 - Unity .meta文件对版本控制至关重要
  5. 修复后重新验证 - 确保错误已实际解决

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特定指导。