NETWORK_同步位置信息

本文探讨了如何使用网络视图和状态缓冲区实现远程对象状态的高效同步,通过发送和接收本地变换信息,确保在不同网络环境下对象状态的一致性和实时性。

using UnityEngine;
using System.Collections;

public class NetworkInterpolatedTransform : MonoBehaviour {
 
 public double interpolationBackTime = 0.1;
 
 internal struct  State
 {
  internal double timestamp;
  internal Vector3 pos;
  internal Quaternion rot;
 }

 // We store twenty states with "playback" information
 State[] m_BufferedState = new State[20];
 // Keep track of what slots are used
 int m_TimestampCount;
 
 void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
 {
  // Always send transform (depending on reliability of the network view)
  if (stream.isWriting)
  {
   Vector3 pos = transform.localPosition;
   Quaternion rot = transform.localRotation;
   stream.Serialize(ref pos);
   stream.Serialize(ref rot);
  }
  // When receiving, buffer the information
  else
  {
   // Receive latest state information
   Vector3 pos = Vector3.zero;
   Quaternion rot = Quaternion.identity;
   stream.Serialize(ref pos);
   stream.Serialize(ref rot);
   
   // Shift buffer contents, oldest data erased, 18 becomes 19, ... , 0 becomes 1
   for (int i=m_BufferedState.Length-1;i>=1;i--)
   {
    m_BufferedState[i] = m_BufferedState[i-1];
   }
   
   // Save currect received state as 0 in the buffer, safe to overwrite after shifting
   State state;
   state.timestamp = info.timestamp;
   state.pos = pos;
   state.rot = rot;
   m_BufferedState[0] = state;
   
   // Increment state count but never exceed buffer size
   m_TimestampCount = Mathf.Min(m_TimestampCount + 1, m_BufferedState.Length);

   // Check integrity, lowest numbered state in the buffer is newest and so on
   for (int i=0;i<m_TimestampCount-1;i++)
   {
    if (m_BufferedState[i].timestamp < m_BufferedState[i+1].timestamp)
     Debug.Log("State inconsistent");
   }
   
   //Debug.Log("stamp: " + info.timestamp + "my time: " + Network.time + "delta: " + (Network.time - info.timestamp));
  }
 }
 
 // This only runs where the component is enabled, which is only on remote peers (server/clients)
 void Update () {
  double currentTime = Network.time;
  double interpolationTime = currentTime - interpolationBackTime;
  // We have a window of interpolationBackTime where we basically play
  // By having interpolationBackTime the average ping, you will usually use interpolation.
  // And only if no more data arrives we will use extrapolation
  
  // Use interpolation
  // Check if latest state exceeds interpolation time, if this is the case then
  // it is too old and extrapolation should be used
  if (m_BufferedState[0].timestamp > interpolationTime)
  {
   for (int i=0;i<m_TimestampCount;i++)
   {
    // Find the state which matches the interpolation time (time+0.1) or use last state
    if (m_BufferedState[i].timestamp <= interpolationTime || i == m_TimestampCount-1)
    {
     // The state one slot newer (<100ms) than the best playback state
     State rhs = m_BufferedState[Mathf.Max(i-1, 0)];
     // The best playback state (closest to 100 ms old (default time))
     State lhs = m_BufferedState[i];
     
     // Use the time between the two slots to determine if interpolation is necessary
     double length = rhs.timestamp - lhs.timestamp;
     float t = 0.0F;
     // As the time difference gets closer to 100 ms t gets closer to 1 in
     // which case rhs is only used
     if (length > 0.0001)
      t = (float)((interpolationTime - lhs.timestamp) / length);
     
     // if t=0 => lhs is used directly
     transform.localPosition = Vector3.Lerp(lhs.pos, rhs.pos, t);
     transform.localRotation = Quaternion.Slerp(lhs.rot, rhs.rot, t);
     return;
    }
   }
  }
  // Use extrapolation. Here we do something really simple and just repeat the last
  // received state. You can do clever stuff with predicting what should happen.
  else
  {
   State latest = m_BufferedState[0];
   
   transform.localPosition = latest.pos;
   transform.localRotation = latest.rot;
  }
 }
}

转载于:https://www.cnblogs.com/softimagewht/archive/2011/09/03/2164603.html

JobInfo.NETWORK_TYPE_UNMETERED是Android中用于指定网络类型的常量。 ### 定义 JobInfo.NETWORK_TYPE_UNMETERED代表非计费网络类型。非计费网络通常指的是那些不会产生额外数据费用的网络,比如Wi - Fi网络,使用这种网络进行数据传输一般不会产生流量费用。 ### 用途 其主要用途是在使用JobScheduler调度任务时,限制任务只能在非计费网络下执行。这样可以避免在使用移动数据网络(可能会产生费用)时执行一些消耗大量数据的后台任务,节省用户的流量费用,提高用户体验。例如,应用程序可能需要在后台下载大型文件、同步大量数据等操作,使用非计费网络可以确保这些操作不会给用户带来额外的费用负担。 ### 使用方法 在使用JobInfo.Builder类来构建JobInfo对象时,可以使用setRequiredNetworkType方法并传入JobInfo.NETWORK_TYPE_UNMETERED作为参数,来指定任务需要在非计费网络下执行。以下是一个示例代码: ```java import android.app.job.JobInfo; import android.app.job.JobScheduler; import android.content.ComponentName; import android.content.Context; public class JobSchedulerExample { public static final int MY_BACKGROUND_JOB = 0; public static void scheduleJob(Context context) { JobScheduler js = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); JobInfo job = new JobInfo.Builder( MY_BACKGROUND_JOB, new ComponentName(context, MyJobService.class)) .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED) .setRequiresCharging(true) .build(); js.schedule(job); } } ``` 在上述代码中,通过`.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)`这一行代码,将任务设置为仅在非计费网络下执行,同时还设置了任务需要在设备充电时执行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值