返回介绍

NetworkMessageInfo.timestamp 时间戳

发布于 2019-12-18 15:38:08 字数 2470 浏览 991 评论 0 收藏 0

JavaScript => public var timestamp: double;
C# => public double timestamp;

Description 描述

The time stamp when the Message was sent in seconds.

消息发送的时间戳单位秒。

Timestamps can be used to implement interpolation or extrapolation of continous streams of packets The timestamp is passed as a double to avoid overflow when a game is running for a long time. Internally timestamps are sent as 32 bit integers with millisecond accuracy to save bandwidth. Timestamps are automatically adjusted to be relative to Network.time. Thus Network.time - messageInfo.timeStamp is the time the packet spent in transit.

时间戳可以用于实现内插或外推法的连续数据流的数据包,当游戏运行很长时间,时间戳通过作为双精度以避免溢出。内部时间戳发送32位整数,使用毫秒精度以节省带宽。时间戳相对于 Network.time是自动将调整的。因此 Network.time-messageInfo.timeStamp 是该数据包在传输过程中花费的时间。

JavaScript:

var something : float;
	var transitTime: double;
	function OnSerializeNetworkView (stream : BitStream, 
		info : NetworkMessageInfo) {
		var horizontalInput : float = 0.0;
		if (stream.isWriting) {
			// Sending
			horizontalInput = transform.position.x;
			stream.Serialize (horizontalInput);
		} else {
			// Receiving
			transitTime = Network.time - info.timestamp;
			stream.Serialize (horizontalInput);
			something = horizontalInput;
		}
	}
 
	function OnGUI() {
		GUILayout.Label("Last transmission time: "+ transitTime);
	}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    public float something;
    public double transitTime;
    void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info) {
        float horizontalInput = 0.0F;
        if (stream.isWriting) {
            horizontalInput = transform.position.x;
            stream.Serialize(ref horizontalInput);
        } else {
            transitTime = Network.time - info.timestamp;
            stream.Serialize(ref horizontalInput);
            something = horizontalInput;
        }
    }
    void OnGUI() {
        GUILayout.Label("Last transmission time: " + transitTime);
    }
}

networkmessageinfo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文