State Synchronization Details 状态同步细节
You can enable State Synchronization per Network View by choosing either Reliable Delta Compressed or Unreliable from the State Synchronization drop-down. You must then choose what kind of data will synchronized in the Observed property.
你可以每一个Network View启动状态同步通过从State Synchronization的下拉列表中选择Reliable Delta Compressed 或者 Unreliable.然后你必须选择什么种类的数据将被同步在Observed(观察)属性。
Unity can pack/unpack some specific classes: Transform, Animation, Rigidbody and MonoBehaviour.
Unity可以打包/解包一些指定的类:Transform, Animation, Rigidbody 和MonoBehaviour.
Transforms are serialized by storing position, rotation and scale. Parenting information is not transferred over the network.
Transforms通过存储位置,旋转和缩放来被序列化的。父子关系信息不被网络传输。
Animation serializes each running animation state, that is time, weight, speed and enabled.
Animation序列化每一个运行的动作状态,时间,权重,速度和是否启动。 Rigidbody serializes position, rotation, velocity and angular velocity. Rigidbody序列化位置,旋转,速度和角速度。
Scripts (MonoBehaviours) call the function OnSerializeNetworkView(). 脚本(MonoBehaviours)调用方法OnSerializeNetworkView().
Reliability and bandwidth 可靠性和带宽
Network Views currently support two types of reliability. Reliable Delta Compressed and Unreliable.
Network View现在支持两种类型的可靠性。Reliable Delta Compressed和Unreliable.
Both have their own advantage and disadvantages, and the right mode should be chosen on a case by case basis.
他们各有优点和缺点,并且原则上应该选择正确的模式。
For additional information about minimizing bandwidth, please read the Minimizing Bandwidth page.
有关于缩小带宽的额外的信息,请阅读Minimizing Bandwidth page.
Reliable Delta Compressed 可靠增量压缩
Reliable Delta Compressed mode will automatically compare the data that was last received by the client. If no data has changed compared to it, no data will be sent. But on top of that the data will also be compared on a per
property basis. For example, if your position has changed but your rotation has not. Only the position has to be sent across the network.
Reliable Delta Compressed(可靠增量压缩)模式将自动比较客户端最近一次接收到的数据。如果比较以前没有数据变化,没有数据将被发送。但是除此之外数据将比较每一个属性的基础上。例如,如果你的位置改变但是旋转没有变的话。仅仅把位置发送通过网络。
In this mode, Unity internally packs one bit prior to each property that determines if the data has actually changed. If it has not changed, the property will not be included in the serialized data thus saving a lot of bandwidth.
在这种模式下,在Unity内部打包一个比特在前面表示每个属性,它定义数据是否有事实上的变化。如果它没有变化,属性将不会包括在序列化数据因此节省了大量的带宽。
Unity will also ensure that every packet that is sent will arrive by resending the
packet in the case of UDP packet drop until it is received. This means that if a packet is dropped, any packets sent later will not be applied until the dropped packet is re-sent and received. Until then, all later packets will wait in the buffer.
如果发生UDP数据包丢包的情况,Unity也将确认每一个被发送的数据包通过重发是否将会到达,直到它接收到。这就意味着一个数据包如果丢失,任何最近发送数据包将不会被应用,直到丢失的数据包被重新发送和接收。在这之前,所有最近的数据包将在缓存里等待。
Unreliable 不可靠
In Unreliable mode, Unity will send out the current state regardless of if it has changed or not. State will not be delta compressed since it is unknown if the sent data will actually arrive on the receiver.
在不可靠模式,无论状态是否发生改变,Unity都将发送当前状态 。状态将不被增量压缩,因为它不知道已发送的数据是否真正的到达接收者。
Deciding which method to use 决定使用哪种方法
The Network layer uses UDP which is an unreliable unordered protocol but it can send reliable ordered packets, just like TCP does. It internally uses ACKs and NACKs do control packet transmission, ensuring no packets are dropped. The downside to using reliable ordered packets is that if a packet is dropped or delayed, everything stops until that packet has arrived safely. This can cause noticeable delays in transmission in lag intensive networks.
网络层使用UDP,这是一个不可靠的无序协议,但它可以发送可靠有序的数据包,就像TCP。它在内部使用ACK和NACK去控制数据包的传达,确认没有数据包丢失。使用可靠有序的数据包的缺点是,如果一个数据包被丢弃或延迟,一切都停止,直到该数据包已安全抵达。当在滞后密集的网络中,这可能导致传输明显的延迟。
Unreliable sending is useful for data where you know that it is going to change every frame anyway. For example, in a racing game, you can practically rely on that the player's car is always moving. Thus delta compression and reliable transfer only adds overhead without adding an practical gain.
不可靠发送对于你知道将会在每一帧都变化的数据上非常有用。例如,在赛车游戏,你几乎可以依靠玩家的车子总是向前的。因此,增量压缩和可靠传输不会增加实际收益反而会增加开销。
In general, you should use Unreliable sending when you know data is
changing all the time and minimizing lag is critical. If the data tracked by the Network View is not changing every frame and bandwidth is important to you, then delta compressed is preferred.
总之,你应该使用不可靠发送当你知道数据是时时刻刻的变化并且最小化滞后是关键。如果通过Network View的追踪数据不是每一帧都改变的并且带宽对于你来说是重要的,那么增量压缩是首选。
It is important to understand that lag is not the same thing as bandwidth. They are two separate properties that you want to optimize for in different use case. 理解延迟和带宽的关系不是等同的是非常重要的。他们是两个单独的属性,你要根据不同的使用情况去进行优化。
Prediction 预言
When the server has full authority over the world state, the clients only update the game according to these states they receive from the server. One problem which arises is that controls feel unnatural as when a player pushes the
forward button for example, he won't move until the updated state is received from the server. This delay depends on the latency of the connection so the worse the connection the more unnatural the actions of the player become. 当服务器有世界各地的国家充分授权,客户端只根据他们从服务器获得的这些国家来更新游戏 。其中一个出现问题就是控制感觉不自然,例如当一个玩家按下向前键的时候,他没有移动直到从服务器那边接收到更新的状态。这个延迟取决于连接的延迟,所以更糟糕的连接使得玩家的行为变得更加的不自然。 One way to help with this is Player Prediction which means the client, knowing how the server calculates his movement, predicts the movement himself. So the player moves instantly with the control but the server sees his position from his last update. When the state update arrives from the server, the client will compare what he predicted with what actually happened. This might differ as the server might know more about the environment around the
player, the client just knows what he needs to know. Errors in prediction are corrected as they happen, if they are corrected gradually the correction will look more smooth and less noticeable.
一种方式是用Player Prediction(玩家预测)意思就是客户端,知道服务器如何计算这个行为,预测他自己的行为。所以玩家马上移动通过控制但是服务器从他最新的更新里面看到数据。当状态更新到达服务器的时候,客户端将比较他预测的结果与真实发生的情况。这个可能不同于服务器可能知道更多的关于玩家的环境,客户端仅仅知道他需要知道的东西。他们发生的预测错误被改正,如果他们被逐步的纠正那么纠正将看起来更加的平滑并且不容易被注意到。
Dead reckoning or interpolation/extrapolation 轨迹推算或者内插/ 外推法
It is possible to apply the same principle with player prediction to the
opponents of the player. Extrapolation is when several last (buffered) known position, velocity and direction of an opponent is used to predict where he will be in the next frames. When the next state update finally arrives with the correct position, the client state will be updated with accurate information, possibly skipping if the prediction was bad. In FPS games the behavior of players can be very erratic so this kind of prediction only has limited effect. If the lag gets high enough the opponent will skip badly as the errors in prediction get larger.
可能应用与玩家预测相同的原理到玩家的对手。Extrapolation(外插法)是用一些最新的(缓存的)知道的位置,速度和一个对手的方向来预测他在下一帧将在哪里出现 。当下一个状态更新包括正确的位置最终到达的时候,正确的状态将用正确的信息更新,可能忽略如果预测不好的话。在FPS游戏玩家的行为可以非常的不规律所以这种方式的预测仅仅是在效果。如果延迟足够高的话,对手将严重的忽略在预测中变得更大的错误。
Interpolation is when packets get dropped on the way to the client and the opponent position would normally pause and then jump to the newest position when a new packet finally arrives. By delaying the world state by some set amount of time (like 100 ms) and then interpolating the last known position with the new one, the movement between these two points, where packets were dropped, will be smooth.
Interpolation(内插法)是当数据包丢失在去客户端的路上然后对手的位置通常会暂停,然后当一个新的包最终到达时会跳转到最新的位置 。通过延迟一些世界状态的时间(像100 毫秒) 然后插值在最近知道的位置和现在的这个之间,移动将在这两个点之间,那么丢包的那个地方,将被圆滑。