Просмотр исходного кода

自动连接,键盘导航自动选择首个选项

lvjincheng 2 лет назад
Родитель
Сommit
7639883ad4

+ 1 - 1
Assets/BowArrow/Scripts/Bluetooth/BluetoothAim.cs

@@ -136,7 +136,7 @@ public class BluetoothAim : MonoBehaviour
                 else
                 {
                     notUserDoConnectTime += Time.deltaTime;
-                    if (notUserDoConnectTime > 60)
+                    if (notUserDoConnectTime > 2.5f)
                     {
                         DoConnect();
                         notUserDoConnectTime = 0.1f;

+ 0 - 12
Assets/BowArrow/Scripts/Expand/SB_EventSystem/KeyBoardInterface.cs

@@ -18,17 +18,5 @@ public class KeyBoardInterface : MonoBehaviour
         {
             KeyBoardSelector.ins.Enter();
         }
-        if (Input.GetKeyDown(KeyCode.Escape))
-        {
-            HandleEscape();
-        }
-    }
-
-    public void HandleEscape()
-    {
-        if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
-        {
-            PersistenHandler.ins.menuBackCtr.HandleKeyDownEscape();
-        }
     }
 }

+ 11 - 7
Assets/BowArrow/Scripts/Expand/SB_EventSystem/KeyBoardNavigation.cs

@@ -41,18 +41,22 @@ public class KeyBoardNavigation : MonoBehaviour
 
     void Awake()
     {
-        if (CommonConfig.StandaloneMode)
-        {
-            NavigationSet.Add(this);
-        }
-        else
+        if (!CommonConfig.StandaloneMode)
         {
             Destroy(this);
         }
     }
 
-    void OnDestroy()
+    void OnEnable()
     {
-        NavigationSet.Remove(this);
+        NavigationSet.Add(this);
+    }
+
+    void OnDisable()
+    {
+        if (NavigationSet.Contains(this))
+        {
+            NavigationSet.Remove(this);
+        }
     }
 }

+ 53 - 2
Assets/BowArrow/Scripts/Expand/SB_EventSystem/KeyBoardSelector.cs

@@ -12,6 +12,7 @@ public class KeyBoardSelector : MonoBehaviour
     EventSystem _eventSystem;
     PointerEventData _pointerEventData;
     public bool doing = false;
+    private float _lastAutoSetFirstBtnTime;
     public static KeyBoardSelector ins;
 
     void Start()
@@ -21,13 +22,36 @@ public class KeyBoardSelector : MonoBehaviour
         _pointerEventData = new PointerEventData(_eventSystem);
         gameObject.AddComponent<KeyBoardInterface>();
     }
-
+    
     void Update()
     {
         if (doing)
         {
             RefreshLastSelectBtn();
-            if (!IsLastBtnValid()) Quit();
+            if (SB_EventSystem.ins.simulateMouseIsAwaked)
+            {
+                if (!IsBtnCanReach(_lastSelectBtn)) Quit();
+            }
+            else
+            {
+                if (!IsLastBtnValid()) Quit();
+            }
+        }
+        else
+        {
+            if (Time.realtimeSinceStartup - _lastAutoSetFirstBtnTime > 0.2)
+            {
+                _lastAutoSetFirstBtnTime = Time.realtimeSinceStartup;
+                RefreshLastSelectBtn();
+                if (SB_EventSystem.ins.simulateMouseIsAwaked)
+                {
+                    if (!IsBtnCanReach(_lastSelectBtn)) SetFirstSelectBtn();
+                }
+                else
+                {
+                    if (!IsLastBtnValid()) SetFirstSelectBtn();
+                }
+            }
         }
     }
 
@@ -67,6 +91,20 @@ public class KeyBoardSelector : MonoBehaviour
         }
     }
 
+    void SetFirstSelectBtn()
+    {
+        doing = true;
+        RefreshLastSelectBtn();
+        UpdateSelectables();
+        if (_selectables.Count > 0)
+        {
+            Button nextOne = KeyBoardNavigation.Next(_selectables[0], 1, true);
+            if (nextOne == null) return;
+            _lastSelectBtn = nextOne;
+            SB_EventSystem.ins.mouseConfirm.SetSelectable(_lastSelectBtn);
+        }
+    }
+
     void UpdateSelectables()
     {
         _selectables.Clear();
@@ -100,6 +138,19 @@ public class KeyBoardSelector : MonoBehaviour
         return false;
     }
 
+    bool IsBtnCanReach(Button btn)
+    {
+        if (!btn || !btn.interactable || !btn.gameObject || !btn.gameObject.activeInHierarchy) return false;
+        List<RaycastResult> raycastResults = new List<RaycastResult>();
+        _pointerEventData.position = JCUnityLib.RectTransformUtils.GetPositionByPivot(btn.transform as RectTransform, Vector2.one * 0.5f);
+        _eventSystem.RaycastAll(_pointerEventData, raycastResults);
+        if (raycastResults.Count == 0) return false;
+        RaycastResult rs = raycastResults[0];
+        if (!rs.gameObject) return false;
+        Button rayHitBtn = rs.gameObject.GetComponentInParent<Button>();
+        return btn == rayHitBtn;
+    }
+
     void RefreshLastSelectBtn()
     {
         if (SB_EventSystem.ins.mouseConfirm._targetSelectable) _lastSelectBtn = SB_EventSystem.ins.mouseConfirm._targetSelectable.GetComponent<Button>();

+ 2 - 1
Assets/BowArrow/Scripts/Expand/SB_EventSystem/MouseConfirm/MouseConfirm.cs

@@ -24,12 +24,14 @@ public class MouseConfirm : MonoBehaviour
     }
 
     void Show() {
+        if (KeyBoardSelector.ins) return;
         if (!gameObject.activeSelf) gameObject.SetActive(true);
     }
 
     public void Hide() {
         ResumeTarget();
         _targetSelectable = null;
+        if (KeyBoardSelector.ins) return;
         if (gameObject.activeSelf) gameObject.SetActive(false);
     }
 
@@ -79,7 +81,6 @@ public class MouseConfirm : MonoBehaviour
             if (_mouseButton) _mouseButton.OnSelect();
         }
 
-        if (KeyBoardSelector.ins && KeyBoardSelector.ins.doing) return;
         Show();
     }
 

+ 4 - 1
Assets/BowArrow/Scripts/View/Home/HomeView.cs

@@ -137,10 +137,13 @@ public class HomeView : JCUnityLib.ViewBase
         switch (target)
         {
             case "开始游戏":
-                if ((CommonConfig.isReleaseVersion || CommonConfig.StandaloneMode) && !BluetoothStatus.IsAllConnected()) {
+#if !UNITY_EDITOR
+                if ((CommonConfig.isReleaseVersion || CommonConfig.StandaloneMode) && !BluetoothStatus.IsAllConnected())
+                {
                     PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请先连接设备"));
                     return;
                 }
+#endif
                 ViewMgr.Instance.ShowView<GameStartView>();
                 action_OnClickStartGame?.Invoke();
                 break;