This document is about: PUN 1
SWITCH TO

PUN Classic (v1), PUN 2 and Bolt are in maintenance mode. PUN 2 will support Unity 2019 to 2022, but no new features will be added. Of course all your PUN & Bolt projects will continue to work and run with the known performance in the future. For any upcoming or new projects: please switch to Photon Fusion or Quantum.

Introduction

Get Started

Photon Unity Networking (PUN) is a Unity package for multiplayer games. Flexible matchmaking gets your players into rooms where objects can be synced over the network. RPCs, Custom Properties or "low level" Photon events are just some of the features. The fast and (optionally) reliable communication is done through dedicated Photon server(s), so clients don't need to connect one to one.

PUN exports to basically all platforms supported by Unity and comes in two flavors:

PUN FREE

pun free No-cost package with various demos, pre-made scripts and reference documentation. Exports to basically all platforms.

Get PUN FREE

PUN PLUS

pun free unity plugin Same content as PUN FREE, plus a 100 concurrent user plan (about 40k monthly active) for the Photon Cloud (valid 12 months).

Get PUN PLUS

Some Code Required

To get the best out of PUN, you will need to do some programming. This page shows you several important code snipptet but it's more of an overview than a guide.

To get properly started, work through the "PUN Basics Tutorial".

Connect and Callbacks

C#

    PhotonNetwork.ConnectUsingSettings("v1");

ConnectUsingSettings sets your client’s game version and uses the PhotonServerSettings for everything else. When you run this, PUN uses callbacks to let you know when the client established the connection, joined a room, etc.. Just like Unity's "magic methods", you only need to look up the callback method names and implement them. For example: OnConnectedToMaster.

Matchmaking

Within OnConnectedToMaster you could try to join an existing room or create your own. The following code snippets show possible method calls to start or join games.

C#

    //Join room "someRoom"
    PhotonNetwork.JoinRoom("someRoom");
    //Fails if there are no open games. Error callback: OnPhotonJoinRoomFailed

C#

    //Tries to join any random game:
    PhotonNetwork.JoinRandomRoom();
    //Fails if there are no open games. Error callback: OnPhotonRandomJoinFailed

C#

    //Create this room.
    PhotonNetwork.CreateRoom("MyMatch");
    // Fails if "MyMatch" room already exists and calls: OnPhotonCreateRoomFailed

When friends want to play together and have a way to communicate outside of PUN (e.g. with Photon Chat, Facebook), they can make up a room name and use JoinOrCreateRoom. If nobody else should be matched into this room, make it invisible for matchmaking:

C#

    RoomOptions roomOptions = new RoomOptions();
    roomOptions.IsVisible = false;
    roomOptions.MaxPlayers = 4;
    PhotonNetwork.JoinOrCreateRoom(nameEveryFriendKnows, roomOptions, TypedLobby.Default);

With JoinOrCreateRoom, the room gets created if it doesn't exist, so it doesn't matter who is first. If it's full, OnPhotonJoinRoomFailed gets called (if you implemented it somewhere).

Read more about matchmaking in our guide.

Game Logic

GameObjects can be Instantiated as "networked GameObjects", which have a PhotonView component. This identifies the object and the owner (or controller). The player who's in control, updates everyone else. Continuous updates can be sent by attaching a script into the Observed field of a PhotonView. The script must implement OnPhotonSerializeView like this:

C#

    // in an "observed" script:
    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if (stream.isWriting)
        {
            Vector3 pos = transform.localPosition;
            stream.Serialize(ref pos);
        }
        else
        {
            Vector3 pos = Vector3.zero;
            stream.Serialize(ref pos);  // pos gets filled-in. must be used somewhere
        }
    }

Clients can do Remote Procedure Calls on specific networked objects for anything that happens infrequently:

C#

    // defining a method that can be called by other clients:
    [PunRPC]
    public void OnAwakeRPC(byte myParameter)
    {
        //Debug.Log(string.Format("RPC: 'OnAwakeRPC' Parameter: {0} PhotonView: {1}", myParameter, this.photonView));
    }

C#

    // calling the RPC somewhere else
    photonView.RPC("OnAwakeRPC", PhotonTargets.All, (byte)1);

Independent from GameObjects, you can also send your own events:

C#

    PhotonNetwork.RaiseEvent(eventCode, eventContent, sendReliable, raiseEventOptions);

Read more about PUN's RPCs and RaiseEvent.

Demos and Tutorials

In the PUN packages, you find several demos and useful scripts, which you can reuse and or dissect and redo.

To get properly started, take your time to read and code the "PUN Basics Tutorial".

Back to top