# Rudder SDK for Unity

LiveOps SDK for Unity: device auth, player profile, remote config, stores,
inventory, leaderboards, battle pass, quests, storage, scenarios and realtime.

The package wraps `Rudder.Core.dll` (the .NET SDK) with Unity adapters:
`UnityWebRequest` transport, `PlayerPrefs` token storage, websocket realtime.

## Installation

The package is published to the Rudder scoped registry. Add the registry to
your project's `Packages/manifest.json`:

```json
{
  "scopedRegistries": [
    {
      "name": "Rudder",
      "url": "https://hub.rudder.build/api/packages/rudder/npm/",
      "scopes": ["rudder"]
    }
  ],
  "dependencies": {
    "rudder.sdk": "0.2.0",
    "com.unity.nuget.newtonsoft-json": "3.2.2"
  }
}
```

`com.unity.nuget.newtonsoft-json` is required — `Rudder.Core` serializes with
Newtonsoft.Json and the package does not bundle the DLL.

## Quickstart

Create a configuration asset via `Assets > Create > Rudder > Configuration`,
set `ProjectKey`, `BaseUrl` and `RealtimeUrl`, then initialize once at
startup:

```csharp
using RudderSdk.Unity;

Rudder.Initialize(configuration);   // or add the Rudder component to a scene
var client = await Rudder.Ready;    // faults if initialization failed

var session = await client.Auth.LoginWithDeviceAsync("global", "en", nickname: "Player");
var configs = await client.RemoteConfig.LoadAsync();
var stores = await client.Stores.ListAsync();
```

All features hang off the client: `Auth`, `Player`, `BattlePass`, `Quests`,
`Stores`, `Inventory`, `Leaderboards`, `RemoteConfig`, `Scenario`, `Storage`,
`Ugc`, `Realtime`.

```csharp
client.Scenario.OnStoreOffer += session => { /* show offer UI */ };
await client.Scenario.TriggerAsync("player_login");

var purchase = await client.Stores.PurchaseAsync("cozy-camp-shop", "moonberry-boost");

var leaderboard = client.Leaderboards.FindBySlug("cozy-collector-score");
await leaderboard.SubmitAsync(score);
var top = await leaderboard.ListAsync(10);
```

## Lifecycle

- `Rudder.State` — `NotInitialized` / `Initializing` / `Ready` / `Failed`.
- `Rudder.LastError` — the initialization error when `State` is `Failed`.
- `Rudder.Client` — the initialized `RudderClient`; throws until `Ready`.
- `Rudder.Ready` — task that completes with the client or the initialization
  error; `Rudder.Initialized` is the event-flavored sugar over it.
- HTTP timeout defaults to 10 seconds, configurable per
  `RudderConfiguration.TimeoutSeconds`.

Errors surface as `RudderApiException` subclasses from `RudderSdk.Core`:
`RudderAuthException` (401, session over), `RudderNotFoundException` (404),
`RudderRateLimitException` (429) and `RudderNetworkException` (no response).
