lvjincheng 4 лет назад
Родитель
Сommit
e6ff3f41dc

+ 17 - 6
Assets/BowArrow/Scripts/Bluetooth/AimHandler.cs

@@ -213,18 +213,29 @@ public class AimHandler : MonoBehaviour
         {
             if (bytes.Length == 2) {
                 if (bytes[0] == 0x66 && bytes[1] == 0x31) {
-                    DoIdentity();//视角回正
+                    if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) {
+                        //鼠标居中
+                        SB_EventSystem.ins.MakeMouseToScreenCenter(); 
+                    } else {
+                        //视角回正
+                        DoIdentity();
+                    }
                 } else if (bytes[0] == 0x66 && bytes[1] == 0x32) {
-                    if (SB_EventSystem.ins) SB_EventSystem.ins.AwakenSimulateMouse(); //唤起虚拟鼠标
+                    if (SB_EventSystem.ins) {
+                        //唤起/隐藏虚拟鼠标
+                        SB_EventSystem.ins.AwakenSimulateMouse(); 
+                    }
                 } else {
-                    DeviceBatteryView.ins.RenderBattery(1, bytes[0]); //显示电量
+                    //显示电量
+                    DeviceBatteryView.ins.RenderBattery(1, bytes[0]); 
                 }
             } else if (bytes[0] == 0x5b) {
                 if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) {
-                    //模拟鼠标处于激活状态
-                    SB_EventSystem.ins.ClickMouse();
+                    //点击鼠标
+                    SB_EventSystem.ins.ClickMouse(); 
                 } else {
-                    ShootCheck.ins.ShootByInfrared(bytes); //红外射击检测       
+                    //红外射击检测
+                    ShootCheck.ins.ShootByInfrared(bytes); 
                 }
             }
             return;

+ 154 - 37
Assets/BowArrow/Scripts/Expand/SB_EventSystem.cs

@@ -1,11 +1,15 @@
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
+using UnityEngine.UI;
 using JC.Unity;
+using DG.Tweening;
 
+/**SmartBow_事件系统 */
 public class SB_EventSystem : MonoBehaviour
 {   
     public static SB_EventSystem ins;
+    MouseTest mouseTest;
 
     void Awake()
     {
@@ -17,51 +21,164 @@ public class SB_EventSystem : MonoBehaviour
             AwakenSimulateMouse();
         }
     }
+
+    void Start() {
+        mouseTest = new MouseTest(this);
+        InitListenerForMouseClickHightColor();
+    }
+
+    void Update() {
+        UpdateMoveSimulateMouse();
+        mouseTest.Update();
+    }
     
     [SerializeField] SimulateMouse simulateMouse;
