跳转至

Unity + Koog:通过 Kotlin 智能体驱动你的游戏

在 GitHub 上打开 下载 .ipynb

本笔记本将引导你使用 Koog 和模型上下文协议(MCP)构建一个精通 Unity 的 AI 智能体。我们将连接到一个 Unity MCP 服务器,发现工具,通过 LLM 进行规划,并对你当前打开的场景执行操作。

先决条件 - 已安装 Unity-MCP 服务器插件的 Unity 项目 - JDK 17+ - 在 OPENAI_API_KEY 环境变量中设置 OpenAI API 密钥

%useLatestDescriptors
%use koog
lateinit var process: Process

1) 提供你的 OpenAI API 密钥

我们从 OPENAI_API_KEY 环境变量中读取 API 密钥,以便将密钥与笔记本内容分离。

val token = System.getenv("OPENAI_API_KEY") ?: error("OPENAI_API_KEY environment variable not set")
val executor = simpleOpenAIExecutor(token)

2) 配置 Unity 智能体

我们为 Unity 定义一个简洁的系统提示词和智能体设置。

val agentConfig = AIAgentConfig(
    prompt = prompt("cook_agent_system_prompt") {
        system {
            "You are a Unity assistant. You can execute different tasks by interacting with tools from the Unity engine."
        }
    },
    model = OpenAIModels.Chat.GPT4o,
    maxAgentIterations = 1000
)

3) 启动 Unity MCP 服务器

我们将从你的 Unity 项目目录启动 Unity MCP 服务器,并通过标准输入/输出进行连接。

// https://github.com/IvanMurzak/Unity-MCP
val pathToUnityProject = "path/to/unity/project"
val process = ProcessBuilder(
    "$pathToUnityProject/com.ivanmurzak.unity.mcp.server/bin~/Release/net9.0/com.IvanMurzak.Unity.MCP.Server",
    "60606"
).start()

4) 从 Koog 连接并运行智能体

我们从 Unity MCP 服务器发现工具,构建一个简单的“先规划后执行”策略,并运行一个仅使用工具来修改你当前打开场景的智能体。

import kotlinx.coroutines.runBlocking

runBlocking {
    // Create the ToolRegistry with tools from the MCP server
    val toolRegistry = McpToolRegistryProvider.fromTransport(
        transport = McpToolRegistryProvider.defaultStdioTransport(process)
    )

    toolRegistry.tools.forEach {
        println(it.name)
        println(it.descriptor)
    }

    val strategy = strategy<String, String>("unity_interaction") {
        val nodePlanIngredients by nodeLLMRequest(allowToolCalls = false)
        val interactionWithUnity by subgraphWithTask<String, String>(
            // work with plan
            tools = toolRegistry.tools,
        ) { input ->
            "Start interacting with Unity according to the plan: $input"
        }

        edge(
            nodeStart forwardTo nodePlanIngredients transformed {
                "Create detailed plan for " + agentInput + "" +
                    "using the following tools: ${toolRegistry.tools.joinToString("\n") {
                        it.name + "\ndescription:" + it.descriptor
                    }}"
            }
        )
        edge(nodePlanIngredients forwardTo interactionWithUnity onAssistantMessage { true })
        edge(interactionWithUnity forwardTo nodeFinish)
    }

    val agent = AIAgent(
        promptExecutor = executor,
        strategy = strategy,
        agentConfig = agentConfig,
        toolRegistry = toolRegistry,
        installFeatures = {
            install(Tracing)

            install(EventHandler) {
                onAgentStarting { eventContext ->
                    println("OnAgentStarting first (strategy: ${strategy.name})")
                }

                onAgentStarting { eventContext ->
                    println("OnAgentStarting second (strategy: ${strategy.name})")
                }

                onAgentCompleted { eventContext ->
                    println(
                        "OnAgentCompleted (agent id: ${eventContext.agentId}, result: ${eventContext.result})"
                    )
                }
            }
        }
    )

    val result = agent.run(
        " extend current opened scene for the towerdefence game. " +
            "Add more placements for the towers, change the path for the enemies"
    )

    result
}

5) 关闭 MCP 进程

在运行结束时,务必清理外部的 Unity MCP 服务器进程。

// Shutdown the Unity MCP process
process.destroy()