I want my Unity application to create a room and h...
# ask-ai
b
I want my Unity application to create a room and have an agent join that room. What Unity code do I need to make the agent join the room?
t
Thanks for reaching out, Mat. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — you're asking for the Unity code to create a room and have an agent join it. Quick answer: Unity code can create/connect to the room, but the agent must be dispatched server-side via the access token. Below are the exact steps and minimal Unity code plus the backend token change needed to trigger the agent. 1) Unity (client) — connect (this creates the room if it doesn't exist)
Copy code
using System.Collections;
using UnityEngine;
using LiveKit; // your LiveKit Unity SDK namespace

public class LiveKitConnector : MonoBehaviour
{
    IEnumerator Start()
    {
        var room = new Room();
        // use your LiveKit wss url and the participant token returned by your backend
        var connOp = room.Connect("wss://<your-project>.livekit.cloud", "<participant-token>");
        yield return connOp;

        if (!connOp.IsError)
        {
            Debug.Log("Connected to room");
            // now room exists and participants are present
        }
        else
        {
            Debug.LogError("Connect error: " + connOp.Error);
        }
    }
}
2) Backend (server) — generate a token that requests agent dispatch • The Unity client must receive a participant token generated by your backend. • When creating that token, include room join permission for the room name and add a RoomConfiguration that dispatches the agent. Example (Node.js/TypeScript style):
Copy code
import { AccessToken, VideoGrant } from "livekit-server-sdk";
import { RoomConfiguration, RoomAgentDispatch } from "@livekit/protocol";

const at = new AccessToken(API_KEY, API_SECRET, { identity: "user-id" });

const grant: VideoGrant = { room: "my-room", roomJoin: true };
at.addGrant(grant);

// request agent dispatch when this token is used to connect
at.roomConfig = new RoomConfiguration({
  agents: [
    new RoomAgentDispatch({ agentName: "my-agent", metadata: "optional" })
  ],
});

const token = at.toJwt();
// return `token` to the Unity client
What happens when Unity connects with that token • The first client connection creates the room. • LiveKit sees the token includes
roomConfig
and dispatches the specified agent to join the room automatically. • The agent itself runs as a separate service and joins via its own SDK; you do not write agent-join code in Unity. If you want, I can: • Provide a concrete backend snippet for your language (Node/Python/Go). • Show an example agent service connection snippet. Which would you like? Sources: Unity quickstart (WebGL) | LiveKit docs | Agent-assisted warm transfer | LiveKit docs | Agent dispatch | LiveKit docs
b
For testing, is there a way to use command-line tools in place of the server code you just provided? I have the LiveKit CLI and am on Windows 11.
t
Yes — you can test without writing backend code using the LiveKit CLI on Windows 11. Below are the minimal steps and commands to (A) dispatch an agent directly, or (B) generate a token that will dispatch an agent when a participant connects. Prerequisites • Install LiveKit CLI: ◦ winget:
winget install LiveKit.LiveKitCLI
◦ or download from https://github.com/livekit/livekit-cli/releases • Authenticate to your project:
lk cloud auth
• Ensure your agent is deployed/registered (you can deploy with
lk agent create ./agent-directory
if you need to). A — Dispatch an agent to a room now (creates room if needed) 1. Run:
Copy code
lk dispatch create --agent-name test-agent --room my-room --metadata '{"user_id":"12345"}'
1. Verify dispatch / agent status:
Copy code
lk agent list
   lk agent status --id <agent-id>
   lk agent logs --id <agent-id>
B — Generate a participant token that triggers agent dispatch when the participant connects 1. Create a token that includes dispatch:
Copy code
lk token create --api-key <livekit_api_key> --api-secret <livekit_api_secret> --room my-room --identity test-participant --join --dispatch-agent test-agent --dispatch-metadata '{"user_id":"12345"}' --valid-for 24h
This prints a token you can use in your Unity app. 1. Use that token in your Unity
room.Connect(wssUrl, token)
call — when the participant connects, LiveKit will dispatch
test-agent
automatically. Notes •
lk dispatch create
is useful for ad-hoc testing;
lk token create --dispatch-agent
simulates the real flow where the token causes dispatch on participant join. • Use
lk agent logs
to watch the agent join events and debug any issues. Would you like a ready-made Unity snippet that uses the token printed by
lk token create
, or a specific CLI command tailored to your project name and agent name? Sources: LiveKit CLI Setup | LiveKit Docs | Agent dispatch | LiveKit docs | livekit/livekit_composite | DeepWiki