- 最后登录
- 2018-12-19
- 注册时间
- 2012-8-20
- 阅读权限
- 90
- 积分
- 54706
- 纳金币
- 32328
- 精华
- 41
|
Unity's native networking supports everything discussed on the previous page. Server creation and client connection, sharing data between connected clients, determining which player controls which objects, and punching through network configuration variations are all supported out of the box. This page will walk you through the Unity-specific implementation of these networking practices.Unity中的原生网络支持在上一个章节讨论的每项内容。创建服务器和客户端的连接,在连接的客户端间共享数据,处理玩家各自控制的对象和通过网络推送配置的变化等等功能均立即可用。本章节会带你浏览这些网络相关技术的Unity特定的实现细节。
Creating a ServerBefore you can begin playing a networked game, you have to determine the different computers you will be communicating with. To do this, you have to create a server. This can be a machine that is also ***nning the game or it can be a dedicated machine that is not participating in the game. To create the server, you simply call Network.InitializeServer() from a script. When you want to connect to an existing server as a client, you call Network.Connect() instead.在你开始玩一个网络游戏前,你可以通过创建一个服务器,来知道将要连接的不同电脑。这会用到一台也运行着游戏的机器或者这台机器并不参与游戏而是专用于提供服务。你可以通过从脚本中调用Network.InitializeServer()来创建一个服务器。当你想以客户端的身份连接到一个存在的服务器时,你则需要使用调用Network.Connect()。In general, you will find it very useful to familiarize yourself with the entire Network class.通常,你会发现这对熟悉整个Network类非常有用。
Communicating using Network Views
使用NetworkViews互相通信The Network View is a Component that sends data across the network. Network Views make your GameObject capable of sending data using RPC calls or State Synchronization. The way you use Network Views will determine how your game's networking behaviors will work. Network Views have few options, but they are incredibly important for your networked game.NetworkView是一个跨网络传送数据的组件。网络视图使你的游戏对象可以通过RPC传送数据或是状态同步。你是用网络视图的方式将决定你的游戏网络部分的行为。网络视图只有很少一些些配置选项,但他们对你的网络游戏是非常重要的。For more information on using Network Views, please read the Network View Guide page and Component Reference page.更多关于网络视图的信息,请查阅网络视图指南和组件参考页面。
Remote Procedure Calls 远程过程调用Remote Procedure Calls (RPCs) are functions declared in scripts that are attached to a GameObject that contains a Network View. The Network View must point to the script which contains the RPC function. The RPC function can then be called from any script within that GameObject.远程过程调用(RPC)是在附加到一个包含了网络视图的游戏对象的脚本中声明的函数。网络视图必须引用包含了RPC函数的脚本组件。RPC函数能被游戏对象中的任何其他脚本组件的函数调用。For more information on using RPCs in Unity, please read the RPC Details page.更多关于Unity中使用RPC的信息,请阅读RPC Details页面。
State Synchronization 状态同步State Synchronization is the continual sharing of data across all game clients. This way a player's position can be synchronized over all clients, so it seems it is controlled locally when data is actually being delivered over a network. To synchronize state within a GameObject you just need to add a NetworkView to that object and tell it what to observe. The observed data is then synchronized across all clients in the game.状态同步是所有游戏客户端持续共享的数据。通过这种方式,一个角色的位置能被同步到所有客户端。For more information on using State Synchronization in Unity, please read the State Synchronization page.更多关于状态同步的信息,请查阅State Synchronization页面。
Network.Instantiate() 网络实例化Network.Instantiate() lets you instantiate a prefab on all clients in a natural and easy way. Essentially this is an Instantiate() call, but it performs the instantiation on all clients.Network.Instantiate() 使你能用一种自然简单的方式在所有客户端实例化一个预制件。本质上来说这是一个实例化调用,但他能同时在所有客户端执行这个操作。Internally Network.Instantiate is simply a buffered RPC call which is ***cuted on all clients (also locally). It allocates a NetworkViewID and assigns it to the instantiated prefab which makes sure it synchronizes across all clients correctly.Network.Instantiate()简单的由在所有客户端(包括本地)执行的缓存RPC调用实现。它分配一个NetworkViewID,然后把ID和实例化的预制件联系在一起一保证能在所有客户端进行同步。For more information please read the Network Instantiate page.更多信息请查阅Network Instantiate页面。
NetworkLevelLoad() 网络关卡加载Dealing with sharing data, state of client players, and loading levels can be a bit overwhelming. The Network Level Load page contains a helpful example for managing this task.处理共享数据,客户端角色的状态,读取关卡可能会有一些难度,Network Level Load包含了一个有用的例子来执行这个任务。
Master Server 主服务器The Master Server helps you match games. When you start a server you connect to the master server, and it provides a list of all the active servers.主控服务器帮助你找到游戏。当你将连接到主控服务器,它会提供一个所有活动服务器的列表给你。The Master Server is a meeting place for servers and clients where servers are advertised and compatible clients can connect to ***nning games. This prevents the need for fiddling with IP addresses for all parties involved. It can even help users host games without them needing to mess with their routers where, under normal circumstances, that would be required. It can help clients bypass the server's firewall and get to private IP addresses which are normally not accessible through the public internet. This is done with help from a facilitator which facilitates connection establishment.主控服务器是主机和那些客户端碰头的场所,这里有主机的广告,合适的客户端能连接到主机进行游戏。主控服务器避免了欺诈性的IP地址。能帮助用户主持一个游戏,而不会受到路由器的干扰,在通常情况下,这是必须的。它还能帮助客户端绕过服务器的防火墙,获取通常不能通过互联网访问的私有IP地址。这是通过一种连接促进机制来实现的。For more information please read the Master Server page.更多信息查阅Master Server页面。
Minimizing Bandwidth 最小化带宽Using the minimum amount of bandwidth to make your game ***n correctly is essential. There are different methods for sending data, different techniques for deciding what or when to send and other tricks at your disposal.使用最少量得带宽使你的游戏能正常运行是必须的。有几种不同的发送数据的方法,不同的技术用来处理什么时候和发送什么数据,以及一些花招供你使用。For tips and tricks to reduce bandwidth usage, please read the Minimizing Bandwith page.关于减少带宽使用的要诀和技巧,请阅读Minimizing Bandwith page。
Debugging Networked GamesUnity comes with several facilities to help you debug your Networked game.Unity有几种设施来帮助你调试你的网络游戏。 The Network Manager can be used for logging all incoming and outgoing network traffic.
Network Manager能备用来记录所有出入的网络通信。
Using the Inspector and Hierarchy View effectively you can track object creation and inspect view id's etc.
使用监视器和层级视图你可以有效的追踪对象的创建和检查视图等等。
You can launch Unity two times on the same machine, and open different projects in each. On Windows, this can be done by just launching another Unity instance and opening the project from the project wizard. On Mac OS X, multiple Unity instances can be opened from the terminal, and a -projectPath argument can be specified:
在同一机器上你能运行Unity两次,然后打开不同的工程。在Windows环境中,通过执行另一个Unity实例并在项目向导中打开项目。在Mac环境下,多个Unity实例能通过带有特定的-projectPath参数在terminal被打开。 /Applications/Unity/Unity.app/Contents/MacOS/Unity -projectPath "/Users/MyUser/MyProjectFolder/"
/Applications/Unity/Unity.app/Contents/MacOS/Unity -projectPath "/Users/MyUser/MyOtherProjectFolder/"Make sure you make the player ***n in the background when debugging networking because, for example, if you have two instances ***nning at once, one of them doesn't have focus. This will break the networking loop and cause undesirable results. You can enable this in Edit->roject Settings->layer in the editor or with Application***nInBackground当调试网络时请确定让播放器运行在后台,例如,如果你有两个实例同一时间运行,他们中的一个会不能接受输入。这会中断网络循环并导致不可预测的结果,你能通过编辑器中Edit->roject Settings -> Player启动这个选项,或者通过Application***nInBackground来实现。【来源:互联网】
更多精彩教程,尽在web3D纳金网http://www.narkii.com/college/ |
|