Kaynağa Gözat

禁止九轴计算,缓存九轴数据

lvjincheng 3 yıl önce
ebeveyn
işleme
ae2d2290ff
1 değiştirilmiş dosya ile 26 ekleme ve 0 silme
  1. 26 0
      Assets/BowArrow/Scripts/Bluetooth/AimHandler.cs

+ 26 - 0
Assets/BowArrow/Scripts/Bluetooth/AimHandler.cs

@@ -244,6 +244,21 @@ public class AimHandler : MonoBehaviour
         if (bytes[19] == 0 && bytes[20] == 0 && bytes[21] == 0 && bytes[22] == 0 && bytes[23] == 0 && bytes[24] == 0)
             return;
 
+        if (ban9AxisCalculate) //如果箭射出后禁止九轴计算,就缓存最新几帧九轴数据
+        {
+            
+            if (cached9AxisFrames.Count < 3)
+            {
+                cached9AxisFrames.Enqueue(bytes);
+            }
+            else
+            {
+                cached9AxisFrames.Dequeue();
+                cached9AxisFrames.Enqueue(bytes);
+            }
+            return;
+        }
+
         float ax = TwoByteToFloat(bytes[7], bytes[8]);
         float ay = TwoByteToFloat(bytes[9], bytes[10]);
         float az = TwoByteToFloat(bytes[11], bytes[12]);
@@ -352,9 +367,20 @@ public class AimHandler : MonoBehaviour
     }
 
     private bool ban9AxisCalculate = false;
+    private Queue<byte[]> cached9AxisFrames = new Queue<byte[]>();
     public void Ban9AxisCalculate(bool ban) {
         ban9AxisCalculate = ban;
         if (!ban) {
+            //恢复九轴计算时,把缓存的最新几帧计算了
+            while (cached9AxisFrames.Count > 0)
+            {
+                try
+                {
+                    OnDataReceived(cached9AxisFrames.Dequeue());
+                }
+                catch (Exception) { }
+            }
+            //立马应用到控制物体中
             if (controlObj) controlObj.localRotation = newRotation;
         }
     }