Manage rooms
To use the Presence package, you must manage your rooms through a lobby. You can attach rooms to a Unity Cloud Unity scene, join and leave rooms, and monitor rooms.
Prerequisites
Before you manage your rooms, you must first get your workspace and scene information.
Create and attach a room to a scene
To create and attach a room to a scene, see the following steps:
- Go to GameObject > Create Empty and name the new game object RoomManager.
- In your
Assets/Scriptsdirectory, create a new script and name itSceneToRoomConverter. - Attach the
SceneToRoomConverterscript to your RoomManager game object. In your
SceneToRoomConverterscript file, use theSceneToRoomConverterclass to reference anIRoomProvider<Room>and theSceneManageryou created in the prerequisites. This script stores the current room and raises an event whenever a selected room is converted into an active room.public class SceneToRoomConverter : MonoBehaviour { [SerializeField] SceneManager m_SceneManager; IRoomProvider<Room> m_RoomProvider; Room m_CurrentRoom; public event Action<Room> RoomSelected; }Use the
Awakemethod to retrieve services from PlatformServices and to subscribe to theSceneSelectedevent. Use theOnDestroymethod to unsubscribe from the event.void Awake() { m_RoomProvider = PlatformServices.RoomProvider; m_SceneManager.SceneSelected += OnSceneSelected; } void OnDestroy() { m_SceneManager.SceneSelected -= OnSceneSelected; }Use the
OnSceneSelectedmethod to create a room from the selected scene, call the build event, and let the room receive network events.async void OnSceneSelected(IScene scene) { // Cleans up the previous room if necessary if (m_CurrentRoom != null) { await m_CurrentRoom.StopMonitoringAsync(); m_CurrentRoom = null; } if (scene == null) { RoomSelected?.Invoke(null); } else { m_CurrentRoom = await m_RoomProvider.GetRoomAsync(scene.Id); await m_CurrentRoom.StartMonitoringAsync(); RoomSelected?.Invoke(m_CurrentRoom); } }
Join and leave rooms
Note: you can only join one room at a time.
To join or leave a room, see the following steps:
- In your scene, create Join Room and Leave Room buttons.

- In your
Assets/Scriptsdirectory, create a new script and name itRoomJoinManager. - Attach the
RoomJoinManagerscript to your RoomManager game object. In your
RoomJoinManagerscript file, use theRoomJoinManagerclass to reference theSceneToRoomConverterand your join and leave buttons. This script stores the current room and describes whether a participant has joined the current room or not.public class RoomJoinManager : MonoBehaviour { [SerializeField] SceneToRoomConverter m_SceneToRoom; [SerializeField] Button m_JoinRoomButton; [SerializeField] Button m_LeaveRoomButton; Room m_CurrentRoom; bool m_HasJoined; }Use the
Awakemethod to subscribe to theRoomSelectedevent and to the join and leave buttons onClick events. Use theOnDestroymethod to unsubscribe from the events.void Awake() { m_SceneToRoom.RoomSelected += OnRoomSelected; m_JoinRoomButton.onClick.AddListener(new UnityEngine.Events.UnityAction(OnJoinRoomClicked)); m_LeaveRoomButton.onClick.AddListener(new UnityEngine.Events.UnityAction(OnLeaveRoomClicked)); } void OnDestroy() { m_SceneToRoom.RoomSelected -= OnRoomSelected; m_JoinRoomButton.onClick.RemoveAllListeners(); m_LeaveRoomButton.onClick.RemoveAllListeners(); }Use the
OnRoomSelectedmethod to store the current room, update the UI, and clean up any room you previously joined.async void OnRoomSelected(Room room) { m_JoinRoomButton.interactable = false; m_LeaveRoomButton.interactable = false; // Cleans up any previously joined room if (m_CurrentRoom != null && m_HasJoined) { await m_CurrentRoom.LeaveAsync(); m_CurrentRoom = null; } // Sets up the current room if (room != null) { m_CurrentRoom = room; m_HasJoined = false; m_JoinRoomButton.interactable = true; } }Use the
OnJoinRoomClickedandOnLeaveRoomClickedmethods to call the Join and Leave methods in the current room and update the UI.async void OnJoinRoomClicked() { m_JoinRoomButton.interactable = false; m_LeaveRoomButton.interactable = false; await m_CurrentRoom.JoinAsync(); m_HasJoined = true; m_LeaveRoomButton.interactable = true; } async void OnLeaveRoomClicked() { m_JoinRoomButton.interactable = false; m_LeaveRoomButton.interactable = false; await m_CurrentRoom.LeaveAsync(); m_HasJoined = false; m_JoinRoomButton.interactable = true; }
(Optional) Monitor a room
You can monitor the network events happening in multiple rooms whether you have joined those rooms or not.
To monitor a room, see the following steps:
- In your scene, add a text field to display the monitoring information.
- In your
Assets/Scriptsdirectory, create a new script and name itRoomInfoUpdater. - Attach the
RoomInfoUpdaterscript to your RoomManager game object. In your
RoomInfoUpdaterscript file, use theRoomInfoUpdaterclass to reference theSceneToRoomConverterand your text field. This script stores the current room.public class RoomInfoUpdater : MonoBehaviour { [SerializeField] SceneToRoomConverter m_SceneToRoom; [SerializeField] Text m_RoomInfoText; Room m_CurrentRoom; }Use the
Awakemethod to subscribe to theRoomSelectedevent. Use theOnDestroymethod to unsubscribe from the event.void Awake() { m_SceneToRoom.RoomSelected += OnRoomSelected; } void OnDestroy() { m_SceneToRoom.RoomSelected -= OnRoomSelected; }Use the
OnRoomSelectedmethod to store the current room, update the UI, and subscribe and unsubscribe to a room'sParticipantsChangedevent.void OnRoomSelected(Room room) { if (m_CurrentRoom != null) m_CurrentRoom.ParticipantAdded -= ApplyRoomUpdate; m_CurrentRoom.ParticipantRemoved -= ApplyRoomUpdate; m_CurrentRoom = room; if (room == null) { m_RoomInfoText.text = "Not monitoring any room"; } else { m_CurrentRoom.ParticipantAdded += ApplyRoomUpdate; m_CurrentRoom.ParticipantRemoved += ApplyRoomUpdate; ApplyRoomUpdate(m_CurrentRoom); } }Use the
ApplyRoomUpdatemethod to read the room properties and update the text field.void ApplyRoomUpdate(IParticipant participant) { ApplyRoomUpdate(m_CurrentRoom); } void ApplyRoomUpdate(Room room) { var sb = new StringBuilder(); sb.AppendLine($"RoomId : {room.RoomId}"); sb.AppendLine($"Nb participants : {room.ConnectedParticipants.Count}"); foreach (var machin in room.ConnectedParticipants) { sb.AppendLine($"- {machin.Name}"); } m_RoomInfoText.text = sb.ToString(); }