-    [System.NonSerialized] public bool simulateMouseIsAwaked;
-    public void AwakenSimulateMouse() {
-        simulateMouseIsAwaked = !simulateMouse.gameObject.activeSelf;
-        simulateMouse.gameObject.SetActive(simulateMouseIsAwaked);
-        hasAxisQuat = false;
-    }
-    Quaternion targetAxisQuat;
-    Quaternion nowAxisQuat;
-    bool hasAxisQuat;
-    Vector2 deltaVectorForMouse;
-    public void MoveSimulateMouse(Quaternion axisQuat) {
-        if (hasAxisQuat) {
-            Vector3 lastAngle = nowAxisQuat.eulerAngles;
-            targetAxisQuat = axisQuat;
-            nowAxisQuat = Quaternion.Lerp(nowAxisQuat, targetAxisQuat, Time.deltaTime * 15f);
-            Vector3 curAngle = nowAxisQuat.eulerAngles;
-            float dx = FormatDeltaAngleY(curAngle.y - lastAngle.y) / 72f * simulateMouse.GetScaleScreenWidth();
-            float dy = -FormatDeltaAngleX(curAngle.x - lastAngle.x) / 72f * simulateMouse.GetScaleScreenHeight();
-            deltaVectorForMouse.x = dx;
-            deltaVectorForMouse.y = dy;
-            simulateMouse.MoveMousePointer(deltaVectorForMouse);
-        } else {
-            nowAxisQuat = targetAxisQuat = axisQuat;
-            hasAxisQuat = true;
+
+    #region  客户要求鼠标点到按钮时,按钮高亮
+        Graphic lockGraphic = null;
+        Color pointerClickColor = Color.yellow;
+        void InitListenerForMouseClickHightColor() {
+            simulateMouse.OnPointerClick += (Selectable target) => {
+                if (lockGraphic) return;
+                Button btn = target.GetComponent<Button>();
+                if (btn.transition != Selectable.Transition.ColorTint) return;
+                if (btn.targetGraphic) {
+                    Graphic graphic = btn.targetGraphic;
+                    lockGraphic = graphic;
+                    Color oldColor = graphic.color;
+                    graphic.color = pointerClickColor;
+                    DoTweenUtil.CallDelay(0.3f, () => {
+                        lockGraphic = null;
+                        graphic.color = oldColor;    
+                    });
+                }
+            };
         }
-    }
+    #endregion
+
+    #region 鼠标激活/关闭
+        [System.NonSerialized] public bool simulateMouseIsAwaked;
+        public void AwakenSimulateMouse() {
+            simulateMouseIsAwaked = !simulateMouse.gameObject.activeSelf;
+            simulateMouse.gameObject.SetActive(simulateMouseIsAwaked);
+            hasAxisQuat = false;
+        }
+    #endregion 鼠标激活/关闭
+
+    #region 模拟鼠标移动
+        Vector3 targetNormalVector;
+        Quaternion targetAxisQuat;
+        Quaternion nowAxisQuat;
+        bool hasAxisQuat;
+        public void MoveSimulateMouse(Quaternion axisQuat) {
+            if (clickCooling) { //点击就是射出,射出会产生抖动,防止接收抖动后的数据
+                hasAxisQuat = false;
+                return;
+            } 
+            //格式化
+            Vector3 normalVector = axisQuat * Vector3.forward; //得出对应的法向量
+            axisQuat = Quaternion.LookRotation(normalVector); //变回四元组,主要作用是规范,去除z轴影响
+            if (hasAxisQuat) {
+                //法向量的z从+到-或从-到+,都会导致y轴转180度,也就是x轴旋转从<90跨越>90时触发的问题,这种情况要避免
+                if (
+                    normalVector.z <= 0 && targetNormalVector.z >= 0 ||
+                    normalVector.z >= 0 && targetNormalVector.z <= 0
+                ) {
+                    hasAxisQuat = false; //重置
+                    return;
+                }
+                //ok
+                targetAxisQuat = axisQuat;
+                targetNormalVector = normalVector;
+            } else {
+                nowAxisQuat = targetAxisQuat = axisQuat;
+                targetNormalVector = normalVector;
+                hasAxisQuat = true;
+            }
+        }
+        Vector2 deltaVectorForMouse;
+        void UpdateMoveSimulateMouse() {
+            if (!simulateMouseIsAwaked) return;
+            if (hasAxisQuat) {
+                Vector3 lastAngle = nowAxisQuat.eulerAngles;
+                nowAxisQuat = Quaternion.Lerp(nowAxisQuat, targetAxisQuat, Time.deltaTime * 15f);
+                Vector3 curAngle = nowAxisQuat.eulerAngles;
+                float dx = FormatDeltaAngleY(curAngle.y - lastAngle.y) / 150f * simulateMouse.GetScaleScreenWidth();
+                float dy = -FormatDeltaAngleX(curAngle.x - lastAngle.x) / 150f * simulateMouse.GetScaleScreenHeight();
+                deltaVectorForMouse.x = dx;
+                deltaVectorForMouse.y = dy;
+                simulateMouse.MoveMousePointer(deltaVectorForMouse);
+            }
+        }
+        float FormatDeltaAngleX(float value)
+        {
+            return FormatDeltaAngleY(value);
+        }
+        float FormatDeltaAngleY(float value) 
+        {
+            if (Mathf.Abs(value) > 180) {
+                if (value < 0) {
+                    return 360f + value; 
+                }
+                if (value > 0) {
+                    return value - 360f;
+                }
+            }
+            return value;
+        }
+    #endregion 模拟鼠标移动
+
+    //点击鼠标
+    bool clickCooling = false; //防止高频连续多次点击
     public void ClickMouse() {
+        if (clickCooling) return;
+        clickCooling = true;
+        DoTweenUtil.CallDelay(0.3f, () => {
+            clickCooling = false;
+        });
         simulateMouse.ClickMousePointer();
     }
-    float FormatDeltaAngleX(float value)
-    {
-        return FormatDeltaAngleY(value);
+
+    //鼠标居中
+    public void MakeMouseToScreenCenter() {
+        simulateMouse.MakeMouseToScreenCenter();
     }
-    float FormatDeltaAngleY(float value) 
-    {
-        if (Mathf.Abs(value) > 180) {
-            if (value < 0) {
-                return 360f + value; 
-            }
-            if (value > 0) {
-                return value - 360f;
+
+    /** 鼠标测试类 */
+    class MouseTest {
+        SB_EventSystem es;
+        float mouseTestSensitivity = 3f;
+        bool mouseTest = false; //开启测试-开关
+        public MouseTest(SB_EventSystem es) {
+            this.es = es;
+        }
+        public void Update() {
+            if (mouseTest) {
+                if (Input.GetKey(KeyCode.A)) {
+                    es.deltaVectorForMouse.x = -mouseTestSensitivity;
+                }
+                else if (Input.GetKey(KeyCode.D)) {
+                    es.deltaVectorForMouse.x = +mouseTestSensitivity;
+                } else {
+                    es.deltaVectorForMouse.x = 0;
+                }
+                if (Input.GetKey(KeyCode.W)) {
+                    es.deltaVectorForMouse.y = +mouseTestSensitivity;
+                }
+                else if (Input.GetKey(KeyCode.S)) {
+                    es.deltaVectorForMouse.y = -mouseTestSensitivity;
+                } else {
+                    es.deltaVectorForMouse.y = 0;
+                }
+                es.simulateMouse.MoveMousePointer(es.deltaVectorForMouse);
+                if (Input.GetKeyDown(KeyCode.E)) {
+                    es.AwakenSimulateMouse();
+                }
+                if (Input.GetKeyDown(KeyCode.R)) {
+                    es.simulateMouse.ClickMousePointer();
+                }
             }
         }
-        return value;
     }
 }
+

+ 8 - 0
Assets/BowArrow/Scripts/Util.meta

@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: 822b0e751b3b9744dac1ec3e6cc95d1c
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 9 - 0
Assets/BowArrow/Scripts/Util/DoTweenUtil.cs

@@ -0,0 +1,9 @@
+using DG.Tweening;
+
+public class DoTweenUtil {
+    public static void CallDelay(float delayTime, TweenCallback callback) {
+        Sequence sequence = DOTween.Sequence();
+        sequence.PrependInterval(delayTime).AppendCallback(callback);
+        sequence.SetUpdate(true);
+    }
+}

+ 11 - 0
Assets/BowArrow/Scripts/Util/DoTweenUtil.cs.meta

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

+ 15 - 4
Assets/DependAsset/JC/SimulateMouse/SimulateMouse.cs

@@ -11,6 +11,8 @@ namespace JC.Unity {
 	{
         [SerializeField] CanvasScaler canvasScaler;
         [SerializeField] RectTransform mousePointer;
+        [NonSerialized] public Action<Selectable> OnPointerEnter;
+        [NonSerialized] public Action<Selectable> OnPointerClick;
 		ScreenRayRaycasterUIWrapper mScreenRayRaycasterUIWrapper = new ScreenRayRaycasterUIWrapper();
  
 		void Awake()
@@ -20,7 +22,7 @@ namespace JC.Unity {
  
 			if (mScreenRayRaycasterUIWrapper != null) {
                  // 把鼠标的位置传 Ray 发射位置
-				mScreenRayRaycasterUIWrapper.Init(() => { 
+				mScreenRayRaycasterUIWrapper.Init(this, () => { 
                     Vector2 pos = mousePointer.anchoredPosition;
                     pos.x *= (float) Screen.width / GetScaleScreenWidth();
                     pos.y *= (float) Screen.height / GetScaleScreenHeight();
@@ -34,7 +36,7 @@ namespace JC.Unity {
         }
 
         void OnEnable() {
-            mousePointer.anchoredPosition = new Vector2(GetScaleScreenWidth()/2,GetScaleScreenHeight()/2);  
+            MakeMouseToScreenCenter();
         }
 
         void Update() {
@@ -94,6 +96,9 @@ namespace JC.Unity {
             {
                 mScreenRayRaycasterUIWrapper.Click();
             }
+            public void MakeMouseToScreenCenter() {
+                mousePointer.anchoredPosition = new Vector2(GetScaleScreenWidth()/2,GetScaleScreenHeight()/2);
+            }
         #endregion 对外提供的接口
 	}
 	public class ScreenRayRaycasterUIWrapper 
@@ -106,14 +111,16 @@ namespace JC.Unity {
  
         EventSystem m_eventSystem;
         PointerEventData m_pointerEvent;
- 
+
+        SimulateMouse mSimulateMouse;
         Func<Vector2> mFunPointerPos;
         private bool mIsClick = false;
         private bool mIsPress = false;
         private bool mIsDrag = false;
  
-        public void Init(Func<Vector2> funPointerPos)
+        public void Init(SimulateMouse simulateMouse, Func<Vector2> funPointerPos)
         {
+            mSimulateMouse = simulateMouse;
             m_eventSystem = EventSystem.current;
             m_pointerEvent = new PointerEventData(m_eventSystem);
             m_pointerEvent.button = PointerEventData.InputButton.Left;
@@ -157,6 +164,8 @@ namespace JC.Unity {
             {
                 if (m_clickHandler != null && mIsClick)
                 {
+                    try { mSimulateMouse.OnPointerClick?.Invoke(m_currentSelectable); }
+                    catch (System.Exception e) { Debug.LogError(e.Message); } 
                     m_clickHandler.OnPointerClick(m_pointerEvent);
                     Select(null);
                 }
@@ -184,6 +193,8 @@ namespace JC.Unity {
             m_currentSelectable = s;
  
             if (m_currentSelectable) {
+                try { mSimulateMouse.OnPointerEnter?.Invoke(m_currentSelectable); }
+                catch (System.Exception e) { Debug.LogError(e.Message); } 
                 m_currentSelectable.OnPointerEnter(m_pointerEvent);
                 m_clickHandler = m_currentSelectable.GetComponent<IPointerClickHandler>();
                 m_dragHandler = m_currentSelectable.GetComponent<IDragHandler>();