# Unity

Add the Wavedash Unity package and publish through WebGL builds.

Source: https://docs.wavedash.com/engines/unity

Add the Wavedash Unity package, configure your WebGL build settings, and call the C# SDK through the JavaScript bridge.

<GithubLink href="https://github.com/wvdsh/sdk-unity" label="View the SDK on GitHub" />
<PlaytestLink href="https://wavedash.com/playtest/unity-example/4ab37287-a1d2-4743-8dc1-479b517585fb" />

## Install the SDK

Open **Window > Package Manager**, click **+** > **Install package from git URL...**, and paste:

```
https://github.com/wvdsh/sdk-unity.git
```

## WebGL build settings

1. Switch to **WebGL** platform in **File > Build Settings**
2. In **Player Settings**, set compression to Gzip or Brotli and enable decompression fallback
3. Select the **Default** WebGL template (not Minimal)
4. Build to a folder you set as `upload_dir` in `wavedash.toml`

<Warning>
The **Minimal** template omits the `buildUrl` variable in `index.html`. The Wavedash CLI expects that variable. Use **Default**.
</Warning>

## Use the SDK from C#

<Urgent>
**Calling `Init()` is required.** It opts your game into Wavedash platform features and is how the SDK confirms you're set up. Call it once your game is ready to play.
</Urgent>

```csharp
void Awake()
{
    Wavedash.SDK.Init(new Dictionary<string, object> { { "debug", true } });
    // Init() automatically calls ReadyForEvents() unless you pass { "deferEvents", true }
    // in the config. If you do defer, call Wavedash.SDK.ReadyForEvents() manually after
    // your pre-game setup is complete.
}

async void LogPlayerAndScore()
{
    var user = Wavedash.SDK.GetUser();
    Debug.Log(user != null ? user["username"] : "no user");

    var lb = await Wavedash.SDK.GetLeaderboard("high-scores");
    string leaderboardId = lb != null ? (string)lb["id"] : null;
    var result = await Wavedash.SDK.UploadLeaderboardScore(leaderboardId, 1500, keepBest: true);
    if (result != null) Debug.Log($"rank {result["rank"]}");
}
```

## P2P messaging

Unity exposes the same WebRTC P2P API as the JavaScript SDK. Messages are binary (`byte[]` or `ArraySegment<byte>`). Reliable messages are ordered and guaranteed; unreliable messages are faster but lossy.

```csharp
// Broadcast to every peer in the lobby
Wavedash.SDK.BroadcastP2PMessage(payload, channel: 0, reliable: true);

// Send to a specific peer
Wavedash.SDK.SendP2PMessage(targetUserId, payload, channel: 0, reliable: true);

// Drain queued incoming messages once per frame
private readonly List<Wavedash.P2PMessage> _messageBuffer = new();

void Update()
{
    int count = Wavedash.SDK.DrainP2PChannel(0, _messageBuffer);
    for (int i = 0; i < count; i++)
    {
        var msg = _messageBuffer[i];
        HandleMessage(msg.SenderId, msg.Channel, msg.Payload);
    }
}
```

`Wavedash.SDK.MAX_PAYLOAD_SIZE` reports the maximum payload bytes for a single P2P message, derived from your `P2PConfig` at `Init()` time. See [Multiplayer networking](/multiplayer/networking) for the cross-language reference and channel conventions.

For Mirror or Netcode for GameObjects projects, the SDK ships `WavedashTransport` integrations under `Integrations~/Mirror` and `Integrations~/NetcodeForGameObjects`.

## wavedash.toml

```toml
game_id = "YOUR_GAME_ID_HERE"
upload_dir = "./Builds/WebGL"

[unity]
version = "6000.0.2f1"
```
