Codely Bridge Custom Tools
Codely Bridge dispatches tool requests to a running Unity / Tuanjie editor. Use execute_custom_tool to invoke custom C# static methods in your project.
Prepare the editor script
Install Bridge, then place the script under Assets/Editor/. If using assembly definitions, ensure the editor assembly references Bridge and its JSON types. Wait for compilation to finish and resolve Console errors before invoking the tool.
Custom operations must implement their own Undo, dirty-state and save behavior where appropriate. Returning a success value does not perform these operations automatically.
using System.Collections.Generic;
using Codely.Newtonsoft.Json.Linq;
using UnityEditor;
using UnityTcp.Editor.Tools;
public static class ProjectTools
{
[ExecuteCustomTool.CustomTool("get_project_info", "Read project name, version and build scenes")]
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 Dictionary<string, object>
{
["success"] = true,
["data"] = new Dictionary<string, object>
{
["projectName"] = PlayerSettings.productName,
["version"] = PlayerSettings.bundleVersion,
["scenes"] = scenes,
["targetPlatform"] = EditorUserBuildSettings.activeBuildTarget.ToString()
}
};
}
}
Registration rules
| Item | Requirement |
|---|---|
| Attribute | [ExecuteCustomTool.CustomTool(name, description)], applied once to a method |
| Name | Use a unique registration name to avoid collisions |
| Description | Optional explanatory text |
| Discovery | Public static methods in loaded assemblies |
| Input | Exactly one Codely.Newtonsoft.Json.Linq.JObject parameter |
| Return | Prefer object or Dictionary<string, object>; do not return an unawaited Task |
Bridge scans and caches tools on first use. Duplicate names overwrite earlier registrations; do not rely on assembly enumeration order. After editing scripts, wait for compilation and assembly reload before retrying.
Manual registration
Use ExecuteCustomTool.RegisterTool(string name, MethodInfo method) for code-side registration; an existing name is overwritten. ExecuteCustomTool.GetRegisteredTools() ensures discovery has run and returns registered names. Manually registered methods should follow the same input convention.
Invoke a tool
Pass these arguments to execute_custom_tool. tool_name is the registered name, not the class or C# method name:
{"tool_name":"get_project_info","parameters":{}}
The outer argument object and tool_name are required. Missing, null or non-object parameters currently become an empty JObject. Validate your tool's required fields yourself.
Lookup tries an exact name first, then a case-insensitive match. A missing tool returns an error listing available names. Invocation exceptions are unwrapped and returned as errors.
Return values
- A
Dictionary<string, object>containingsuccessis returned directly. - Other values are wrapped in
{ success, message, data, state }, with the original value indata. - An anonymous
new { success = false }is therefore wrapped as data inside an outer success response. To report a failure, return a dictionary withsuccess: falseand an error description.
The example uses a standard dictionary, with project information under data.
Explain the capability to Codely
Use a project SKILL.md to describe when to invoke execute_custom_tool, its registration name, parameters and result fields. The attribute registers the C# method; the skill explains its use to the model. See Skills for installation and reload instructions.
Troubleshooting
| Symptom | Check |
|---|---|
| Tool not found | Compilation, public/static visibility, a single JObject parameter, assembly references and registration name |
| Old implementation runs | Wait for compilation and assembly reload |
| Empty parameters | Ensure parameters is a JSON object with the expected field names |
| Outer success hides an inner failure | Return a dictionary containing success, rather than an anonymous failure object |
| Editor unavailable | Resolve the Bridge connection before debugging the method |
Use Unity Insight for indexed read-only analysis and Unity Tools for editor operations.