UnityAgent SDK を使って、独自のツールを開発し、LLMから呼び出せるようにする方法を解説します。

SDK パッケージ

カスタムツールの開発には com.ajisaiflow.unityagent.sdk パッケージが必要です。UnityAgent 本体をインストールすると依存関係として自動インストールされます。

SDK パッケージは AgentToolAttribute と ToolRisk enum のみを含む軽量なパッケージです。

基本ルール

カスタムツールは以下のルールに従う必要があります:

  1. クラスは public static であること
  2. メソッドは public static で、戻り値は string(または IEnumerator)
  3. メソッドに [AgentTool("説明")] 属性を付与
  4. パラメータの型は string, int, float, bool のみ
  5. デフォルト値付きパラメータも使用可能

AgentToolAttribute

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})";
    }
}