Skip to content

Unity and C#

using Game;
using Game.Stores;
using RudderSdk;
var rudder = await Rudder.SignInDevice(sdkKey, SystemInfo.deviceUniqueIdentifier);
var stores = await rudder.Stores.Stores();
await rudder.Stores.Buy(new BuyArguments { Offer = "gold-bag" });
var balances = await rudder.Session.Wallet();

The SDK is the package @rudder/unity of the Rudder package registry, https://hub.rudder.build/api/packages/rudder/npm/, which needs no account.

  • Unity 6: add the registry and the package to Packages/manifest.json:

    {
    "scopedRegistries": [
    {
    "name": "Rudder",
    "url": "https://hub.rudder.build/api/packages/rudder/npm/",
    "scopes": ["@rudder"]
    }
    ],
    "dependencies": {
    "@rudder/unity": "1.3.0"
    }
    }

    Or add the scoped registry in Project Settings → Package Manager and install the SDK from My Registries of the Package Manager. Requests go through UnityWebRequest, and their continuations stay on the main thread.

  • .NET: the same package carries Rudder.Sdk.csproj (.NET Standard 2.1); reference it from your project. Requests go through HttpClient.

The SDK has no dependencies. It reads and writes JSON without reflection, so it runs under IL2CPP and in WebGL builds. A WebGL build runs in a browser: add the origin of the page that hosts it to the Allowed origins of the environment (see Browser games).

Then, in a repository where rudder project use chose the project, generate the client into your assets:

Terminal window
rudder client generate --lang csharp --output Assets/Rudder/Rudder.g.cs \
--game-address https://api.rudder.build --namespace Game

Pass --release-version <version> to generate from the exact version returned by staging publication. Read the staging SDK key with rudder environment show --env staging --json. For WebGL, configure the page origin through the environment settings or the CLI’s environment set-origins command.

  • The class Rudder lives in the namespace you chose (Game), with a property per module (rudder.Stores) and rudder.Session, the session of the SDK.
  • The types of a module live in <namespace>.<Module> (Game.Stores.BuyArguments). Every type is named in full inside the generated code, so a module may declare a type named like another module.
  • An optional or nullable field is T? and is left out of the JSON when it is null.
  • A value of an enum is a string, with its values as constants of a static class of the same name (Rotation.Day). A client built before a value was added keeps reading it.
  • A field named like its class gets the suffix Value. The amount of an Amount is Value: new Amount { Asset = "gems", Value = 10 }.
  • A query method takes a CancellationToken; a mutation method also takes MutationOptions with the IdempotencyKey (see Errors and retries).

SignInDevice(key, deviceId, options, cancellationToken) takes RudderSdk.Options:

OptionMeaning
AddressThe address of Rudder. Required when the client was generated without --game-address.
AttemptsHow many times a request is sent before its failure is thrown, 5 by default.
TransportAn ITransport that sends the requests and waits between attempts; replace it to route or observe them.
Nickname, Avatar, Language, RegionThe profile of the player (see Sign in a device).
var rudder = await Rudder.SignInDevice(sdkKey, deviceId, new RudderSdk.Options
{
Nickname = playerName,
Language = "en-US",
});

A refused call throws RudderSdk.RudderException with Code, Message and the HTTP Status:

try
{
await rudder.Stores.Buy(new BuyArguments { Offer = offerKey });
}
catch (RudderException exception) when (exception.Code == "insufficient")
{
ShowNotEnoughGems();
}

The codes are those the module declares (Stores: not_on_sale, limit_reached, sold_out, …) and those of Rudder (Error codes).