Basic Photon Guide

Michael Cosgrove, April 2021

Starting out, it's pretty handy to make a new object that will be responsible for handling some network setup. You can call this something like "Network Manager." Generally, this can be used to setup the room, instantiate new players, debugging, and handling any other callbacks you might need. In order to do this, you need to add a component that inherits from "MonoBehaviorPunCallbacks". Below is some boilerplate code:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using Photon.Pun;

using Photon.Realtime;


public class NetworkManager : MonoBehaviourPunCallbacks

{

// This is a prefab representing the player. It needs to be in Assets/Resources and have

// a PhotonView component attached to it. In addition, you will probably want to add Photon Transform

// View components to the object's children (e.g., the meshes representing the avatar).

private GameObject spawnedPlayerPrefab;


// Start is called before the first frame update

void Start()

{

ConnectToServer();

}


void ConnectToServer()

{

PhotonNetwork.ConnectUsingSettings();

Debug.Log("Trying to connect to server...");

}


public override void OnConnectedToMaster() {


Debug.Log("Connected to server.");

base.OnConnectedToMaster();

// Here are some default room options

RoomOptions roomOptions = new RoomOptions();

roomOptions.MaxPlayers = 10;

roomOptions.IsVisible = true;

roomOptions.IsOpen = true;

PhotonNetwork.JoinOrCreateRoom("Room 1", roomOptions, TypedLobby.Default);

}


public override void OnJoinedRoom()

{

Debug.Log("Joined a room.");

base.OnJoinedRoom();

}


public override void OnPlayerEnteredRoom(Player newPlayer)

{

Debug.Log("A new player joined the room.");

base.OnPlayerEnteredRoom(newPlayer);

}

public override void OnJoinedRoom() {

base.OnJoinedRoom();

spawnedPlayerPrefab = PhotonNetwork.Instantiate("Network Player", transform.position, transform.rotation);

}

public override void OnLeftRoom()

{

base.OnLeftRoom();

PhotonNetwork.Destroy(spawnedPlayerPrefab);

}

}

Photon View

This is the component responsible for synchronization between clients. Usually this will be attached to a parent object where its children will have other Photon components that can be "observed" by this object (usually they will be responsible for synchronizing other information, like transforms), but this also could be used to see which player "owns" an object via the "isMine" attribute. There are some cases where you may want to transfer ownership information (e.g., if multiple people can move an object). To do this, you can change the Ownership Transfer attribute in the Unity Editor to "Takeover", which lets clients call the RequestOwnership method on the Photon View component to take it over.


Photon Transform View

This is a component that is responsible for synchronizing transformation information. For instance, you might have a Player object with a head, left hand, and right hand child objects. The Player should have a Photon View component attached to it, and the head and hands should have Photon Transform Views. Then you should check to see that the Photon View component of the Player is observing these Transform Views.

Photon Rigidbody View

This is pretty similar to the transform view, except there appears to be some optimization in that physics is done on the client-side, but it will check if the object instances on clients are too out of sync and will update them based on a set threshold. Generally, if one is using an object with rigidbody physics that needs to be synchronized, this component should be used.

Please note that the main source for this guide is the YouTube tutorial series linked below. If anything in this guide is unclear, I highly recommend you go through the first two parts of this series: