# Install on Unity

> Add the Herald Unity package to an iOS and Android game, configure it from C#, identify a user, track events, register for push and use content cards and in-app messages.

The Unity package `com.herald.sdk` is a C# API over the native iOS and Android SDKs. Each call goes to the native SDK, which queues events, runs sessions, handles push and draws in-app messages, so a Unity game behaves like a native app. In the Unity Editor, and on platforms other than iOS and Android, each call is logged to the Console and nothing is sent.

:::prereq
- Unity 6 (6000.0) or later, with the IL2CPP scripting backend.
- iOS: **Player Settings > iOS > Target minimum iOS Version** set to 16.0 (the build stops with a message otherwise) and Xcode 16.
- Android: minimum API level 26, and **Player Settings > Android > Application Entry Point** set to **GameActivity**. The native in-app message views need it; with the legacy Activity entry point, in-app messages wait and a warning is logged.
- A Herald sending connection with an app key, from the **Mobile SDK** card under **Settings > Sending**.
:::

## 1. Add the package

In Unity, open **Window > Package Manager**, click **+**, choose **Add package from git URL** and enter:

```
https://github.com/ender1311/herald.git?path=sdks/unity
```

Or add it to `Packages/manifest.json`, with `#<commit or tag>` on the end to pin a version:

```json
"com.herald.sdk": "https://github.com/ender1311/herald.git?path=sdks/unity"
```

The repository holding the package is private for now, so the URL works only for accounts with access to it. A public package will replace it; this page will change to that URL.

The package needs no External Dependency Manager and no custom Gradle templates. When you build:

- **iOS**: it adds the `herald-ios` Swift package (`HeraldSDK` and `HeraldUI`) to the UnityFramework target of the generated Xcode project and turns on Unity's push token forwarding.
- **Android**: it adds JitPack to `settings.gradle` and the `herald-core`, `herald-ui` and `herald-fcm` modules to the generated Gradle project.

## 2. Configure at launch

Call `Herald.Configure` once, as early as you can, from Unity's main thread:

```csharp
using HeraldSDK;
using UnityEngine;

public static class HeraldSetup
{
    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
    static void Start()
    {
        Herald.Configure("YOUR_APP_KEY", "https://YOUR_HERALD_HOST", new HeraldOptions
        {
            LogLevel = HeraldLogLevel.Debug, // remove before release
        });
    }
}
```

`HeraldOptions` has `DeviceDataCollection`, `LogLevel`, `FlushIntervalSeconds`, `FlushAt`, `InAppMinimumIntervalSeconds`, `TrackLifecycleEvents` and `TrackDeepLinks`, with the native defaults. See [iOS API](/docs/reference/ios-api#setup) for what each does.

## 3. Identify the player and track events

```csharp
using System.Collections.Generic;

Herald.Identify("user-123", new Dictionary<string, object> { { "plan", "free" } });
Herald.SetUserToken(tokenFromYourBackend); // unless identity verification is Off
Herald.SetEmail("player@example.com");
Herald.Track("level_completed", new Dictionary<string, object> { { "level", 3 }, { "stars", 2 } });
Herald.Screen("shop");
Herald.TrackPurchase("gem_pack", 4.99, "USD");
```

Values can be strings, numbers, booleans, null, lists and dictionaries; attributes take strings, numbers, booleans, null or a list of strings. Call `Herald.Reset()` when the player signs out. `Herald.HeraldId` is the player's User ID for a Support ID line. See [Users and identity](/docs/concepts/users-and-identity).

## 4. Push

Call `Herald.RequestPushAuthorization()` at a moment that makes sense to the player. It shows the permission prompt (iOS, and Android 13 and later) and registers the device. The package handles taps itself: it logs the open and opens the push's deep link. Links with your app's own scheme arrive in Unity's `Application.deepLinkActivated`.

- **iOS**: in Xcode, add the **Push Notifications** capability to the Unity-iPhone target, and upload an APNs key to the connection. See [iOS push setup](/docs/push-notifications/ios-setup). The package installs a notification center delegate as the app launches; notifications that aren't from Herald, and how a notification shows while the game is open, are passed to any delegate that was set before it (another plugin's). With none, a notification shows as a banner while the game is open. Another plugin that sets the notification center delegate after launch, such as Unity Mobile Notifications, replaces the package's, and Herald push taps are then neither logged nor opened: forward each response from that delegate to `Herald.shared.handleNotificationResponse(response)` in native code (it returns `false` for notifications that aren't from Herald).
- **Android**: Firebase must be set up in the game. Add the Firebase Unity SDK's **FirebaseApp** package with your `google-services.json`, but not its **Firebase Messaging** package: `herald-fcm` brings its own messaging service, and only one service receives each push. See [Android push setup](/docs/push-notifications/android-setup) for the Firebase project and server key.

Silent sync pushes don't refresh messages in the background in a Unity game; new cards and messages arrive at the next session or `Herald.RefreshContentCards()`.

## 5. Content cards

The package keeps the current cards in `Herald.ContentCards` and raises `Herald.ContentCardsChanged` on Unity's main thread when they change. Draw them in your own UI and report what the player does:

```csharp
Herald.ContentCardsChanged += cards =>
{
    foreach (var card in cards)
    {
        // card.Title, card.Body, card.ImageUrl, card.Url, card.Extras ...
        Herald.LogImpression(card); // when it is on screen
    }
};
// Herald.LogClick(card) when tapped (then open card.Url), Herald.LogDismissal(card) when dismissed
```

## 6. In-app messages

The native SDK draws in-app messages over the game with its default views. To hold them during gameplay, set a delegate:

```csharp
class MessageGate : IInAppMessageDelegate
{
    public static volatile bool InLevel;

    public InAppMessageDisplayChoice ShouldDisplay(InAppMessage message) =>
        InLevel ? InAppMessageDisplayChoice.Later : InAppMessageDisplayChoice.Display;
}

Herald.InAppMessageDelegate = new MessageGate();
// when the level ends:
MessageGate.InLevel = false;
Herald.ShowNext();
```

On Android, `ShouldDisplay` runs on the Android UI thread, not Unity's main thread: read a flag, don't call Unity APIs, and return quickly. `Later` keeps the message pending; `Discard` drops this showing. See [In-app messages](/docs/in-app-messages/overview).

## What the package covers

| C# | Native |
|---|---|
| `Configure` | `configure`, plus the default in-app presenter |
| `Identify`, `SetUserToken`, `Reset`, `HeraldId` | same names |
| `Track`, `Screen`, `TrackPurchase` | same names |
| `SetAttribute`, `SetAttributes`, `SetEmail`, `SetPhone` | same names |
| `RequestPushAuthorization` | same name; taps handled for you |
| `Flush`, `SetEnabled`, `IsEnabled`, `WipeData` | same names |
| `ContentCards`, `ContentCardsChanged`, `RefreshContentCards`, `LogImpression`, `LogClick`, `LogDismissal` | content card store and logging |
| `InAppMessageDelegate`, `ShowNext` | same names |

Not in the Unity package yet: `setSubscription`, `setLocation`, consent, `trackExternalPush`, custom in-app message views, a deep link handler, Live Activities, push stories and geofences. They work from native code in the generated Xcode or Gradle project.

## Related

- [Platform support](/docs/overview/platform-support)
- [iOS API](/docs/reference/ios-api)
- [Android API](/docs/reference/android-api)
