> ## Documentation Index
> Fetch the complete documentation index at: https://docs.zeroruntime.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# AI Voice Agent with Unity

> Connect a Zero Runtime voice AI agent to a native Unity application so users can talk to it in real time using the Google Gemini Live API.

This guide demonstrates how to integrate a real-time Zero Runtime AI agent with a Unity application. The agent, powered by Google's Gemini Live API, acts as a high-energy game show host, guiding the user through a number-guessing game via voice.

## Prerequisites

Before you begin, ensure you have the following:

* A Zero Runtime account (create one from the [Zero Runtime Dashboard](https://app.zeroruntime.ai/)).
* **Unity 2022.3 LTS** or later.
* **Python 3.11+** for the AI agent.
* A **Google API Key** for the Gemini Live API.

<Note>
  You need a Zero Runtime account to generate a token and a Google API key for the Gemini Live API.
  Generate a token from the [Zero Runtime Dashboard](https://app.zeroruntime.ai/) and a Google API key from [Google AI Studio](https://aistudio.google.com/api-keys).
</Note>

## 1. Unity Frontend

<Steps>
  <Step title="Step 1: Install the RTC SDK package" icon="download">
    1. Open Unity's **Package Manager** by selecting from the top bar: Window -> Package Manager.

    2. Click the + button in the top left corner and select **Add package from git URL.**

    3. Paste the following URL and click Add:

    ```js theme={null}
    https://github.com/videosdk-live/videosdk-rtc-unity-sdk.git
    ```

    Install the package from the Unity Package Manager using the git URL above.

    4. Add the `com.unity.nuget.newtonsoft-json` package by following the instructions provided [here](https://github.com/applejag/Newtonsoft.Json-for-Unity/wiki/Install-official-via-UPM).
  </Step>

  <Step title="Step 2: Platform setup" icon="mobile-screen">
    <Tabs>
      <Tab title="Android">
        To integrate the RTC SDK into your Android project, follow these steps:

        1. Add the following repository configuration to your `settingsTemplate.gradle` file:

        ```js title="settingsTemplate.gradle" theme={null}
        dependencyResolutionManagement {
            repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
            repositories {
                **ARTIFACTORYREPOSITORY**
                google()
                mavenCentral()
                jcenter()
                maven {
                    url = uri("https://maven.aliyun.com/repository/jcenter")
                }
                flatDir {
                    dirs "${project(':unityLibrary').projectDir}/libs"
                }
            }
        }
        ```

        2. Install the Android SDK in `mainTemplate.gradle`:

        ```js title="mainTemplate.gradle" theme={null}
        dependencies {
            implementation 'live.videosdk:rtc-android-sdk:0.3.1'
        }
        ```

        3. If your project has set `android.useAndroidX=true`, then set `android.enableJetifier=true` in the `gradleTemplate.properties` file to migrate your project to AndroidX and avoid duplicate class conflicts.

        ```js title="gradleTemplate.gradle" theme={null}
        android.enableJetifier = true;
        android.useAndroidX = true;
        android.suppressUnsupportedCompileSdk = 34;
        ```
      </Tab>

      <Tab title="iOS">
        1. **Build for iOS**: In Unity, export the project for iOS.
        2. **Open in Xcode**: Navigate to the generated Xcode project and open it.
        3. **Configure Frameworks**:
           * Select the **Unity-iPhone** target.
           * Go to the **General** tab.
           * Under **Frameworks, Libraries, and Embedded Content**, add the RTC SDK and its required frameworks.
      </Tab>
    </Tabs>
  </Step>

  <Step title="Step 3: Create a meeting room" icon="key">
    Create a static `roomId` using the Zero Runtime API. Both the Unity app and the AI agent will use this ID to connect to the same meeting.

    ```bash theme={null}
    curl -X POST https://api.videosdk.live/v2/rooms \
      -H "Authorization: YOUR_JWT_TOKEN_HERE" \
      -H "Content-Type: application/json"
    ```

    Replace `YOUR_JWT_TOKEN_HERE` with your Zero Runtime auth token. Copy the `roomId` from the response for the next steps.
  </Step>

  <Step title="Step 4: Configure the Unity project" icon="gear">
    Update `GameManager.cs` with your Zero Runtime auth token and the `roomId` you just created.

    ```csharp title="Unity/Assets/Scripts/GameManager.cs" theme={null}
    // ... existing code ...
    public class GameManager : MonoBehaviour
    {
        // ... existing code ...
        private readonly string _token = "YOUR_ZRT_AUTH_TOKEN";
        private readonly string _staticMeetingId = "YOUR_MEETING_ID";
        // ... existing code ...
    }
    ```
  </Step>

  <Step title="Step 5: Set up platform-specific permissions" icon="shield-check">
    <Tabs>
      <Tab title="Android">
        Add the following permissions to your `AndroidManifest.xml` file:

        ```xml title="AndroidManifest.xml" theme={null}
        <uses-permission android:name="android.permission.CAMERA"/>
        <uses-permission android:name="android.permission.RECORD_AUDIO"/>
        <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
        ```
      </Tab>

      <Tab title="iOS">
        Ensure your `Info.plist` includes descriptions for camera and microphone usage:

        ```xml title="Info.plist" theme={null}
        <key>NSCameraUsageDescription</key>
        <string>Camera access is required for video calls.</string>
        <key>NSMicrophoneUsageDescription</key>
        <string>Microphone access is required for audio calls.</string>
        ```
      </Tab>
    </Tabs>
  </Step>
</Steps>

## 2. Python AI Agent

<Steps>
  <Step title="Step 1: Setup the project" icon="download">
    Use Python 3.11 or higher, then install the Zero Runtime Agents SDK. Create and activate a virtual environment, then install `zrt` and `python-dotenv`. Every provider plugin ships with `zrt`.

    <CodeGroup>
      ```bash uv theme={null}
      uv venv --python 3.11
      source .venv/bin/activate     # macOS/Linux
      .\.venv\Scripts\activate      # Windows
      ```

      ```bash pip theme={null}
      python3.11 -m venv venv
      source venv/bin/activate      # macOS/Linux
      .\venv\Scripts\activate       # Windows
      ```
    </CodeGroup>

    <Tip>
      New to uv? See the [uv install guide](https://docs.astral.sh/uv/getting-started/installation/).
    </Tip>

    <CodeGroup>
      ```bash uv theme={null}
      uv pip install zrt python-dotenv
      ```

      ```bash pip theme={null}
      pip install zrt python-dotenv
      ```
    </CodeGroup>
  </Step>

  <Step title="Step 2: Configure environment variables" icon="key">
    Create a `.env` file in the `Unity-quickstart` directory to store your keys and token securely for the Python agent:

    ```shell title=".env" theme={null}
    # Google API Key for the Gemini Live API
    GOOGLE_API_KEY=your_google_api_key_here

    # Option A: pre-generated ZRT auth token
    ZRT_AUTH_TOKEN=your_zrt_auth_token_here

    # Option B: SDK auto-generates the token (omit ZRT_AUTH_TOKEN)
    # ZRT_API_KEY=your_zrt_api_key
    # ZRT_SECRET_KEY=your_zrt_secret_key
    ```
  </Step>

  <Step title="Step 3: Create the AI agent backend" icon="robot">
    The Python agent joins the same meeting room to interact with the user. Create `agent-unity.py` with your `roomId`.

    ```python title="agent-unity.py" theme={null}
    import os
    import zrt
    from zrt import Agent, Pipeline, Room
    from zrt.plugins import GeminiRealtime, GeminiLiveConfig
    from zrt.inference import AICousticsDenoise
    from dotenv import load_dotenv

    load_dotenv()

    AGENT_ID = "unity-game-show-agent"

    pipeline = Pipeline(
        llm=GeminiRealtime(
            api_key=os.getenv("GOOGLE_API_KEY"),  # optional when GOOGLE_API_KEY is in .env
            config=GeminiLiveConfig(model="gemini-3.1-flash-live-preview", voice="Leda", response_modalities=["AUDIO"]),
        ),
        denoise=AICousticsDenoise(model_id="quail-vf-2.2-l-16khz"),
    )


    class MyVoiceAgent(Agent):
        def __init__(self) -> None:
            super().__init__(
                name="MyVoiceAgent",
                agent_id=AGENT_ID,
                instructions="You are a high-energy game-show host guiding the caller to guess a secret number from 1 to 100 to win 1,000,000$.",
                pipeline=pipeline,
            )

        async def on_enter(self) -> None:
            await self.session.say(
                "Welcome to the Zero Runtime AI Agent game show! I'm your host, and we're about to play for 1,000,000$. Are you ready to play?"
            )

        async def on_exit(self) -> None:
            await self.session.say("Goodbye!")


    def invoke_agent() -> None:
        # Join the same room the Unity frontend created (its meeting ID).
        zrt.invoke(AGENT_ID, room=Room(room_id="YOUR_MEETING_ID"))


    if __name__ == "__main__":
        zrt.serve(MyVoiceAgent, on_ready=invoke_agent)
    ```
  </Step>
</Steps>

## 3. Run the Application

<Steps>
  <Step title="Step 1: Run the AI agent" icon="terminal">
    Open a terminal, navigate to the `Unity-quickstart` directory, activate your virtual environment, and run the agent. It reads `GOOGLE_API_KEY` from the `.env` file.

    <CodeGroup>
      ```bash uv theme={null}
      uv run python agent-unity.py
      ```

      ```bash pip theme={null}
      python agent-unity.py
      ```
    </CodeGroup>
  </Step>

  <Step title="Step 2: Run the Unity application" icon="play">
    1. Open the `Unity/` project in Unity Hub.
    2. Once the project is loaded, press the **Play** button in the Unity Editor to start the application.
    3. Click the **Join Meeting** button in the app to connect to the session.

    Once you join the meeting, the AI agent will greet you and start the game.
  </Step>
</Steps>

## Troubleshooting

<AccordionGroup>
  <Accordion title="'Waiting for participant...' but no connection" icon="plug">
    * Ensure both the Unity application and the agent are running.
    * Check that the `roomId` matches in `GameManager.cs` and `agent-unity.py`.
    * Verify your Zero Runtime token is valid.
  </Accordion>

  <Accordion title="Audio not working" icon="volume-high">
    * Check platform permissions for microphone access (see Step 5 in Part 1).
    * Ensure your Google API key has Gemini Live API access enabled.
  </Accordion>

  <Accordion title="Agent not responding" icon="robot">
    * Verify your `GOOGLE_API_KEY` is set in the `.env` file.
    * Check that the Gemini Live API is enabled in your Google Cloud Console.
  </Accordion>
</AccordionGroup>

## Next Steps

<CardGroup cols={2}>
  <Card title="Quickstart Example" icon="github" href="https://github.com/ZeroRuntimeAI/zrt-python-sdk-examples">
    Complete working example with source code.
  </Card>

  <Card title="Build Your First Voice Agent" icon="microphone" href="/quickstarts/build-your-first-voice-agent">
    Learn the Cascade and Realtime pipelines in depth.
  </Card>
</CardGroup>
