lvjincheng 3 éve
szülő
commit
a3faa970f5

+ 1 - 1
Assets/BowArrow/Modules/NewUserGuider/NewUserGuiderManager.cs

@@ -675,7 +675,7 @@ public class NewUserGuiderManager : MonoBehaviour
         }
         if (Input.GetKeyDown(KeyCode.F3)) {
             Debug.Log("重置设备校准引导,规则引导");
-            AimHandler.ins.MagCalibrater = new o0._9Axis.MagnetometerAutoCalibrater();
+            AimHandler.ins.ResetMag();
             UserSettings.ins.deviceCalibrateGuideFinish = false;
             UserSettings.ins.gameRuleGuideFinish = new HashSet<int>();
         }

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

@@ -47,7 +47,7 @@ public class AimHandler : MonoBehaviour
     Vector3 cMinVector = new Vector3(0, 0, 0);
 
     [o0.Serialize]
-    public MagnetometerAutoCalibrater MagCalibrater;
+    MagnetometerAutoCalibrater MagCalibrater;
     o0GyrCalibrater GyrCalibrater;
 
     //陀螺仪校准进度记录
@@ -92,26 +92,43 @@ public class AimHandler : MonoBehaviour
     public void InitGyr(string record) {
         GyrCalibrater = new o0GyrCalibrater();
         try {
-            if (record == null) record = PlayerPrefs.GetString("o0GyrCalibrater");
-            var res = JsonConvert.DeserializeObject<o0GyrCalibrater>(record);
-            if (res != null) GyrCalibrater = res;
+            if (!string.IsNullOrEmpty(record)) {
+                var res = JsonConvert.DeserializeObject<o0GyrCalibrater>(record);
+                if (res != null) GyrCalibrater = res;
+            }
         } catch(Exception) {}
     }
 
     public void InitMag(string record) {
         MagCalibrater = new MagnetometerAutoCalibrater();
         try {
-            if (record == null) record = PlayerPrefs.GetString("new_mag_record");
-            Json.FromJson<MagnetometerAutoCalibrater>(record, ref MagCalibrater);
+            if (!string.IsNullOrEmpty(record)) {
+                Json.FromJson<MagnetometerAutoCalibrater>(record, ref MagCalibrater);
+            }
         } catch (System.Exception) {}
         magComplete = MagCalibrater.Complete;
     }
 
+    public void ResetGyr() {
+        GyrCalibrater._Average = Vector3.zero;
+    }
+
+    public void ResetMag() {
+        MagCalibrater = new MagnetometerAutoCalibrater();
+    }
+
+    public bool IsGyrCompleted() {
+        return !GyrCalibrater._Average.Equals(Vector3.zero);
+    }
+
+    public bool IsMagCompleted() {
+        return MagCalibrater.Complete;
+    }
+
     public IEnumerator SaveGyr() {
         yield return null;
         string mac = LoginMgr.myUserInfo.mac;
         string record = JsonConvert.SerializeObject(GyrCalibrater);
-        PlayerPrefs.SetString("o0GyrCalibrater", record); 
         UserPlayer.ins.call("userComp.saveMacCalibrate", 0, mac, record);
     }
 
@@ -119,7 +136,6 @@ public class AimHandler : MonoBehaviour
         yield return null;
         string mac = LoginMgr.myUserInfo.mac;
         string record = MagCalibrater.ToJson();
-        PlayerPrefs.SetString("new_mag_record", record);
         UserPlayer.ins.call("userComp.saveMacCalibrate", 1, mac, record);
     }
 
@@ -373,7 +389,7 @@ public class AimHandler : MonoBehaviour
         try {
             if (!MagCalibrater.Update(mag0o)) return;
         } catch(System.Exception) {
-            MagCalibrater = new MagnetometerAutoCalibrater();
+            ResetMag();
             PopupMgr.ins.ShowTipTop("磁场干扰请远离电子设备");
             return;
         }
@@ -438,7 +454,7 @@ public class AimHandler : MonoBehaviour
                 controlObj.localRotation = newRotation;
             }
         }
-        _magCompleteTemp = MagCalibrater.Complete;
+        _magCompleteTemp = IsMagCompleted();
         if (!magComplete && _magCompleteTemp) {
             StartCoroutine(SaveMag());
             PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("tip_mag-calibrate_success"));

