Codely Bridge自定义
概述
Codely Bridge 支持开发者将静态方法注册为自定义工具,通过专属特性标记方法,即可让 AI 助手调用自定义逻辑,快速扩展 Unity 编辑器功能。
核心标记组件为 CustomToolAttribute 特性,该特性仅作用于方法,不可重复标记,用于定义工具名称与描述,标识可被 Tuanjie AI 调用的自定义方法。
快速开始
创建自定义工具类
在你的 Unity 项目中创建一个静态类,并定义静态方法:
using Codely.Newtonsoft.Json.Linq;
using UnityTcp.Editor.Tools;
public static class MyCustomTools
{
[ExecuteCustomTool.CustomTool("create_folder", "在指定路径创建文件夹")]
public static object CreateFolder(JObject parameters)
{
string path = parameters["path"]?.ToString();
string folderName = parameters["folderName"]?.ToString();
if (string.IsNullOrEmpty(path) || string.IsNullOrEmpty(folderName))
{
return new { success = false, error = "缺少必要参数" };
}
// 执行创建文件夹的逻辑
// ...
return new { success = true, message = $"文件夹 {folderName} 创建成功" };
}
}
方法签名要求
自定义工具方法必须严格遵循固定签名格式,确保 Tuanjie AI 能正常识别与调用。
public static object MethodName(JObject parameters)
注意: 原文档中包含飞书多维表格,MDX转换中无法直接嵌入。表格内容需要从飞书文档中手动获取。
特性参数
通过指定特性标记方法,配置工具唯一名称与功能描述,完成方法与自定义工具的绑定。
[ExecuteCustomTool.CustomTool(name, description)]
注意: 原文档中包含飞书多维表格,MDX转换中无法直接嵌入。表格内容需要从飞书文档中手动获取。
完整示例
该示例展示如何通过自定义工具获取项目信息
编写工具脚本
using System.Collections.Generic;
using Codely.Newtonsoft.Json.Linq;
using UnityTcp.Editor.Tools;
using UnityEditor;
public static class ProjectTools
{
[ExecuteCustomTool.CustomTool("get_project_info", "获取项目基本信息,包括项目名称、版本、场景列表等")]
public static object GetProjectInfo(JObject parameters)
{
var scenes = new List<string>();
foreach (var scene in EditorBuildSettings.scenes)
{
if (scene.enabled)
{
scenes.Add(scene.path);
}
}
return new
{
success = true,
projectName = PlayerSettings.productName,
version = PlayerSettings.bundleVersion,
scenes,
targetPlatform = EditorUserBuildSettings.activeBuildTarget.ToString()
};
}
}
配置 SKILL 说明
添加 SKILL 文档的作用是告知 AI 模型新增工具能力,明确工具的使用场景、调用方式、参数规则与返回结果,让模型知晓何时调用、如何调用该自定义工具。文档需定义工具名称、描述、使用场景、调用格式、返回字段及使用示例,修改后需新建对话会话生效。
---
name: getprojectinfo
description: Retrieve basic information about the current Unity project, including project name, version, enabled scenes in build settings, and target platform.
---
# getProjectInfo
<!-- IMPORTANT: After creating or modifying this file, start a NEW chat session
for changes to take effect. Skills are loaded when a new session starts. -->
## When to Use
- When the user asks about project information
- When you need to know the project name or version
- When checking which scenes are included in the build
- When determining the current build target platform
## Instructions
Use the `get_project_info` custom tool to retrieve project information.
**Tool Name:** `get_project_info`
**Parameters:** None required
Invoke the tool with an empty parameters object:
```json
{
"tool_name": "get_project_info",
"parameters": {}
}
Response Fields:
| Field | Type | Description |
|---|---|---|
| success | bool | Whether the operation succeeded |
| projectName | string | The product name from PlayerSettings |
| version | string | The bundle version from PlayerSettings |
| scenes | string[] | List of enabled scene paths in build settings |
| targetPlatform | string | Current active build target (e.g., "StandaloneWindows", "Android") |
Examples
Example 1: Get project overview
User: "What's the current project info?"
Invoke:
{
"tool_name": "get_project_info",
"parameters": {}
}
Response:
{
"success": true,
"projectName": "My Game",
"version": "1.0.0",
"scenes": [
"Assets/Scenes/MainMenu.unity",
"Assets/Scenes/Game.unity"
],
"targetPlatform": "StandaloneWindows"
}
Example 2: Check build configuration
User: "Which scenes are included in the build?"
Invoke the tool and report the scenes array from the response.
### 运行测试
完成脚本编写与 SKILL 配置后,即可触发工具调用进行功能测试。

## API 参考
### CustomToolAttribute
```typescript
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class CustomToolAttribute : Attribute
{
public string Name { get; } // 工具名称
public string Description { get; } // 工具描述
public CustomToolAttribute(string name, string description = null);
}
ExecuteCustomTool
public static class ExecuteCustomTool
{
// 执行工具
public static object HandleCommand(JObject @params);
// 手动注册工具
public static void RegisterTool(string name, MethodInfo method);
// 获取所有已注册的工具名称
public static IEnumerable<string> GetRegisteredTools();
}
注意事项
- 表格处理: 原文档中的飞书多维表格无法在MDX中直接嵌入,需要手动从原文档中获取表格内容
- 图片路径: 所有图片已保存在
./images/目录下 - 代码高亮: 支持C#、TypeScript、Java、SQL等多种编程语言的高亮显示
- 表格标记: 原文档中的表格标记
<sheet token="..."/>在MDX中已替换为注释说明