Skip to main content

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​

ItemRequirement
Attribute[ExecuteCustomTool.CustomTool(name, description)], applied once to a method
NameUse a unique registration name to avoid collisions
DescriptionOptional explanatory text
DiscoveryPublic static methods in loaded assemblies
InputExactly one Codely.Newtonsoft.Json.Linq.JObject parameter
ReturnPrefer 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> containing success is returned directly.
  • Other values are wrapped in { success, message, data, state }, with the original value in data.
  • An anonymous new { success = false } is therefore wrapped as data inside an outer success response. To report a failure, return a dictionary with success: false and 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​

SymptomCheck
Tool not foundCompilation, public/static visibility, a single JObject parameter, assembly references and registration name
Old implementation runsWait for compilation and assembly reload
Empty parametersEnsure parameters is a JSON object with the expected field names
Outer success hides an inner failureReturn a dictionary containing success, rather than an anonymous failure object
Editor unavailableResolve the Bridge connection before debugging the method

Use Unity Insight for indexed read-only analysis and Unity Tools for editor operations.