【AI编码】【MCP】提供MCP工具

main
刘涛 2026-02-04 15:25:57 +08:00
parent a16f919c85
commit 219b396acf
6 changed files with 3333 additions and 0 deletions

View File

@ -25,6 +25,24 @@ alwaysApply: true
- Use DebugUtil class for logging and debugging. - Use DebugUtil class for logging and debugging.
- Utilize Unity's profiler and frame debugger to identify and resolve performance issues. - Utilize Unity's profiler and frame debugger to identify and resolve performance issues.
## UI Prefab 信息获取
当需要了解 UI Prefab 的结构和组件信息时,按以下步骤操作:
1. 通过 Unity MCP 调用 `extract_ui_prefab` 工具:
```
execute_custom_tool: { tool_name: "extract_ui_prefab", parameters: { prefab_path: "Assets/xxx/xxx.prefab" } }
```
2. 读取返回的 `json_path` 对应的 JSON 文件
3. 参考 `ExtractedPrefabs/README.md` 理解 JSON 格式含义
4. 基于 JSON 内容分析 UI 层级结构、组件类型和属性
**重要**:每次都需要先调用工具生成 JSON避免读取旧数据
**适用场景**
- 需要理解某个 UI 界面的布局结构
- 需要了解 UI 组件的具体属性配置
- 需要分析或修改 UI Prefab 相关代码
- You can @ files here - You can @ files here
- You can use markdown but dont have to - You can use markdown but dont have to

View File

@ -0,0 +1,74 @@
using System.Threading.Tasks;
using MCPForUnity.Editor.Tools;
using Newtonsoft.Json.Linq;
using UnityEditor;
using UnityEngine;
namespace UIInfoBuilder
{
/// <summary>
/// MCP 工具:提取 UI Prefab 信息并生成 JSON 文件
/// AI 可通过 execute_custom_tool 调用此工具
/// </summary>
[McpForUnityTool("extract_ui_prefab", Description = "提取 UI Prefab 信息并生成 JSON 文件,用于 AI 理解 UI 结构")]
public static class ExtractUIPrefabTool
{
public static Task<object> HandleCommand(JObject @params)
{
string prefabPath = @params?["prefab_path"]?.ToString();
// 验证参数
if (string.IsNullOrEmpty(prefabPath))
{
return Task.FromResult<object>(new
{
success = false,
error = "prefab_path 参数不能为空",
hint = "请提供 Prefab 资源路径,如 Assets/Art_Out/UI/Prefab/xxx.prefab"
});
}
// 加载 Prefab
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
if (prefab == null)
{
return Task.FromResult<object>(new
{
success = false,
error = $"无法加载 Prefab: {prefabPath}",
hint = "请确认路径正确且文件存在"
});
}
try
{
// 生成 JSON 文件路径
string jsonPath = Application.dataPath + "/../ExtractedPrefabs/" + prefab.name + ".json";
// 提取 Prefab 信息
UIInfoExtractor.ExtractPrefab(prefab, jsonPath);
// 返回相对路径,便于 AI 读取
string relativePath = "ExtractedPrefabs/" + prefab.name + ".json";
return Task.FromResult<object>(new
{
success = true,
message = $"成功提取 Prefab 信息: {prefab.name}",
json_path = relativePath,
absolute_path = jsonPath,
hint = $"请使用 Read 工具读取 {relativePath} 文件内容,参考 ExtractedPrefabs/README.md 理解 JSON 格式"
});
}
catch (System.Exception ex)
{
return Task.FromResult<object>(new
{
success = false,
error = $"提取失败: {ex.Message}",
hint = "请检查 Prefab 是否有效"
});
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 623d14440b5717542b6b9fb146a88590
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: a23ebeadfc166044487f20f80ab5f341
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@ -5,6 +5,7 @@
"com.code-philosophy.obfuz4hybridclr": "http://1.14.122.170:3000/PHXH/obfuz4hybridclr.git", "com.code-philosophy.obfuz4hybridclr": "http://1.14.122.170:3000/PHXH/obfuz4hybridclr.git",
"com.coffee.ui-particle": "https://github.com/mob-sakai/ParticleEffectForUGUI.git", "com.coffee.ui-particle": "https://github.com/mob-sakai/ParticleEffectForUGUI.git",
"com.coffee.unmask": "https://github.com/mob-sakai/UnmaskForUGUI.git", "com.coffee.unmask": "https://github.com/mob-sakai/UnmaskForUGUI.git",
"com.coplaydev.unity-mcp": "https://github.com/CoplayDev/unity-mcp.git?path=/MCPForUnity",
"com.cysharp.unitask": "http://1.14.122.170:3000/PHXH/UniTask.git?path=src/UniTask/Assets/Plugins/UniTask", "com.cysharp.unitask": "http://1.14.122.170:3000/PHXH/UniTask.git?path=src/UniTask/Assets/Plugins/UniTask",
"com.google.external-dependency-manager": "1.2.183", "com.google.external-dependency-manager": "1.2.183",
"com.leancloud.realtime": "http://1.14.122.170:3000/PHXH/csharp-sdk-upm.git#realtime-1.0.2", "com.leancloud.realtime": "http://1.14.122.170:3000/PHXH/csharp-sdk-upm.git#realtime-1.0.2",