Use case: Manage rooms
To use the Unity Cloud Presence package, you must manage your rooms through a lobby. You can attach rooms to a valid resource: asset
, dataset
, organization
or project
. In this example the room will be attached to a project
. You can join, leave, and monitor rooms.
Before you start
Before you manage your rooms, you must first [get your organizations and project information].
How do I...?
Create and attach a room to a project
To create and attach a room to a project, 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 itProjectToRoomConverter
. - Attach the
ProjectToRoomConverter
script to your RoomManager game object. - In your
ProjectToRoomConverter
script file, use theProjectToRoomConverter
class to reference anIRoomProvider<Room>
. This script stores the current room and raises an event whenever a selected room is converted into an active room.
public class ProjectToRoomConverter : MonoBehaviour
{
IRoomProvider<Room> m_RoomProvider;
Room m_CurrentRoom;
public event Action<Room> RoomSelected;
}
- Use the
Awake
method to retrieve the RoomProvider from PlatformServices.
void Awake()
{
m_RoomProvider = PlatformServices.RoomProvider;
}
- Use the
OnProjectSelected
method to create a room from the selectedproject
. Use theRoomProvider
CreateRoomAsync
method to create the room.
async void OnProjectSelected(IProject project, IOrganization organization)
{
// Cleans up the previous room if necessary
if (m_CurrentRoom != null)
{
await m_CurrentRoom.StopMonitoringAsync();
m_CurrentRoom = null;
}
if (project == null)
{
RoomSelected?.Invoke(null);
}
else
{
m_CurrentRoom = await m_RoomProvider.CreateRoomAsync(organization.Id.ToString(), new RoomCreationParams(project.Name, project.Descriptor.ProjectId.ToString(), "project")).ConfigureAwait(true);
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 theProjectToRoomConverter
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]
ProjectToRoomConverter m_ProjectToRoom;
[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_ProjectToRoom.RoomSelected += OnRoomSelected;
m_JoinRoomButton.onClick.AddListener(new UnityEngine.Events.UnityAction(OnJoinRoomClicked));
m_LeaveRoomButton.onClick.AddListener(new UnityEngine.Events.UnityAction(OnLeaveRoomClicked));
}
void OnDestroy()
{
m_ProjectToRoom.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 theProjectToRoomConverter
and your text field. This script stores the current room.
public class RoomInfoUpdater : MonoBehaviour
{
[SerializeField]
ProjectToRoomConverter m_ProjectToRoom;
[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_ProjectToRoom.RoomSelected += OnRoomSelected;
}
void OnDestroy()
{
m_ProjectToRoom.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();
}