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实例,调用函数
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.
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.
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.
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:
Alternatively you can use PhotonNetwork.LoadLevel. It temporarily disables the message queue as well.
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.
Various topics
Differences to Unity Networking 和Unity网络的区别
- 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是基于于服务器-客户机,但是有一个专用的服务器,由于主机离开不再连接丢失。
- 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)。
- 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专用服务器的连接。
- 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并发玩家。 其他许可成本一次性费用(像你托管)和并发用户的限制。
- 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已经提供了更多的功能比团结,比如内置的负载平衡和离线模式。
- 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),在实际的游戏。
- Unity+ Photon服务器实时对战游戏——远程过程调用 (五)
- Unity+ Photon服务器实时对战游戏——Photon服务器介绍(一)
- Unity+ Photon服务器实时对战游戏——Photon服务器介绍(二)
- Unity+ Photon服务器实时对战游戏——Photon服务器介绍(三)
- Unity+ Photon服务器实时对战游戏——主服务器和大厅(四)
- Unity+ Photon服务器实时对战游戏——Photon实例化网络化的对象 (六)
- 实时游戏对战引擎Photon
- Unity3D利用Photon实现实时联网对战(一)Photon Server介绍
- Unity3D利用Photon实现实时联网对战(一)Photon Sever介绍
- Photon Server游戏服务器从零开始学习(五) NHibernate 对数据库的操作
- 王者荣耀实时对战服务器Photon之Pun应用系列文章
- Unity链接Photon服务器
- Unity Photon服务器
- Unity Photon服务器(二)
- Unity3D自学笔记——Photon服务器的后台架构实现(五)
- 将Unity官方射击游戏 Space Shooter 改为实时对战小游戏,使用天梯实时对战服务(NanoLink)
- Unity3D利用Photon实现实时联网对战(二)PUN SDK介绍
- Unity3D利用Photon实现实时联网对战(三)创建和加入房间
- 新年,新的开始。
- 1006. 换个格式输出整数 (15)
- 常用算法——深度学习
- KB奇遇记(7):不靠谱的项目实施计划_0
- thinkjs使用geetest
- Unity+ Photon服务器实时对战游戏——远程过程调用 (五)
- FreeRTOS的源代码个人分析(基于KEIL下STM32F103的Demo) 三
- spring Bean属性配置细节
- MSSQL 游标的创建和使用
- [自定义控件]android自定义view基础
- 数据科学入门:前言
- Unity+ Photon服务器实时对战游戏——Photon实例化网络化的对象 (六)
- 常用算法——SIFT特征提取
- 日期类问题之月份牌问题