NetworkMessageInfo.timestamp 时间戳

var timestamp : double

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.

时间戳可以为审查、修改数据包提供

帮助,时间戳使用 double 类型记录,以避免游戏程序长时间运行导致的溢出错误。时间戳使用 32bit整型来保存毫秒精度。时间戳会根据 Network.time 进行自动修正,这样 Network.time 减 messageInfo.timeStamp 等于 数据包在网络传输消耗的时间。
using UnityEngine;

using System.Collections;



public class example : 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);

	}

}
//数据

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

		//用户改变某个x坐标

		//打包发送

	}

	//否

	else

	{

		// Receiving 接收

		transitTime = Network.time - info.timestamp;

		stream.Serialize (horizontalInput);

		something = horizontalInput;

		//计算耗费时间

		//解包

		//得到数据

	}

}



function OnGUI()

{

	GUILayout.Label("Last transmission time: "+ transitTime);

}
Page last updated: 2011-1-4