Unity SDK
Package name com.aarware.relay. It uses Newtonsoft JSON (com.unity.nuget.newtonsoft-json) and includes a WebGL WebSocket plugin, so the same code runs in the editor, standalone builds, and WebGL.
Install
Add to Packages/manifest.json:
"com.aarware.relay": "<package URL from your dashboard>"
Unity pins the exact commit in Packages/packages-lock.json (hash). After the SDK changes, update that hash or Unity keeps the old code. Never edit files in Library/PackageCache.
Configure
Create a config asset: Assets → Create → Relay → Configuration.
| Field | Meaning |
|---|---|
serverUrl | wss://ws.riffserver.com |
appId | your app's id |
autoReconnect | reconnect after an unexpected drop (default on) |
reconnectMaxAttempts | attempts before giving up (default 5) |
authToken | the signed connect token, if the app requires one (set it before connecting) |
debugMode | log every message to the Console |
Wire the config into your own component with [SerializeField] RelayConfig config; in the Inspector.
Start
public class Net : MonoBehaviour {
[SerializeField] RelayConfig config;
async void Start() {
RelayManager.Instance.Initialize(config);
var client = RelayManager.Instance.Client;
client.OnMessageReceived.AddListener(OnMsg);
await client.ConnectAsync(playerId: "player-123", playerName: "Ada");
await client.JoinRoomAsync("lobby");
}
}
RelayManager is a persistent singleton (survives scene loads). If you prefer, put RelayClient on your own GameObject and call Initialize(config) yourself. Every callback runs on the Unity main thread.
Actions
| Method | Notes |
|---|---|
ConnectAsync(playerId, playerName) / ConnectCoroutine(...) | playerId defaults to a new GUID |
Disconnect() | stops auto-reconnect |
JoinRoomAsync(name, RoomOptions) | returns RoomState; times out after 5 s |
LeaveRoomAsync(), ListRoomsAsync(), GetRoomStateAsync() | wait for the reply (5 s timeout) |
SetRoomPropertiesAsync, SetPlayerPropertiesAsync | fire and forget; confirmed via callbacks |
SendToRoomAsync, SendToPlayerAsync, SendToMasterAsync | see Messaging |
UpdatePositionAsync(pos, rotation, animState) | see Gameplay |
RaiseEventAsync, SetTurnAsync, EndGameAsync | see Gameplay |
Calls made while disconnected are queued and sent when the connection opens.
State
| Property | Meaning |
|---|---|
IsConnected, IsInRoom | connection and room state |
LocalPlayerId, ConnectionId | who you are |
CurrentRoom, RoomPlayers | the room and its players |
IsMasterClient | are you the room master |
Random | the shared random generator |
Callbacks (UnityEvents on RelayClient)
OnConnected, OnDisconnected(reason), OnReconnecting(attempt), OnPlayerJoined(Player), OnPlayerLeft(Player), OnRoomPropertiesChanged(dict), OnPlayerPropertiesChanged(...), OnMessageReceived(RelayMessage), OnPositionUpdate(PositionUpdate), OnTurnChanged(playerId), OnGameEnded(data), OnError(RelayError).
Anything the SDK does not handle itself (message, directMessage, eventRaised) arrives on OnMessageReceived.
Errors
Refused actions raise OnError with a code (ROOM_FULL, NOT_HOST, ...). The awaited calls (JoinRoomAsync and friends) throw a RelayError for the same codes, or a TimeoutException after 5 seconds.
WebGL
A browser-side plugin (RelayWebSocket.jslib) carries the socket. Only code inside its mergeInto({...}) block ships in the build, so state lives on Module. Do not add top-level variables there.
Conventions this SDK expects of your game
- Wire references in the Inspector with
[SerializeField]; the SDK never searches the scene for you. - Keep your own event names in one place (an enum or constants class) so sender and receiver agree.