项目简介
Unity MCP 通过模型上下文协议将 Claude、GPT 等大语言模型接入 Unity 编辑器,提供 47 个工具用于创建场景、编辑脚本、管理资源和自动化游戏开发流程。开源采用 MIT 许可证。
README 预览
\n \n \n \n \n\n\n\n\n[English](README.md) [简体中文](docs/i18n/README-zh.md) | [Discord](https://discord.gg/y4p8KfzrN4) [Wiki](https://coplaydev.github.io/unity-mcp/)\n\n#### Proudly sponsored and maintained by [Aura](https://www.tryaura.dev/) — the AI assistant for Unreal & Unity.\n##### And don't miss [Godot AI](https://github.com/hi-godot/godot-ai), the new open source project from the makers of MCP for Unity.\n\n\n\nCreate your Unity apps with LLMs. MCP for Unity bridges AI assistants — Claude, Codex, VS Code, local LLMs, and more — with your Unity Editor via Model Context Protocol. Give your LLM the tools to manage assets, control scenes, edit scripts, run tests, and automate your game dev workflows.\n\n\n \n\n\n---\n\n\n\nRecent Updates\n\n* **[v10.0.0](https://github.com/CoplayDev/unity-mcp/releases/tag/v10.0.0)** (2026-06-30)\n* **[v9.7.3](https://github.com/CoplayDev/unity-mcp/releases/tag/v9.7.3)** (2026-06-15)\n* **[v9.7.1](https://github.com/CoplayDev/unity-mcp/releases/tag/v9.7.1)** (2026-05-24)\n* **[v9.7.0](https://github.com/CoplayDev/unity-mcp/releases/tag/v9.7.0)** (2026-05-22)\n* **[v9.6.8](https://github.com/CoplayDev/unity-mcp/releases/tag/v9.6.8)** (2026-04-27)\n\nFull history: [Release Notes](https://coplaydev.github.io/unity-mcp/releases).\n\n\n\n\n---\n\n## What it does\n\nControl the Unity Editor in natural language from any MCP client — create scenes & GameObjects, edit C# scripts, manage assets, run tests, profile, and build. 47 focused MCP tool entrypoints, any client, free & MIT.\n\n**[Browse the full tool catalog →](https://coplaydev.github.io/unity-mcp/reference/tools/)**\n\n---\n\n## Quickstart\n\n**Requirements:** Unity **2021.3 LTS → 6.x** · Python **3.10+** (via [`uv`](https://docs.astral.sh/uv/)). Works with **any MCP client** — Claude Desktop & Code, Cursor, VS Code, Windsurf, Cline, Gemini CLI, and more.\n\n1. **Install** — Unity → Package Manager → Add from git URL:\n `https://gith
常见问题 (5)
故障排除如何修复 Unity 6 中的 CS0234 错误:命名空间 "Microsoft.CodeAnalysis" 中不存在类型或命名空间名称 "Formatting"?
当启用 USE_ROSLYN 时,MCP for Unity v10.0.0 会出现此错误,因为 ManageScript.cs 使用了 Microsoft.CodeAnalysis.Formatting,但 MCPForUnity.Editor.asmdef 未引用 Roslyn Workspaces 程序集。
推荐修复方法:将 MCP for Unity 更新到包含该修复的版本,该修复移除了对 Workspaces 层格式化的依赖。
手动解决方法:打开 MCPForUnity.Editor.asmdef,并将 Microsoft.CodeAnalysis.CSharp.Workspaces.dll 添加到 precompiledReferences 中:
"precompiledReferences": [
"Newtonsoft.Json.dll",
"Microsoft.CodeAnalysis.dll",
"Microsoft.CodeAnalysis.CSharp.dll",
"Microsoft.CodeAnalysis.CSharp.Workspaces.dll",
"System.Collections.Immutable.dll",
"System.Reflection.Metadata.dll",
"System.Runtime.CompilerServices.Unsafe.dll"
]
请确保该 DLL 确实存在于项目中,然后保存 asmdef 并让 Unity 重新编译。如果一键安装程序未提供 Microsoft.CodeAnalysis.CSharp.Workspaces.dll,请使用更新后的软件包版本或禁用 USE_ROSLYN,而不是引用一个缺失的 DLL。
故障排除在 MCP for Unity 中使用 manage_components 的 set_property 设置 SpriteRenderer.sprite 时,如何修复 "SerializedProperty 'sprite' not found" 错误?
发生这种情况的原因是,MCP for Unity 使用公共 C# 属性名(sprite)查找序列化字段,但 Unity 内置的 SpriteRenderer 将其存储在原生后备字段 m_Sprite 中。对于其他序列化字段名使用 m_ 前缀的内置对象引用属性,也可能出现同样的失败。
临时解决方法:使用 execute_code 而不是 set_property 来设置属性:
var sr = gameObject.GetComponent<SpriteRenderer>();
sr.sprite = AssetDatabase.LoadAssetAtPath<Sprite>("Assets/Sprites/WhiteSquare.png");
EditorUtility.SetDirty(sr);
如果你只有 GUID:
var path = AssetDatabase.GUIDToAssetPath("YOUR-ASSET-GUID");
var sr = gameObject.GetComponent<SpriteRenderer>();
sr.sprite = AssetDatabase.LoadAssetAtPath<Sprite>(path);
EditorUtility.SetDirty(sr);
永久性代码修复:在 MCPForUnity/Editor/Helpers/ComponentOps.cs 中,更新 SetViaSerializedProperty 的查找逻辑,使其回退到 Unity 原生的 m_ 字段命名约定:
SerializedProperty prop = so.FindProperty(propertyName)
?? so.FindProperty(normalizedName)
?? so.FindProperty("m_" + char.ToUpperInvariant(propertyName[0]) + propertyName.Substring(1));
更稳健的修复方法是遍历 SerializedObject 的顶层属性,并在去除 m_ 前缀后以不区分大小写的方式匹配所请求的名称。该问题已在 MCP for Unity main 分支及 Unity 6000.3.13f1 上报告。
故障排除如何修复在 Unity 6000.6 上读取 MCP for Unity GameObject 资源时 EditorUtility.InstanceIDToObject 抛出的 "NotImplementedException"?
将 com.coplaydev.unity-mcp 升级到 10.3.0 或更高版本;该修复已在最新的 beta/main 分支中提供。Unity 6000.6 移除了 EditorUtility.InstanceIDToObject(int) 的实现,导致 MCP for Unity 10.2.0 在调用 mcpforunity://scene/gameobject/{id} 或 .../components 时失败。作为临时解决方案,请避免使用 GameObject 资源,改为通过 execute_code 遍历活动场景来解析对象:
var roots = UnityEngine.SceneManagement.SceneManager.GetActiveScene().GetRootGameObjects();
var all = roots.SelectMany(r => r.GetComponentsInChildren<Transform>(true)).Select(t => t.gameObject);
var target = all.FirstOrDefault(go => go.GetInstanceID() == instanceId);
使用返回的对象来检查组件,而不是使用已失效的 instance-ID 资源。
故障排除如何修复 MCP for Unity 在从带有 SplineContainer knots 或 Unity.Mathematics 结构体的 GameObject 读取组件时永久挂起的问题?
此问题会影响 MCP for Unity 10.1.2、10.2.0 以及 issue 中提到的 beta 版本,触发条件为使用 Unity 6000.0.73f1、com.unity.splines 2.8.4、com.unity.mathematics 1.3.2 和 Newtonsoft Json 3.2.2。编辑器卡死的原因是 Newtonsoft.Json 会序列化 Unity.Mathematics 类型(如 float2、float3、float4 和 quaternion)上的公共 swizzle 属性。每个 swizzle 会返回另一个带有更多 swizzle 的结构体,从而形成组合式的序列化循环。空的样条曲线可以正常返回,因为它们不暴露 BezierKnot 值。
修复方法是为 Unity.Mathematics 命名空间中的值类型添加一个 JsonConverter,使其仅序列化公共实例字段,例如将 float3 序列化为 {x,y,z},将 quaternion 序列化为 {value}。在 GameObjectSerializer._outputSerializer.Converters 中注册该转换器,最好将其插入到索引 0 的位置。等效的修复方案还包括:使用一个跳过标记了 [EditorBrowsable(EditorBrowsableState.Never)] 的属性的契约解析器(contract resolver),或者在 GetComponentData 中为每个组件设置 token 上限,这样一个失控的组件只会失败,而不会冻结 Unity。
临时解决方法:在加载时,使用反射将仅序列化字段的 Unity.Mathematics 转换器插入 GameObjectSerializer._outputSerializer.Converters。在补丁发布之前,请避免对包含非空 SplineContainer 组件或其他暴露 float2/float3/float4/quaternion 字段的组件的 GameObject 调用 mcpforunity://scene/gameobject/{id}/components。如果编辑器失去响应,30 秒的桥接超时并不会释放 Unity 主线程,因此必须重启 Unity。一个标题为 "fix: serialize Unity.Mathematics structs by their fields, not their swizzles" 的 PR 提供了上游修复;请在修复版本或发布版本可用后使用。
故障排除如何修复 Unity MCP read_console 在存在 Debug.Log 消息时返回 0 个条目的问题?
read_console 使用 Unity 内部的 LogEntries.StartGettingEntries() / GetEntryInternal() API,这些 API 会继承 Console 窗口工具栏的严重级别过滤器和搜索文本。如果 Log/Warning/Error 切换按钮处于关闭状态,即使 success 为 true 且消息确实存在于 Editor.log 中,API 也会返回零条记录。
**临时解决方法**
1. 打开 Unity 的 **Console** 窗口。
2. 在工具栏中启用 **Log**、**Warning** 和 **Error** 切换按钮。
3. 清空 Console 的搜索/筛选框。
4. 再次调用 read_console。
你也可以通过 Editor C# 代码(例如通过 execute_code)重新启用严重级别标志:
using UnityEditor;
using System.Reflection;
var logEntriesType = typeof(Editor).Assembly.GetType("UnityEditor.LogEntries");
var setFlag = logEntriesType.GetMethod(
"SetConsoleFlag",
BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic
);
setFlag.Invoke(null, new object[] { 1 << 7, true }); // Log
setFlag.Invoke(null, new object[] { 1 << 8, true }); // Warning
setFlag.Invoke(null, new object[] { 1 << 9, true }); // Error
var setFilter = logEntriesType.GetMethod(
"SetFilteringText",
BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic
);
setFilter?.Invoke(null, new object[] { "" });
然后使用 types: ["log"] 或 types: ["all"] 重试 read_console。
**永久修复:** 修改 ReadConsole.GetConsoleEntries,保存当前的 consoleFlags 和筛选文本,在遍历期间临时强制开启 LogLevelLog(1 << 7)、LogLevelWarning(1 << 8)和 LogLevelError(1 << 9)并清空搜索筛选条件,然后恢复原始状态。一种侵入性较小的方案是:当请求的严重级别被屏蔽时,返回类似 "note": "Console window filters are hiding log/warning entries" 的警告。
该问题在 MCP for Unity v10.0.0 和 Unity 6000.3.9f1 中观察到。