Managing teams of a room is a little complicated. You need to subscribe to a few lobby events and modify the room custom data. Here is a quick rundown of the workflow:
Subscribe to the lobby events.
Room owner should assign new players who joined the room to a team.
Room owner should remove players who left the room from all teams.
All room members should refresh the players list when room custom data is changed to reflect the updated teams.
Only the room owner can change the room custom data.
Open the LobbyDemo.cs script and add the following code to Start() and onDestroy() to subscribe to the lobby events.
voidStart(){ // Subscribe to Lobby eventsNetworkClient.Lobby.OnNewPlayerJoinRoomEvent+= Lobby_OnNewPlayerJoinRoomEvent;NetworkClient.Lobby.OnPlayerLeaveRoomEvent+= Lobby_OnPlayerLeaveRoomEvent;NetworkClient.Lobby.OnRoomCustomDataChangeEvent+= Lobby_OnRoomCustomDataChangeEvent;}voidonDestroy(){ // Unsubscrible to Lobby eventsNetworkClient.Lobby.OnNewPlayerJoinRoomEvent-= Lobby_OnNewPlayerJoinRoomEvent;NetworkClient.Lobby.OnPlayerLeaveRoomEvent-= Lobby_OnPlayerLeaveRoomEvent;NetworkClient.Lobby.OnRoomCustomDataChangeEvent-= Lobby_OnRoomCustomDataChangeEvent;}
Next, Implement the OnNewPlayerJoinRoomEvent handler method.
voidLobby_OnNewPlayerJoinRoomEvent(SWJoinRoomEventData eventData){Debug.Log("Player joined room");Debug.Log(eventData); // Store the new playerId and player name pairplayersDict[eventData.newPlayerId] =eventData.GetString();if (NetworkClient.Lobby.IsOwner) { // Find the smaller team and assign the new player to it.if(roomData.team1.players.Count<roomData.team2.players.Count) {roomData.team1.players.Add(eventData.newPlayerId); }else {roomData.team2.players.Add(eventData.newPlayerId); } // Update the room custom dataNetworkClient.Lobby.ChangeRoomCustomData(roomData, (bool successful,SWLobbyError error) => {if (successful) {Debug.Log("ChangeRoomCustomData successful");RefreshPlayerList(); }else {Debug.Log("ChangeRoomCustomData failed: "+ error); } }); }}
Implement the OnPlayerLeaveRoomEvent handler method.
voidLobby_OnPlayerLeaveRoomEvent(SWLeaveRoomEventData eventData){Debug.Log("Player left room: "+ eventData);if (NetworkClient.Lobby.IsOwner) { // Remove the players from both team.roomData.team2.players.RemoveAll(eventData.leavePlayerIds.Contains);roomData.team1.players.RemoveAll(eventData.leavePlayerIds.Contains); // Update the room custom dataNetworkClient.Lobby.ChangeRoomCustomData(roomData, (bool successful,SWLobbyError error) => {if (successful) {Debug.Log("ChangeRoomCustomData successful");RefreshPlayerList(); }else {Debug.Log("ChangeRoomCustomData failed: "+ error); } }); }}
Implement the OnRoomCustomDataChangeEvent handler method.
voidLobby_OnRoomCustomDataChangeEvent(SWRoomCustomDataChangeEventData eventData){Debug.Log("Room custom data changed: "+ eventData);SWRoom room =NetworkClient.Lobby.RoomData; roomData =room.GetCustomData<RoomCustomData>(); // Room custom data changed, refresh the player list.RefreshPlayerList();}