O'Reilly logo

Unity Multiplayer Games by Alan Stagner

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Sending/receiving messages

Once connected to a room, we have a connection that can be used to send and receive messages. Let's register event handlers for receiving messages, as well as disconnections.

First, we'll add methods to handle these events:

// called when we've disconnected from the room
void OnDisconnect( object sender, string message )
{
  Debug.Log( "Disconnected from room" );
}
// called when a message is received
void OnMessage( object sender, Message e )
{
}

Then we'll modify our OnGUI method to hook up these event handlers upon connection:

void OnGUI() { if( roomConnection != null ) return; if( GUILayout.Button( "Join Random", GUILayout.Width( 200f ) ) ) { client.Multiplayer.CreateJoinRoom( "$service-room$", "MyCode", true, null, null, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required