UnityPreview

A UPM package with an async/await API. Uses UnityWebRequest and JsonUtility, so it runs on every Unity platform including WebGL.

Install

  1. Download rean-sdk-unity-0.1.0.zip and unzip it (e.g. into Packages/com.reanstudios.sdk), or use Package Manager › Add package from disk and pick its package.json.
  2. Unity 2021.3 or newer. Import the Quick start sample from the Package Manager to see everything working.

Sign in

csharp
using Rean;

var rean = new ReanClient("rean_xxxxxxxxxxxxxxxxxxxxxxxx");

// 1. Started from the REAN Launcher?  2. Saved session?  3. Device code.
var user = await rean.SignInWithLauncherAsync();
if (user == null && !await rean.TryRestoreSessionAsync())
{
    user = await rean.SignInWithDeviceAsync(code =>
        prompt.text = $"Go to {code.VerificationUri} and enter {code.UserCode}");
}
Debug.Log($"Signed in as {rean.CurrentUser.displayName}");

The refresh token is stored with PlayerPrefsTokenStore by default. For release builds pass your own IReanTokenStore that uses the platform's secure storage. All methods must be called from the main thread; the SDK runs its timers from a hidden [REAN SDK] object.

Friends, presence and invites

csharp
foreach (var friend in await rean.GetFriendsAsync())
    AddRow(friend.displayName, friend.presence.status, friend.presence.activity?.details);

await rean.SetActivityAsync(new ReanActivity
{
    details = "Ranked", state = "Round 1",
    partyId = lobbyId, partySize = 1, partyMax = 5, joinable = true,
    startedAt = DateTimeOffset.UtcNow.ToUnixTimeSeconds(),
}); // re-sent every 60 s until ClearActivityAsync()

rean.EventReceived += async e =>
{
    if (e.type == "invite.received" && await AskPlayer($"{e.data.from.displayName} invited you"))
        JoinLobby(await rean.RespondToInviteAsync(e.data.id, accept: true));
};

await rean.SendInviteAsync(friend.id, joinSecret: lobbyId, message: "Ranked?");

Errors are thrown as ReanException with Status and Code (e.g. insufficient_scope, not_friends, network_error).