Remote Procedure Calls (RPCs) are exactly what the name implies: methods that can be called on remote clients in the same room. To enable remote calls for a method of a MonoBehaviour, you must apply the attribute: [PunRPC]. A PhotonView instance is needed on the same GameObject, to call the marked functions.
远程过程调用(Remote Procedure call,rpc)正是其名称所暗示的:方法,可以被称为远程客户端在同一房间。 启用远程调用的方法MonoBehaviour,你必须应用属性:[PunRPC]。 需要在同一GameObject PhotonView实例,调用函数
[PunRPC] void ChatMessage( string a, string b) { Debug.Log( "ChatMessage " + a + " " + b); }To call the method from any script, you need access to a PhotonView object. If your script derives from Photon.MonoBehaviour, it has a photonView field. Any regular MonoBehaviour or GameObject can use: PhotonView.Get(this) to get access to its PhotonView component and then call RPCs on it.
从任何脚本调用该方法,您需要访问一个PhotonView对象。 如果你的脚本来源于Photon。 MonoBehaviour,photonView字段。 任何正则MonoBehaviour或GameObject可以使用:PhotonView.Get(这)获得其PhotonView组件,然后调用rpc。 PhotonView photonView = PhotonView.Get( this); photonView.RPC( "ChatMessage", PhotonTargets.All, "jup", "and jup!");So, instead of directly calling the target method, you call RPC() on a PhotonView. Provide the name of the method to call, which players should call the method and then provide a list of parameters.
因此,而不是直接调用目标方法,你叫PhotonView RPC()。 提供调用的方法的名称,玩家应该调用该方法,然后提供一个参数列表。Careful: The parameters list used in RPC() has to match the number of expected parameters! If the receiving client can’t find a matching method, it will log an error. There is one exception to this rule: The last parameter of a RPC method can be of type PhotonMessageInfo, which will provide some context for each call.
注意:RPC()中使用的参数列表匹配的数量预计参数! 如果接收端找不到匹配的方法,它将记录一个错误。 有一个例外:一个RPC方法的最后一个参数可以PhotonMessageInfo类型,这将为每个调用提供一些上下文。 [PunRPC] void ChatMessage( string a, string b, PhotonMessageInfo info) { Debug.Log(String.Format( "Info: {0} {1} {2}", info.sender, info.photonView, info.timestamp)); }RPCs are called on specific PhotonViews and always target the matching one on the remote client. If the remote client does not know the fitting PhotonView, the RPC is lost.
A typical cause for lost RPCs is when clients load and set up levels. One client is faster or in the room for a longer time and sends important RPCs for objects that are not yet loaded on the other clients. The same happens when RPCs are buffered.
The solution is to pause the message queue, during scene loading. This code shows how how you can do it:
private IEnumerator MoveToGameScene() { // Temporary disable processing of futher network messages PhotonNetwork.isMessageQueueRunning = false; Application.LoadLevel(levelName); }Alternatively you can use PhotonNetwork.LoadLevel. It temporarily disables the message queue as well.
或者您可以使用PhotonNetwork.LoadLevel。 它暂时禁用消息队列。Disabling the message queue will delay incoming and outgoing messages until the queue is unlocked. Obviously, it's very important to unlock the queue when you're ready to go on.
禁用消息队列将推迟传入和传出消息,直到队列解锁。 显然,这是非常重要的解锁队列当你准备继续。RPCs that belonged to the previously loaded scene but still arrived will now be discarded. But you should be able to define a break between both scenes by RPC.
rpc属于前面加载场景但仍然到达现在将被丢弃。 但是你应该能够定义一个打破两国由RPC场景。