+ 17 - 8
Assets/BowArrow/Scripts/Bluetooth/BluetoothAim.cs

@@ -519,17 +519,26 @@ public class BluetoothAim : MonoBehaviour
     }
 
     void UploadMacAddress(byte[] bytes) {
-        if (hasData) return;
-        string str = System.Text.Encoding.ASCII.GetString (bytes);
-        if (str == null) return;
-        if (str.Contains(":")) {
-            if (!str.Equals(LoginMgr.myUserInfo.mac)) {
-                LoginMgr.myUserInfo.mac = str;
-            }
-            SaveMac(str);
+        string mac = System.Text.Encoding.ASCII.GetString(bytes);
+        if (mac != null) mac = mac.Trim();
+        if (CheckIsMacValid(mac)) {
+            LoginMgr.myUserInfo.mac = mac;
+            SaveMac(mac);
         }
     }
 
+    bool CheckIsMacValid(string mac) {
+        if (mac == null) return false;
+        if (!mac.StartsWith("{")) return false;
+        if (!mac.EndsWith("}")) return false;
+        if (!mac.Contains(":")) return false;
+        char[] validChars = {'{','}',':','0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
+        foreach (var c in mac.ToCharArray()) {
+            if (Array.IndexOf(validChars, c) == -1) return false;
+        }
+        return true;
+    }
+
     void SaveMac(string mac) {
         Action<Newtonsoft.Json.Linq.JToken> cb = (Newtonsoft.Json.Linq.JToken o) => {
             string gyrStr = o.Value<string>("gyr");

+ 34 - 30
Assets/BowArrow/Scripts/View/DeviceCalibrateView.cs

@@ -59,10 +59,10 @@ public class DeviceCalibrateView : MonoBehaviour, MenuBackInterface
         });
         if (guide) { //看看哪项没校准,就引导校准
             List<DeviceCalibrateItem> guideList = new List<DeviceCalibrateItem>();
-            if (string.IsNullOrEmpty(PlayerPrefs.GetString("o0GyrCalibrater", null))) {
+            if (!AimHandler.ins.IsGyrCompleted()) {
                 guideList.Add(DeviceCalibrateItem.Gyr);
             }
-            if (string.IsNullOrEmpty(PlayerPrefs.GetString("new_mag_record", null))) {
+            if (!AimHandler.ins.IsMagCompleted()) {
                 guideList.Add(DeviceCalibrateItem.Mag);
             }
             deviceCalibrateItemForGuide = guideList.ToArray();
@@ -135,6 +135,7 @@ public class DeviceCalibrateView : MonoBehaviour, MenuBackInterface
         this.transform.Find("Gyr").gameObject.SetActive(deviceCalibrateItem == DeviceCalibrateItem.Gyr);
         this.transform.Find("Mag").gameObject.SetActive(deviceCalibrateItem == DeviceCalibrateItem.Mag);
         RefreshResetMagBtn();
+        RefreshGyrBtn();
     }
 
     /* ------ 新地磁计校准(2022-10-3) ------ */
@@ -150,7 +151,7 @@ public class DeviceCalibrateView : MonoBehaviour, MenuBackInterface
         Color outColor;
         ColorUtility.TryParseHtmlString("#005AB6", out outColor);
         magReset.GetComponentInChildren<Text>().color = outColor;
-        if (AimHandler.ins.MagCalibrater.Complete) {
+        if (AimHandler.ins.IsMagCompleted()) {
             magReset.GetComponentInChildren<TextAutoLanguage>().SetText(116);
         } else {
             magReset.GetComponentInChildren<TextAutoLanguage>().SetText(85);
@@ -172,7 +173,7 @@ public class DeviceCalibrateView : MonoBehaviour, MenuBackInterface
         calibrateMagDoing = !calibrateMagDoing;
         if (calibrateMagDoing) {
             calibrateMagStartTime = Time.realtimeSinceStartup;
-            AimHandler.ins.MagCalibrater = new o0._9Axis.MagnetometerAutoCalibrater();
+            AimHandler.ins.ResetMag();
             flag_MagCalibarateOperateAndFinish = 0;
         }
         interactableAllSkipBtns(!calibrateMagDoing);
@@ -185,7 +186,7 @@ public class DeviceCalibrateView : MonoBehaviour, MenuBackInterface
     }
     void UpdateForMag() {
         if (deviceCalibrateItem == DeviceCalibrateItem.Mag) {
-            if (!AimHandler.ins.MagCalibrater.Complete) {
+            if (!AimHandler.ins.IsMagCompleted()) {
                 progressMagCalibrate.gameObject.SetActive(true);
                 magTipOk.SetActive(false);
                 //地磁校准超时提示
@@ -239,7 +240,6 @@ public class DeviceCalibrateView : MonoBehaviour, MenuBackInterface
             PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("device-calibrate_n-connect"));
             return;
         }
-        //Logic
         gyrCalibrating = !gyrCalibrating;
         interactableAllSkipBtns(!gyrCalibrating);
         if (gyrCalibrating) {
@@ -250,44 +250,24 @@ public class DeviceCalibrateView : MonoBehaviour, MenuBackInterface
             canUpdateGyrCalibrateProgress = false;
         }
         AimHandler.ins.CalibrateGyr(gyrCalibrating);
-        //UI
-        Button btn = btnGyrCalibrate.GetComponent<Button>();
-        if (gyrCalibrating) {
-            btn.GetComponentInChildren<TextAutoLanguage>().SetText(121);
-            btn.GetComponentInChildren<Text>().color = Color.red;
-        } else {
-            btn.GetComponentInChildren<TextAutoLanguage>().SetText(116);
-            Color outColor;
-            ColorUtility.TryParseHtmlString("#005AB6", out outColor);
-            btn.GetComponentInChildren<Text>().color = outColor;
-        }
+        AimHandler.ins.ResetGyr();
+        RefreshGyrBtn();
     }
 
     void FinishGyrCalibrate()
     {   
-        //Logic
         gyrCalibrating = false;
         interactableAllSkipBtns(true);
         canUpdateGyrCalibrateProgress = false;
         AimHandler.ins.CalibrateGyr(false);
         StartCoroutine(AimHandler.ins.SaveGyr());
-        //UI
-        Button btn = btnGyrCalibrate.GetComponent<Button>();
-        if (guide) {
-            btn.enabled = false;
-            btn.GetComponent<Image>().sprite = Resources.Load<Sprite>("Textures/Common/ButtonGray");
-            btn.GetComponentInChildren<Text>().color = Color.gray;
-        } else {
-            btn.GetComponentInChildren<TextAutoLanguage>().SetText(116);
-            Color outColor;
-            ColorUtility.TryParseHtmlString("#005AB6", out outColor);
-            btn.GetComponentInChildren<Text>().color = outColor;
-        }
+        RefreshGyrBtn();
     }
 
     bool canUpdateGyrCalibrateProgress = false;
     void UpdateForGyr()
     {
+        if (deviceCalibrateItem != DeviceCalibrateItem.Gyr) return;
         if (canUpdateGyrCalibrateProgress) {
             int progress = AimHandler.ins.gyrCalibrateCompleteCount * 100 / AimHandler.ins.gyrCalibrateTotalCount;
             progressGyrCalibrate.text = progress + "%";
@@ -298,6 +278,30 @@ public class DeviceCalibrateView : MonoBehaviour, MenuBackInterface
                     action_GyrCalibarateOperateAndFinish?.Invoke();
                 }
             }
+        } else {
+            if (AimHandler.ins.IsGyrCompleted()) progressGyrCalibrate.text = "100%";
+            else progressGyrCalibrate.text = "0%";
+        }
+    }
+
+    void RefreshGyrBtn() {
+        Button btn = btnGyrCalibrate.GetComponent<Button>();
+        if (guide) {
+            if (AimHandler.ins.IsGyrCompleted()) {
+                btn.enabled = false;
+                btn.GetComponent<Image>().sprite = Resources.Load<Sprite>("Textures/Common/ButtonGray");
+                btn.GetComponentInChildren<Text>().color = Color.gray;
+            }
+        } else {
+            if (gyrCalibrating) {
+                btn.GetComponentInChildren<TextAutoLanguage>().SetText(121);
+                btn.GetComponentInChildren<Text>().color = Color.red;
+            } else {
+                btn.GetComponentInChildren<TextAutoLanguage>().SetText(AimHandler.ins.IsGyrCompleted() ? 116 : 85);
+                Color outColor;
+                ColorUtility.TryParseHtmlString("#005AB6", out outColor);
+                btn.GetComponentInChildren<Text>().color = outColor;
+            }
         }
     }