Преглед изворни кода

更新AimHandler的controlObj获取方式

lvjincheng пре 4 година
родитељ
комит
f8ff467a28

+ 16 - 3
Assets/BowArrow/Scenes/Game.unity

@@ -250,7 +250,7 @@ GameObject:
   m_Icon: {fileID: 0}
   m_NavMeshLayer: 0
   m_StaticEditorFlags: 0
-  m_IsActive: 1
+  m_IsActive: 0
 --- !u!4 &22246723
 Transform:
   m_ObjectHideFlags: 0
@@ -1089,7 +1089,7 @@ Transform:
   - {fileID: 543450653046805445}
   - {fileID: 543450653274718124}
   m_Father: {fileID: 0}
-  m_RootOrder: 2
+  m_RootOrder: 3
   m_LocalEulerAnglesHint: {x: 0, y: 90, z: 0}
 --- !u!1 &133083727
 GameObject:
@@ -12119,6 +12119,7 @@ GameObject:
   serializedVersion: 6
   m_Component:
   - component: {fileID: 1680053129}
+  - component: {fileID: 1680053130}
   m_Layer: 0
   m_Name: CameraToLook
   m_TagString: Untagged
@@ -12139,8 +12140,20 @@ Transform:
   m_Children:
   - {fileID: 383624205}
   m_Father: {fileID: 0}
-  m_RootOrder: 3
+  m_RootOrder: 2
   m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
+--- !u!114 &1680053130
+MonoBehaviour:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_GameObject: {fileID: 1680053128}
+  m_Enabled: 1
+  m_EditorHideFlags: 0
+  m_Script: {fileID: 11500000, guid: 535cffd51d2f08a47817d1142453c8c8, type: 3}
+  m_Name: 
+  m_EditorClassIdentifier: 
 --- !u!1 &1680621885
 GameObject:
   m_ObjectHideFlags: 0

+ 28 - 32
Assets/BowArrow/Scripts/Bluetooth/AimHandler.cs

@@ -4,11 +4,21 @@ using System.Collections.Generic;
 using System.Linq;
 using UnityEngine.UI;
 using Newtonsoft.Json;
-using o0Aien;
 
 public class AimHandler : MonoBehaviour
 {
-    [SerializeField] Transform controlObj;
+    Transform controlObj {
+        get {
+            CameraToLook ctl = CameraToLook.ins;
+            if (ctl && ctl.isNew) 
+            {
+                ctl.isNew = false;
+                InitAutoIdentity();
+            }
+            if (ctl) return ctl.transform;
+            else return null;      
+        }
+    }
     [SerializeField] Button SetIdentityButton;
     [SerializeField] Button MagCalibrationButton;
     [SerializeField] Button GyrCalibrationButton;
@@ -285,56 +295,42 @@ public class AimHandler : MonoBehaviour
         }
         // 记录一些旋转角---end
 
-        receiveDataCount++;
-        if (!hasAutoIdentity && receiveDataCount == 5) {
-            doIdentity = true;
-        }
+        checkAutoIdentity();
     }   
 
-    void DoIdentity()
-    {
-        if (hasAutoIdentity)
-        {
-            doIdentity = true;
-            Debug.Log("reset identity");
-        }
-    }
-
     public void Update()
     {
-        if (hasAutoIdentity && controlObj)
+        if (controlObj && hasAutoIdentity)
         {
             // 最终结果进行lerp
             controlObj.localRotation = Quaternion.Lerp(controlObj.localRotation, newRotation, Time.deltaTime * 8);
             // controlObj.localRotation = newRotation;          
         }
-        if (doIdentity)
-        {
-            _9Axis.SetIdentity();
-            if (controlObj)
-            {
-                controlObj.localRotation = _9Axis.States.Last().Qua;
-            }
-            doIdentity = false;
-            hasAutoIdentity = true;
-        }
     }
 
     int receiveDataCount = 0;
-    bool doIdentity = false;
     bool hasAutoIdentity = false;
     Quaternion newRotation;
 
-    public void InitAutoIdentity() 
+    void InitAutoIdentity() 
     {
         receiveDataCount = 0;
-        doIdentity = false;
         hasAutoIdentity = false;
     }
 
-    public void SetControlObject(Transform obj)
+    void checkAutoIdentity() 
+    {
+        receiveDataCount++;
+        if (!hasAutoIdentity && receiveDataCount == 5) {
+            hasAutoIdentity = true;
+            DoIdentity();
+        }
+    }
+
+    void DoIdentity()
     {
-        controlObj = obj;
-        if (controlObj) InitAutoIdentity();
+        if (!hasAutoIdentity) return;
+        _9Axis.SetIdentity();
+        if (controlObj) controlObj.localRotation = _9Axis.States.Last().Qua;
     }
 }

+ 19 - 0
Assets/BowArrow/Scripts/Game/CameraToLook.cs

@@ -0,0 +1,19 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class CameraToLook : MonoBehaviour
+{
+    public bool isNew = true;
+    public static CameraToLook ins = null;
+
+    void Start()
+    {
+        ins = this;
+    }
+
+    void OnDestroy() 
+    {
+        ins = null;
+    }
+}

+ 11 - 0
Assets/BowArrow/Scripts/Game/CameraToLook.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 535cffd51d2f08a47817d1142453c8c8
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: