docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    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:

    1. Go to GameObject > Create Empty and name the new game object RoomManager.
    2. In your Assets/Scripts directory, create a new script and name it ProjectToRoomConverter.
    3. Attach the ProjectToRoomConverter script to your RoomManager game object.
    4. In your ProjectToRoomConverter script file, use the ProjectToRoomConverter class to reference an IRoomProvider<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;
        }
    
    1. Use the Awake method to retrieve the RoomProvider from PlatformServices.
        void Awake()
        {
            m_RoomProvider = PlatformServices.RoomProvider;        
        }  
    
    1. Use the OnProjectSelected method to create a room from the selected project. Use the RoomProvider 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:

    1. In your scene, create Join Room and Leave Room buttons.
      Screenshot of Join Room and Leave Room buttons
    2. In your Assets/Scripts directory, create a new script and name it RoomJoinManager.
    3. Attach the RoomJoinManager script to your RoomManager game object.
    4. In your RoomJoinManager script file, use the RoomJoinManager class to reference the ProjectToRoomConverter 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;
        }
    
    1. Use the Awake method to subscribe to the RoomSelected event and to the join and leave buttons onClick events. Use the OnDestroy 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();
        }
    
    1. 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;
            }
        }
    
    1. Use the OnJoinRoomClicked and OnLeaveRoomClicked 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:

    1. In your scene, add a text field to display the monitoring information.
    2. In your Assets/Scripts directory, create a new script and name it RoomInfoUpdater.
    3. Attach the RoomInfoUpdater script to your RoomManager game object.
    4. In your RoomInfoUpdater script file, use the RoomInfoUpdater class to reference the ProjectToRoomConverter 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;
        }
    
    1. Use the Awake method to subscribe to the RoomSelected event. Use the OnDestroy method to unsubscribe from the event.
        void Awake()
        {
            m_ProjectToRoom.RoomSelected += OnRoomSelected;
        }
    
        void OnDestroy()
        {
            m_ProjectToRoom.RoomSelected -= OnRoomSelected;
        }
    
    1. Use the OnRoomSelected method to store the current room, update the UI, and subscribe and unsubscribe to a room's ParticipantsChanged 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);
            }
        }
    
    1. 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();
        }
    
    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)