docs.unity3d.com
    Show / Hide Table of Contents

    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:

    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 SceneToRoomConverter.
    3. Attach the SceneToRoomConverter script to your RoomManager game object.
    4. In your SceneToRoomConverter script file, use the SceneToRoomConverter class to reference an IRoomProvider<Room> and the SceneManager 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;
       }
      
    5. Use the Awake method to retrieve services from PlatformServices and to subscribe to the SceneSelected event. Use the OnDestroy method to unsubscribe from the event.

       void Awake()
       {
           m_RoomProvider = PlatformServices.RoomProvider;
      
           m_SceneManager.SceneSelected += OnSceneSelected;
       }
      
       void OnDestroy()
       {
           m_SceneManager.SceneSelected -= OnSceneSelected;
       }
      
    6. 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:

    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 SceneToRoomConverter 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;
       }
      
    5. 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_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();
       }
      
    6. 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;
           }
       }
      
    7. 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 SceneToRoomConverter 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;
       }
      
    5. Use the Awake method to subscribe to the RoomSelected event. Use the OnDestroy method to unsubscribe from the event.

       void Awake()
       {
           m_SceneToRoom.RoomSelected += OnRoomSelected;
       }
      
       void OnDestroy()
       {
           m_SceneToRoom.RoomSelected -= OnRoomSelected;
       }
      
    6. 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);
           }
       }
      
    7. 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();
       }
      
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023