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

# Scripting API

> C# scripting and programmatic control for Unity

# Scripting API

The Daydream Unity plugin exposes a C# API for scripting, automation, and building custom interactions.

## Accessing the Component

```csharp theme={null}
// Get the Daydream component from a Camera
var daydream = GetComponent<Daydream>();
```

## State Machine

The component follows a simple state machine:

```mermaid theme={null}
stateDiagram-v2
    [*] --> Idle
    Idle --> Creating: Play pressed
    Creating --> Connecting: Stream created
    Connecting --> Streaming: WebRTC connected
    Streaming --> Reconnecting: Disconnected
    Reconnecting --> Streaming: Reconnected
    Reconnecting --> Error: Max retries
    Creating --> Error: API error
    Connecting --> Error: WebRTC error
```

### States

| State          | Description                              |
| -------------- | ---------------------------------------- |
| `Idle`         | Component created, not yet started       |
| `Creating`     | Stream endpoint being created via API    |
| `Connecting`   | WebRTC handshake in progress             |
| `Streaming`    | Video streaming and AI processing active |
| `Reconnecting` | Auto-reconnecting after disconnect       |
| `Error`        | Connection failed                        |

```csharp theme={null}
var daydream = GetComponent<Daydream>();

// Check current state
if (daydream.State == DaydreamState.Streaming)
{
    Debug.Log("AI processing is active!");
}
```

## Output Texture

Access the AI-transformed video as a `Texture` for use on any surface:

```csharp theme={null}
var daydream = GetComponent<Daydream>();

// Get the AI output texture
Texture aiOutput = daydream.OutputTexture;

// Apply to a material
GetComponent<Renderer>().material.mainTexture = aiOutput;
```

<Tip>
  Set `showOverlay = false` to disable the built-in fullscreen overlay when you want to render the AI output on custom surfaces like 3D objects or UI elements.
</Tip>

## Changing Prompts at Runtime

```csharp theme={null}
var daydream = GetComponent<Daydream>();

// Change the prompt — updates are sent to the server automatically
daydream.SetPrompt("cyberpunk cityscape, neon lights");
```

Parameters are synced to the server every 100ms via debounced PATCH requests. You can also modify any public field directly:

```csharp theme={null}
var daydream = GetComponent<Daydream>();

// Adjust generation parameters
daydream.guidanceScale = 2.0f;
daydream.delta = 0.5f;
daydream.seed = 123;

// Toggle ControlNets
daydream.controlnets[0].conditioningScale = 0.6f; // Depth
daydream.controlnets[1].conditioningScale = 0.3f; // Canny

// Toggle display
daydream.showOverlay = false;
daydream.showOriginalPIP = false;
```

## Inspector Fields

All fields on the `Daydream` component are public and can be modified from scripts:

| Field               | Type                 | Description                  |
| ------------------- | -------------------- | ---------------------------- |
| `apiUrl`            | `string`             | Server endpoint              |
| `apiKey`            | `string`             | Authentication token         |
| `modelId`           | `string`             | Diffusion model ID           |
| `resolution`        | `int`                | Output size (384-1024)       |
| `prompt`            | `string`             | Text prompt                  |
| `negativePrompt`    | `string`             | Negative prompt              |
| `guidanceScale`     | `float`              | Prompt adherence (0.1-20.0)  |
| `delta`             | `float`              | Diffusion strength (0.0-1.0) |
| `seed`              | `int`                | RNG seed (-1 for random)     |
| `numInferenceSteps` | `int`                | Diffusion steps (1-100)      |
| `controlnets`       | `ControlNetConfig[]` | ControlNet configurations    |
| `ipAdapter`         | `IPAdapterConfig`    | IP Adapter configuration     |
| `showOverlay`       | `bool`               | Show fullscreen overlay      |
| `showOriginalPIP`   | `bool`               | Show PIP camera view         |
| `pipSize`           | `float`              | PIP size ratio (0.15-0.4)    |

## Types

### ControlNetConfig

```csharp theme={null}
[Serializable]
public class ControlNetConfig
{
    public bool enabled;
    public string modelId;
    public float conditioningScale;    // 0.0-1.0
    public string preprocessor;        // "depth", "canny", "tile", etc.
    public float controlGuidanceStart; // 0.0-1.0
    public float controlGuidanceEnd;   // 0.0-1.0
}
```

### IPAdapterConfig

```csharp theme={null}
[Serializable]
public class IPAdapterConfig
{
    public bool enabled;
    public float scale;
    public string type;       // "regular" or "faceid"
    public string weightType; // "linear", "style transfer", etc.
}
```

### PromptEntry / SeedEntry

```csharp theme={null}
[Serializable]
public class PromptEntry
{
    public string text;
    public float weight;
}

[Serializable]
public class SeedEntry
{
    public int seed;
    public float weight;
}
```

## Example: Audio-Reactive Effects

Modulate AI parameters based on audio analysis:

```csharp theme={null}
using UnityEngine;

public class AudioReactiveDaydream : MonoBehaviour
{
    public Daydream daydream;
    public AudioSource audioSource;

    private float[] spectrum = new float[256];

    void Update()
    {
        audioSource.GetSpectrumData(spectrum, 0, FFTWindow.Rectangular);

        // Map bass frequencies to guidance scale
        float bass = 0f;
        for (int i = 0; i < 8; i++) bass += spectrum[i];
        daydream.guidanceScale = Mathf.Lerp(1.0f, 3.0f, bass * 10f);

        // Map mid frequencies to delta
        float mids = 0f;
        for (int i = 8; i < 64; i++) mids += spectrum[i];
        daydream.delta = Mathf.Lerp(0.3f, 0.8f, mids * 20f);
    }
}
```

## Example: Prompt Cycling

Cycle through prompts on a timer:

```csharp theme={null}
using UnityEngine;

public class PromptCycler : MonoBehaviour
{
    public Daydream daydream;
    public float interval = 10f;
    public string[] prompts = {
        "anime style, vibrant colors",
        "oil painting, impressionist",
        "cyberpunk, neon lights",
        "watercolor, soft pastels"
    };

    private int index;
    private float timer;

    void Update()
    {
        timer += Time.deltaTime;
        if (timer >= interval)
        {
            timer = 0f;
            index = (index + 1) % prompts.Length;
            daydream.SetPrompt(prompts[index]);
        }
    }
}
```

## Example: Render to 3D Surface

Apply AI output to a 3D object instead of the screen overlay:

```csharp theme={null}
using UnityEngine;

public class DaydreamOnMesh : MonoBehaviour
{
    public Daydream daydream;
    public Renderer targetRenderer;

    void Start()
    {
        // Disable the built-in overlay
        daydream.showOverlay = false;
    }

    void Update()
    {
        if (daydream.OutputTexture != null)
        {
            targetRenderer.material.mainTexture = daydream.OutputTexture;
        }
    }
}
```

## Next Steps

* [Features](/sdks/unity/features) - ControlNets, IP Adapter, prompt scheduling
* [Installation](/sdks/unity/installation) - Setup guide
* [GitHub Repository](https://github.com/daydreamlive/daydream-unity) - Source code and issues
