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

# Unity

> Gamavas Pass for Unity: sign-in, achievements and XP, and selling your game on WebGL, Windows, macOS, Linux, Android and iOS.

The Unity package works the same on every platform, with the sign-in that fits each one:

| Platform | How players sign in |
| - | - |
| **WebGL** | The Gamavas popup, through the Gamavas script in your page |
| **Windows, macOS, Linux** (and the editor) | The system browser, then straight back to the game |
| **Android, iOS** | The system browser, then a link back into your app |

Unity 2021.3 or later. The source and releases are at [github.com/Gamavas/pass-unity](https://github.com/Gamavas/pass-unity) (MIT).

## Install

**1. Add the package**

In Unity, open **Window → Package Manager → + → Add package from git URL…** and enter:

```text theme={"theme":{"light":"github-light","dark":"github-dark"}}
https://github.com/Gamavas/pass-unity.git#v0.4.0
```

Or add it to `Packages/manifest.json`:

```json Packages/manifest.json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "dependencies": {
    "fun.gamavas.pass": "https://github.com/Gamavas/pass-unity.git#v0.4.0"
  }
}
```

**2. Connect your game**

At [developer.gamavas.fun](https://developer.gamavas.fun), connect your game listing to get a **client ID**. It's public, so it's fine in your build.

Under **Where sign-in returns**, add an address for each platform you ship:

| Platform | Add |
| - | - |
| WebGL | The exact page your game runs on, e.g. `https://your-game.example/` |
| Desktop | `http://127.0.0.1/`. Any port works, as the standard for desktop apps allows. |
| Android, iOS | Your bundle ID in lower case, then `:/pass`, e.g. `com.studio.game:/pass`. See [Mobile](#mobile). |

**3. Start the SDK**

Call `Init` once, as early as you can:

```csharp Login.cs theme={"theme":{"light":"github-light","dark":"github-dark"}}
using Gamavas.Pass;
using Gamavas.Pass.Core;
using UnityEngine;

public class Login : MonoBehaviour
{
    void Awake()
    {
        GamavasPass.Init("YOUR_CLIENT_ID");
        GamavasPass.PlayerChanged += player =>
            Debug.Log(player == null ? "Signed out" : $"Hi @{player.handle}");
    }
}
```

## Sign players in

The quickest way is the **GamavasSignInButton** component: add it to any UI element and listen to its **On Signed In** event. It needs an EventSystem in the scene, like any UI button.

The component starts the sign-in **on press**, not on release. That matters on WebGL: browsers only open popups during a click, so the popup opens on that same click.

To use your own button, call `SignInAsync` from its click handler:

```csharp theme={"theme":{"light":"github-light","dark":"github-dark"}}
try
{
    Player player = await GamavasPass.SignInAsync();
    Debug.Log($"{player.displayName} (@{player.handle}), Gamavas ID {player.id}");
}
catch (GamavasPassException e)
{
    // e.Code: popup_closed, access_denied, cancelled, timeout, network…
}
```

`player.id` is the player's Gamavas ID: the same in every game, so it's the one to store. Pass `askForEmail: true` to also ask for their email. They can decline, and then `player.email` is null.

> [!TIP]
>
> Players stay signed in between sessions. On startup, check `GamavasPass.Player`. On WebGL and mobile, `await GamavasPass.Ready` first: it finishes a sign-in that came back while the game was loading.

To sign the player out of your game (not of Gamavas), call `await GamavasPass.SignOutAsync()`.

## Achievements and XP

Add achievements to your app in the developer portal, then unlock them from your game:

```csharp theme={"theme":{"light":"github-light","dark":"github-dark"}}
UnlockResult result = await GamavasPass.UnlockAsync("first-win");
if (result.newlyUnlocked)
    ShowToast($"{result.name}: +{result.xpAwarded} XP");

List<Achievement> all = await GamavasPass.AchievementsAsync();
Progress me = await GamavasPass.ProgressAsync(); // me.level, me.title, me.xp, me.toNextLevel
```

Unlocking again is harmless: `newlyUnlocked` is false and no XP is added. On WebGL, Gamavas shows an "Achievement unlocked" notice; pass `notify: false` to show your own. On desktop and mobile, show your own.

> [!NOTE]
>
> Achievements marked **server** in the portal can't be unlocked from the game, so a modified client can't grant them. Unlock them [from your server](#your-server).

## Sell your game

Set a price on your app's **Store** page in the developer portal, then gate your game on ownership:

```csharp theme={"theme":{"light":"github-light","dark":"github-dark"}}
if (!await GamavasPass.OwnsAsync())
{
    bool bought = await GamavasPass.PurchaseAsync();
    if (!bought) return; // closed the checkout, or didn't buy
}
StartGame();
```

Players pay by card or with purchased Gamavas coins, and you get 90% of the price. On WebGL the checkout is a popup, so call `PurchaseAsync` from a click. On desktop and mobile it opens the browser and checks every few seconds until the game is owned, for up to 15 minutes.

> [!WARNING]
>
> **Mobile app stores** have their own rules for selling digital content inside apps, and buying through Gamavas's checkout can break them. Check the App Store and Google Play rules before calling `PurchaseAsync` in a store build. `OwnsAsync` is fine everywhere, so a game bought on the web still unlocks.

To try this without paying, turn on test mode: `new GamavasPassOptions { Test = true }`. The checkout then offers a free **Complete test purchase** button, for you and the testers you add on the **Store** page. Turn it off before you ship.

## Platforms

> [tab] WebGL
>
> Add the Gamavas script to your WebGL template's `<head>` (or the built `index.html`):
>
> ```html theme={"theme":{"light":"github-light","dark":"github-dark"}}
> <script src="https://gamavas.fun/sdk/pass.js"></script>
> ```
>
> The SDK loads the script itself if it's missing. In the `<head>`, though, the sign-in popup can close before Unity starts loading in it.

> [tab] Desktop
>
> Sign-in opens the player's own browser, never a web view inside the game, so players can trust the page and use their saved passwords. Gamavas sends them back to `http://127.0.0.1:<port>/` on their own computer, the game picks up the result, and the browser tab tells them they can close it.
>
> The player is remembered in `PlayerPrefs`.

> [tab] Mobile
>
> Sign-in opens the browser. Gamavas sends the player back to your app's own link, `com.studio.game:/pass?code=…`, which the system opens in your game, and the SDK finishes the sign-in. PKCE stops another app that claims the same link from using what it catches. See [Mobile setup](#mobile).

## Mobile

**1. Register the link**

In the developer portal, under **Where sign-in returns**, add your bundle ID in lower case, then `:/pass`. You'll find the bundle ID in **Player Settings → Other Settings → Package Name / Bundle Identifier**.

A bundle ID with an underscore can't be a link. In that case, choose your own: a reverse domain name you own, with one slash.

```csharp theme={"theme":{"light":"github-light","dark":"github-dark"}}
GamavasPass.Init("YOUR_CLIENT_ID", new GamavasPassOptions { RedirectUri = "com.studio.game:/pass" });
```

**2. iOS: add the URL scheme**

In **Player Settings → Other Settings → Supported URL schemes**, add the part before the colon, for example `com.studio.game`.

**3. Android: add an intent filter**

Turn on **Custom Main Manifest** in **Player Settings → Publishing Settings**. Then, in `Assets/Plugins/Android/AndroidManifest.xml`, add this inside the `<activity>` that has the `LAUNCHER` intent filter:

```xml AndroidManifest.xml theme={"theme":{"light":"github-light","dark":"github-dark"}}
<intent-filter>
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.DEFAULT" />
  <category android:name="android.intent.category.BROWSABLE" />
  <data android:scheme="com.studio.game" />
</intent-filter>
```

> [accordion] The player comes back without finishing
>
> `SignInAsync` fails with code `cancelled` about 2 seconds after the game is back in front, so your button doesn't stay stuck. If they then finish in the browser after all, the link still signs them in and `PlayerChanged` fires.

> [accordion] The phone closed the game during sign-in
>
> The sign-in is saved before the browser opens. When the link starts the game again, the SDK finishes it. `await GamavasPass.Ready` after `Init` if your first scene needs to know who's signed in.

## Your server

If your game has a server, never trust a player ID the game sends. Send the player's **access token** instead, and let your server ask Gamavas who it belongs to:

```csharp theme={"theme":{"light":"github-light","dark":"github-dark"}}
string token = await GamavasPass.GetAccessTokenAsync(); // null when signed out
```

On your server, `@gamavas/pass-server` checks it with your secret key:

```ts server.ts theme={"theme":{"light":"github-light","dark":"github-dark"}}
import { GamavasPass } from "@gamavas/pass-server";

const gamavas = new GamavasPass({ secretKey: process.env.GAMAVAS_SECRET_KEY });
const player = await gamavas.verifyPlayer(token); // only tokens issued to your game count
await gamavas.unlock(player.id, "beat-the-boss"); // server-only achievements too
```

## Options

```csharp theme={"theme":{"light":"github-light","dark":"github-dark"}}
GamavasPass.Init("YOUR_CLIENT_ID", new GamavasPassOptions
{
    Analytics = true,      // play time for your app's Analytics page; never earns XP
    Test = false,          // test purchases for you and your testers; off before you ship
    LoopbackPath = "/",    // desktop: the path on http://127.0.0.1/ that sign-in returns to
    RedirectUri = null,    // mobile: your app's link, if not "<bundle id>:/pass"
    Issuer = "https://gamavas.fun/api/auth", // change only to test against staging
});
```

## Errors

Every failure throws a `GamavasPassException` with a `Code` and Gamavas's explanation in `Message`:

| Code | When |
| - | - |
| `popup_closed` | The player closed the sign-in or checkout popup (WebGL) |
| `access_denied` | The player declined to connect your game |
| `cancelled` | The sign-in was cancelled, or the player came back without finishing (mobile) |
| `timeout` | The browser sign-in took too long (desktop) |
| `signed_out` | The call needs a signed-in player |
| `forbidden` | Not allowed, e.g. a server-only achievement |
| `not_found` | No achievement with that key |
| `rate_limited` | Too many requests: slow down |
| `network` | Gamavas couldn't be reached |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.