Creating and joining rooms

Let us have a look at how to create rooms.

Creating rooms

To create a room in PUN, use the CreateRoom function:

using UnityEngine; using System.Collections; public class Example_CreateRoom : MonoBehaviour { void OnGUI() { // if we're not inside a room, let the player create a room if( PhotonNetwork.room == null ) { // if create room button clicked { // create a room called "RoomNameHere", visible to the lobby, allows other players to join, and allows up to 8 players PhotonNetwork.CreateRoom( "RoomNameHere", true, true, 8 ); } } else { // we're connected to a room, display some info such as room name, player count, etc. // disconnect from the current room // if disconnect button clicked { PhotonNetwork.LeaveRoom(); } } } ...

Get Unity Multiplayer Games now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.