Unity SDK
Unity quickstart for the amba C# SDK — UPM install via git URL or tarball, Amba.ConfigureAsync(), Amba.Events.TrackAsync(), and WebGL events-only caveat.
The amba Unity SDK is distributed as a UPM (Unity Package Manager) package: com.layers.amba. It bundles native amba binaries for every supported Unity target (iOS, Android × 4 ABIs, macOS, Windows, Linux) plus a C# wrapper (Amba.cs).
Supported runtime: Unity 2022.3+ LTS (Mono and IL2CPP). .NET Standard 2.1.
Supported platforms: iOS, Android, macOS, Windows, Linux. WebGL is events-only — the underlying filesystem persistence layer doesn't exist in the browser sandbox, so auth, collections, storage, and push are unavailable. Events are buffered in memory and flushed at exit.
1. Install via Package Manager
Option A — git URL
In the Unity Package Manager (Window → Package Manager) → + → Add package from git URL → paste:
Option B — manifest.json
Edit Packages/manifest.json:
Option C — local tarball (offline / private mirrors)
Download the com.layers.amba-0.1.0.tgz tarball from the releases page, drop it in Packages/, and reference it:
The native binaries land under Packages/com.layers.amba/Plugins/<target>/ and Unity's Plugin Importer flags them for the correct target automatically.
2. Configure on startup
Add a bootstrap MonoBehaviour to your initial scene (or use [RuntimeInitializeOnLoadMethod]):
Don't ship a hard-coded key in source. Read it from a secrets ScriptableObject, an environment-driven build define, or your secrets framework (Unity Cloud Build supports env-driven defines via [Conditional]).
3. First sign-in (anonymous)
Events.TrackAsync is authenticated server-side — the request needs a session token. Mint one anonymously on first launch:
The response is a System.Text.Json.JsonElement — read fields directly with GetProperty(...), or deserialize through your own POCOs with JsonSerializer.Deserialize<MyType>(session.GetRawText()). The session token persists across game launches.
4. First event
Once a session exists, track gameplay events:
5. Sign in with Apple (iOS) / Google (Android)
Unity doesn't ship platform OAuth flows; use a third-party plugin and forward the resulting tokens.
For Sign in with Apple on iOS, the Apple Authentication Asset Store package provides the identity token:
For Sign in with Google on Android, use Google Play Games Services for Unity and forward idToken:
(Same signInWithSocial server endpoint as every other SDK — the wrapper API is Amba.Auth.SignInWithGoogleAsync(idToken).)
6. Register for push
Use Unity's Mobile Notifications package to obtain the device token, then forward to amba:
7. First collection insert
For typed reads, deserialize through your own POCOs:
Server availability:
Collections.FindAsync(the read-list endpoint) is currently landing on staging — the call shape above is correct, but expect a 404 until the route ships.InsertAsync/FindOneAsync/UpdateAsync/DeleteAsyncround-trip today.
8. AI proxy
Server availability: the SDK posts
/v1/client/ai/anthropic/messageswith{ prompt_slug, ... }; the current staging server mounts/v1/client/ai/prompts/:name/invokewith{ context?, messages?, max_tokens? }. The two sides are converging — until they align, this call returns 404 / 400. The prompt slug catalog is shared with the rest of the SDKs once the contract lands.
WebGL caveat (events only)
WebGL builds use a different code path:
Amba.Events.TrackAsyncworks — events are buffered in memory and flushed at exit (or by an explicitAmba.Events.FlushAsyncif you add one).Amba.Auth.*,Amba.Collections.*,Amba.Storage.*,Amba.Push.*throwNotSupportedExceptionbecause the underlying Rust persistence layer requires a filesystem.
The underlying reason is that the WebGL Player runs in an emscripten-compiled WebAssembly sandbox with no localStorage-equivalent that amba can write to safely across sessions. For full surface in the browser, use @layers/amba-web instead and bridge from your WebGL game via Unity → JavaScript interop.
Constructor DI for tests
AmbaClient accepts an INativeMethods via the constructor — production code goes through Amba.ConfigureAsync(...) which wires DefaultNativeMethods (native binding).
IL2CPP + AOT considerations
The C# wrapper uses System.Text.Json — fully AOT-compatible under IL2CPP from Unity 2022.3 onward. No link.xml preserves are needed for the SDK itself; the included Layers.Amba.asmdef keeps stripping conservative.
If you've enabled aggressive code stripping (managed stripping level High), add a small link.xml:
Common pitfalls
Amba.ConfigureAsyncnot awaited before firstAmba.*access throwsInvalidOperationException("Amba.ConfigureAsync must be called before accessing Amba.*"). Configure in a bootstrap MonoBehaviour withDontDestroyOnLoad.- DllNotFoundException: amba_core at runtime — the platform-specific native library wasn't packaged. In the Plugin Importer, verify each
Plugins/<target>/libamba_core.*has the matching target checkbox enabled. - WebGL build fails with "cannot find amba_core" — expected. WebGL doesn't ship
libamba_core; use the events-only code path and gate non-events calls behind#if !UNITY_WEBGL. - iOS bitcode warning — if your Xcode project has bitcode enabled (default in Unity 2022.3 LTS is off), strip it before archiving. The shipped
libamba_core.ais bitcode-free.
See also
- Client API reference — HTTP endpoint reference for every namespace.
- Code samples — same operations side-by-side with the other 7 SDKs.
- Push notifications — campaign API, scheduling, fan-out.
- SDK source:
layers/amba-sdks → unity/.