Unity+ Photon服务器实时对战游戏——远程过程调用 (五)

来源:互联网 发布:印度有帅哥吗 知乎 编辑:程序博客网 时间:2024/04/29 07:45

Remote Procedure Calls 远程过程调用

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));
}

Timing for RPCs and Loading Levels rpc和加载时间的水平

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场景。

Various topics

Differences to Unity Networking 和Unity网络的区别

  1. Host model 主机模式
    • Unity networking is server-client based (NOT P2P!). Servers are run via a Unity client (so via one of the players)
    • Unity网络是基于于服务器-客户机(不是P2P !)。 服务器运行通过一个联合端(通过玩家之一)
    • Photon is server-client based as well, but has a dedicated server; No more dropped connections due to hosts leaving.
    • Photon是基于于服务器-客户机,但是有一个专用的服务器,由于主机离开不再连接丢失。
  2. Connectivity 连接
    • Unity networking works with NAT punchthrough to try to improve connectivity: since players host the network servers, the connection often fails due to firewalls/routers etc. Connectivity can never be guaranteed, there is a low success rate.
    • Unity网络使用NAT punchthrough试图提高连通性:因为玩家主机网络服务器,连接常常失败由于防火墙/路由器等连接无法保证,成功率较低。
    • Photon has a dedicated server, there is no need for NAT punchthrough or other concepts. Connectivity is a guaranteed 100%. If, in the rare case, a connection fails it must be due to a very strict client side network (a business VPN for example).
    • Photon有一个专用的服务器,不需要NAT punchthrough或其他概念。 连通性是100%的保证。 在罕见的情况下,如果连接失败,它必须是由于一个非常严格的客户端网络(例如业务VPN)。
  3. Performance 性能
    • Photon beats Unity networking performance wise. We do not have the figures to prove this yet but the library has been optimized for years now. Furthermore, since the Unity servers are player hosted latency/ping is usually worse; you rely on the connection of the player acting as server. These connections are never any better then the connection of your dedicated Photon server.
    • Photon节拍Unity网络性能明智。 我们没有数据证明这,但现在图书馆多年来一直优化。 此外,自从联合主办的服务器玩家延迟/ ping通常是更糟;你依赖玩家扮演服务器的连接。 这些连接没有任何更好的Photon专用服务器的连接。
  4. Price 价格
    • Like the Unity Networking solution, the Photon Unity Networking plugin is free as well. You can subscribe to use Photon Cloud hosting service for your game. Alternatively, you can rent your own servers and run Photon on them. The free license enables up to 100 concurrent players. Other licenses cost a one-time fee (as you do the hosting) and lift the concurrent user limits.
    • 像Unity网络解决方案,PhotonUnity网络插件是免费的。 您可以订阅使用Photon云托管服务为你的游戏。 或者,你可以租自己的服务器和运行Photon。 免费的许可支持100并发玩家。 其他许可成本一次性费用(像你托管)和并发用户的限制。
  5. Features & maintenance 功能与维护
    • Unity does not seem to give much priority to their Networking implementation. There are rarely feature improvements and bugfixes are as seldom. The Photon solution is actively maintained and parts of it are available with source code. Furthermore, Photon already offers more features than Unity, such as the built-in load balancing and offline mode.
    • Unity似乎并没有给他们的网络实现多优先级。 有很少功能改进和修正很少。 Photon的解决方案是积极维护和部分源代码可用。 此外,Photon已经提供了更多的功能比团结,比如内置的负载平衡和离线模式。
  6. Master Server 主服务器
    • The Master Server for Photon is a bit different from the Master Server for plain Unity Networking: In our case, it’s a Photon Server that lists room-names of currently played games in so called “lobbies”. Like Unity’s Master, it will forward clients to the Game Server(s), where the actual gameplay is done.
    • Photon的主服务器是有点不同于普通Unity网络的主服务器:在我们的例子中,它是一个Photon的服务器列表room-names目前在所谓的“游说”玩游戏。 Unity的主人,它将客户转发给游戏服务器(s),在实际的游戏。 
0 0