Posted on

Observable Components with Photon 2 in Unity

For this lesson, we will teach you how to stream data across the network by making your own observable components. The Photon plugin already provides some observable components such as the Photon Transform View and the Photon Animator View. Although these premade observable components help with much of the networking in a multiplayer game there might be some additional values that you need to sync across the network.

Peer Play Tutorial

To create an observable component you need a script. You will then need to include using Photon.Pun namespace at the top. You will then need to have your script inherit from MonoBehaviourPunCallbacks and IPunObservable. You will then need a variable that you want to have synced. Once you have all of this you then need to a special callback function Which is called OnPhotonSerializeView. Inside this function, you will have an if statement for sending variable and then an else statement for receiving the variables.

Back in Unity, you need to add this new script to the observable components of the Photon View component of the object this script is attached to.