Unity (C#) API

Unity (C#) API

InsertCoin(callback, InitOptions)

Tell Playroom to start! Playroom Kit will then handle room creation, players joining and also let players pick their names, colors and avatars. Once host taps "Launch", the promise resolves. At this point you can start your game.

// Show Playroom UI, let it handle players joining etc and wait for host to tap "Launch"
PlayroomKit.InsertCoin(() =>
{
    Debug.Log("Insert Coin Callback Fired! Time to play.");
}, options);
 
// Start your game!

InitOptions is an optional parameter with the following properties:

optiontypedefaultexplanation
streamModebooleanfalseIf true , Playroom will start in stream mode .
allowGamepadsbooleanfalseIf true , Playroom will let players play game using gamepads connected to the stream device itself. This requires streamMode to also be true.

The gamepads need to be connected to device where stream screen is running. No phones are required in this mode but are optionally allowed as an alternative controller if there aren't enough physical gamepads in the room. Players who join via phone in this mode see an on-screen Joystick.
baseUrlstringCurrent Page URLThe base URL used for generating room link that host shares with other players.

GetState<T>(string key): T

Returns the current value of the given key in the game state.

ℹ️

Since C# is a static-typed language, you would have to mention the data type of the variable in place of T.

string winnerId = PlayroomKit.GetState<string>('winner');

SetState(string key, T value, bool reliable = false): void

Sets the value of the given key in the game state. If reliable is true, the state is synced reliably to all players via Websockets. This is useful for game state that is critical to the game, like the winner.

If reliable is false, the state is synced via WebRTC, which is faster but less reliable. This is useful for game state that is not critical to the game, like the player's current position (you can always rely on next position update).

ℹ️

Currently tested for the following types: string, int, bool, float and Dictionary<string, T>.

PlayroomKit.SetState('winner', 'player1');

OnPlayerJoin(callback)

Register a callback that will be called when a new player joins the room. The callback will be called with the player's PlayroomKit.Player object.

If a new player joins after the game has started, the callback is called with all existing PlayroomKit.Player objects, exclusively for the new player.

PlayroomKit.OnPlayerJoin((player) => {
  Debug.Log($"{player.id} joined!");
});

IsHost()

Returns true if the current player is the host.

if (PlayroomKit.IsHost()) {
  // Do something only the host should do, like reading player input and setting game state
}

IsStreamScreen()

Returns true if the current screen is the stream screen; a non-player screen that is shown when the game is in stream mode .

if (PlayroomKit.IsStreamScreen()) {
  // Do something only the stream screen should do, like showing a countdown
}

MyPlayer() or Me()

Returns the current player's PlayroomKit.Player object.

PlayroomKit.Player player = PlayroomKit.MyPlayer();
Debug.Log($"Hello {player.GetProfile().name}!");

GetPlayer(id)

Returns a player's PlayroomKit.Player object by their ID.

PlayroomKit.Player player = PlayroomKit.GetPlayer("abcd1234");
Debug.Log($"Hello {player.GetProfile().name}!");

PlayroomKit.Player

A PlayroomKit.Player object represents a player in the room. In the Unity SDK, this is usually retrieved through OnPlayerJoin, MyPlayer or GetPlayer.

It has the following methods:

GetProfile(): PlayroomKit.Player.Profile

Returns the player's profile, which has the following properties:

propertytypeexplanation
namestringThe player's name.
colorUnityEngine.Color (opens in a new tab)The player's color.
photostringThe player's avatar. This is a dataURL to an image.

GetState<T>(string key): T

Returns the value of the given key in the player's state.

ℹ️

Since C# is a static-typed language, you would have to mention the data type of the variable in place of T.

int score = player.GetState<int>('score');

SetState(string key, T value, bool reliable = false)

Sets the value of the given key in the player's state.

player.SetState('score', 10);

OnQuit(callback)

Register a callback that will be called when the player quits the room.

player.OnQuit(() => {
  Debug.Log($"{player.id} quit!");
});