UnityAgent SDK を使って、独自のツールを開発し、LLMから呼び出せるようにする方法を解説します。
カスタムツールの開発には com.ajisaiflow.unityagent.sdk パッケージが必要です。UnityAgent 本体をインストールすると依存関係として自動インストールされます。
SDK パッケージは AgentToolAttribute と ToolRisk enum のみを含む軽量なパッケージです。
カスタムツールは以下のルールに従う必要があります:
public static であることpublic static で、戻り値は string(または IEnumerator)[AgentTool("説明")] 属性を付与string, int, float, bool のみusing AjisaiFlow.UnityAgent.SDK;
[AgentTool("ツールの説明文")]
public static string MyTool(string param1, int param2 = 0)
{
// 処理
return "結果の文字列";
}
| プロパティ | 型 | 説明 |
|---|---|---|
Description |
string | ツールの説明(必須、コンストラクタ引数) |
Category |
string | カテゴリ名 |
Author |
string | 作者名 |
Version |
string | バージョン |
Url |
string | URL(ドキュメント等) |
Risk |
ToolRisk | リスクレベル(デフォルト: Caution) |
public enum ToolRisk
{
Safe = 0, // 読み取り専用(List, Get, Inspect 等)
Caution = 1, // 変更操作(Create, Set, Add 等)
Dangerous = 2 // 破壊的操作(Delete, Remove, Run 等)
}
using UnityEngine;
using UnityEditor;
using AjisaiFlow.UnityAgent.SDK;
public static class MyTools
{
[AgentTool("Get the world position of the currently selected object")]
public static string GetSelectedPosition()
{
var go = Selection.activeGameObject;
if (go == null) return "No object selected";
var pos = go.transform.position;
return $"Position: ({pos.x:F3}, {pos.y:F3}, {pos.z:F3})";
}
}