SWNetwork.Lobby has two messaging APIs to support player to player communication. Both APIs support serializable objects, so they are very flexible to use. You can use them to implement your own communication protocol.
Player to Player Message
In the LobbyDemo.cs script and add the following code to the OnPlayerSelected(string) method to send messages to the selected player.
publicvoidOnPlayerSelected(string playerId){Debug.Log("OnPlayerSelected: "+ playerId); // demonstrate player message APIGUI.ShowMessagePlayerPopup(playerId, (bool ok,string targetPlayerId,string message) => {if (ok) {Debug.Log("Send player message "+"playerId= "+ targetPlayerId +" message= "+ message);NetworkClient.Lobby.MessagePlayer(playerId, message, (bool successful,SWLobbyError error) => {if (successful) {Debug.Log("Sent player message");string msg ="Sent to "+ targetPlayerId +": "+ message;GUI.AddRowForMessage(msg,null,null); }else {Debug.Log("Failed to send player messagem "+ error); } }); } });}
To receive the message, you need to subscribe to the OnPlayerMessageEvent lobby event. Update the Start() and onDestroy() methods to subscribe to the event.
voidStart(){ // Subscribe to Lobby eventsNetworkClient.Lobby.OnNewPlayerJoinRoomEvent+= Lobby_OnNewPlayerJoinRoomEvent;NetworkClient.Lobby.OnPlayerLeaveRoomEvent+= Lobby_OnPlayerLeaveRoomEvent;NetworkClient.Lobby.OnRoomCustomDataChangeEvent+= Lobby_OnRoomCustomDataChangeEvent;NetworkClient.Lobby.OnPlayerMessageEvent+= Lobby_OnPlayerMessageEvent;}voidonDestroy(){ // Unsubscrible to Lobby eventsNetworkClient.Lobby.OnNewPlayerJoinRoomEvent-= Lobby_OnNewPlayerJoinRoomEvent;NetworkClient.Lobby.OnPlayerLeaveRoomEvent-= Lobby_OnPlayerLeaveRoomEvent;NetworkClient.Lobby.OnRoomCustomDataChangeEvent-= Lobby_OnRoomCustomDataChangeEvent;NetworkClient.Lobby.OnPlayerMessageEvent-= Lobby_OnPlayerMessageEvent;}
Implement the OnPlayerMessageEvent handler method.
To receive the message, you need to subscribe to the OnPlayerMessageEvent lobby event. Update the Start() and onDestroy() methods to subscribe to the event.