Using PhotonViews
Photon Unity Networking features PhotonViews. These essentially act exactly as their Unity Networking counterpart, allowing an object to serialize its state and send RPCs over the network. Nearly every part of PhotonView is equivalent to Network View, with a few script differences.
If you want to get the PhotonView for an object, you have two options: you can either call the static PhotonView.Get
method, use GetComponent<PhotonView>()
, or you can have your script inherit from Photon.MonoBehaviour
and use this.photonView
to get the view.
To serialize object state, add the OnPhotonSerializeView
function to your script. This function takes a PhotonStream, and a PhotonMessageInfo. This works exactly like serialization in Unity Networking.
To call RPCs, you can call the RPC function on the PhotonView. As with Unity Networking, RPC methods are marked with the [RPC]
attribute.
Here's an example that serializes position and calls an RPC when the spacebar is pressed:
using UnityEngine...