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/Scripts
directory, create a new script and name itSceneToRoomConverter
. - Attach the
SceneToRoomConverter
script to your RoomManager game object. In your
SceneToRoomConverter
script file, use theSceneToRoomConverter
class to reference anIRoomProvider<Room>
and theSceneManager
you 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
Awake
method to retrieve services from PlatformServices and to subscribe to theSceneSelected
event. Use theOnDestroy
method to unsubscribe from the event.void Awake() { m_RoomProvider = PlatformServices.RoomProvider; m_SceneManager.SceneSelected += OnSceneSelected; } void OnDestroy() { m_SceneManager.SceneSelected -= OnSceneSelected; }
Use the
OnSceneSelected
method 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/Scripts
directory, create a new script and name itRoomJoinManager
. - Attach the
RoomJoinManager
script to your RoomManager game object. In your
RoomJoinManager
script file, use theRoomJoinManager
class to reference theSceneToRoomConverter
and 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
Awake
method to subscribe to theRoomSelected
event and to the join and leave buttons onClick events. Use theOnDestroy
method 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
OnRoomSelected
method 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
OnJoinRoomClicked
andOnLeaveRoomClicked
methods 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/Scripts
directory, create a new script and name itRoomInfoUpdater
. - Attach the
RoomInfoUpdater
script to your RoomManager game object. In your
RoomInfoUpdater
script file, use theRoomInfoUpdater
class to reference theSceneToRoomConverter
and 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
Awake
method to subscribe to theRoomSelected
event. Use theOnDestroy
method to unsubscribe from the event.void Awake() { m_SceneToRoom.RoomSelected += OnRoomSelected; } void OnDestroy() { m_SceneToRoom.RoomSelected -= OnRoomSelected; }
Use the
OnRoomSelected
method to store the current room, update the UI, and subscribe and unsubscribe to a room'sParticipantsChanged
event.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
ApplyRoomUpdate
method 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(); }