我一直围着如何最终使20个物体的运动同步到每个60个选定点。我有几个选项,主要是
所以这就是我得出的结论,我需要将网络通用时间精确到0.1秒左右,以便用户看到相同的动作。如果您认为这是一种错误的网络联系方式,请告诉我,因为我非常熟悉网络。
到目前为止,我已经尝试了三种方法来获得同步时间。
我使用System.DateTime
认为这是一个准确且通用的时间,但发现设备之间的差异超过了一秒
我尝试计算两台设备之间的延迟时间,以便我可以通过
计算并删除设备时间变化GetAveragePing(NetworkPlayer player);
的内置方法,但由于NetworkPlayer类似乎不再具有功能性和兼容性,因此它已经过时并且已经过折旧GetCurrentRtt(int hostId, int connectionId, out byte error);
的构建方法中使用另一个返回0且我认为可能会折旧的方法我尝试通过
从服务器访问同步网络时间的形式GetNetworkTimestamp();
获取网络时间戳因此,所有这些方法都失败了所以今天我问你;
还有另一种同步时间的方法吗?
如果其中一种方法有效,那么我出错了,在这种情况下,我可以提供有关我的问题和使用的代码的更多详细信息吗?
我的网络方法是完全错误的,或者至少大部分是错误的,你是如何建议我达到预期目标的?
非常感谢您阅读本文,我希望它详细而且清晰,如果我能帮助您提高对我的问题的理解,我将非常乐意为您提供帮助。感谢您的任何答案/启发性评论。
联网时间代码
脚本A(获取网络时间)
using UnityEngine;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using UnityEngine.UI;
public class GetNetworkTime : MonoBehaviour {
public static System.DateTime NetworkTime()
{
//default Windows time server
const string ntpServer = "time.windows.com";
// NTP message size - 16 bytes of the digest (RFC 2030)
var ntpData = new byte[48];
//Setting the Leap Indicator, Version Number and Mode values
ntpData[0] = 0x1B; //LI = 0 (no warning), VN = 3 (IPv4 only), Mode = 3 (Client Mode)
var addresses = Dns.GetHostEntry(ntpServer).AddressList;
//The UDP port number assigned to NTP is 123
var ipEndPoint = new IPEndPoint(addresses[0], 123);
//NTP uses UDP
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Connect(ipEndPoint);
//Stops code hang if NTP is blocked
socket.ReceiveTimeout = 3000;
socket.Send(ntpData);
socket.Receive(ntpData);
socket.Close();
//Offset to get to the "Transmit Timestamp" field (time at which the reply
//departed the server for the client, in 64-bit timestamp format."
const byte serverReplyTime = 40;
//Get the seconds part
ulong intPart = System.BitConverter.ToUInt32(ntpData, serverReplyTime);
//Get the seconds fraction
ulong fractPart = System.BitConverter.ToUInt32(ntpData, serverReplyTime + 4);
//Convert From big-endian to little-endian
intPart = SwapEndianness(intPart);
fractPart = SwapEndianness(fractPart);
var milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000L);
//**UTC** time
var networkDateTime = (new System.DateTime(1900, 1, 1, 0, 0, 0, System.DateTimeKind.Utc)).AddMilliseconds((long)milliseconds);
//return networkDateTime.ToLocalTime();
return networkDateTime;
}
// stackoverflow.com/a/3294698/162671
static uint SwapEndianness(ulong x)
{
return (uint)(((x & 0x000000ff) << 24) +
((x & 0x0000ff00) << 8) +
((x & 0x00ff0000) >> 8) +
((x & 0xff000000) >> 24));
}
}
脚本B(差异计算器和时间记录器)
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using UnityEngine.UI;
public class SyncTime2 : NetworkBehaviour {
public float serverTimeDif;
public float clientTimeDif;
GetNetworkTime GetNetworkTime;
GameObject time;
Text TimeLog;
// Use this for initialization
void Start () {
GetNetworkTime = GetComponent<GetNetworkTime>();
time = GameObject.Find("time");
TimeLog = time.GetComponent<Text>();
if (isServer)
{
serverTimeDif = (float)System.DateTime.UtcNow.TimeOfDay.TotalSeconds - (float) GetNetworkTime.NetworkTime().TimeOfDay.TotalSeconds;
StartCoroutine("DisplayTime", serverTimeDif);
}
else
{
clientTimeDif = (float)System.DateTime.UtcNow.TimeOfDay.TotalSeconds - (float)GetNetworkTime.NetworkTime().TimeOfDay.TotalSeconds;
StartCoroutine("DisplayTime", clientTimeDif);
}
}
IEnumerator DisplayTime (float TimeDif)
{
for (;;)
{
TimeLog.text = "" + ((float)System.DateTime.UtcNow.TimeOfDay.TotalSeconds + TimeDif);
// Log time so i can see if it is different on different devices
yield return new WaitForSeconds(0.01f);
}
}
}
答案 0 :(得分:0)
Hmmmff ......似乎只是添加时差而不是减去它的工作并同步时间,所以而不是
TimeLog.text = "" + ((float)System.DateTime.UtcNow.TimeOfDay.TotalSeconds - TimeDif);
我应该使用
TimeLog.text = "" - ((float)System.DateTime.UtcNow.TimeOfDay.TotalSeconds + TimeDif);
如果有人能够确认这是不是正确的网络方法我会认为这是一个答案