using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; using VRTK; public enum SelectDriction { //水平切换 Horizontal, //数值切换 Vertical } /// /// 引用手 /// public enum RenfenceHand { RightHand, LeftHand } public class ButtonChangeScro : MonoBehaviour { public RenfenceHand hand = RenfenceHand.LeftHand; /// /// 选择方向 /// public SelectDriction selectDirction; [Header("功能激活")] public bool active = true; /// /// 要切换到页面按钮 /// public ButtonItem[] btns; /// /// 目前正在操作的按钮的下标 /// public int curentIndex; public ButtonItem currentButtonItem; public void Start() { InitData(); } public void InitData() { curentIndex = 0; if (btns != null && btns.Length > 0) { currentButtonItem = btns[curentIndex]; currentButtonItem.Select(); } } /// /// 触控板按下 /// /// /// public void TouPadPressedHandle(object sender, ControllerInteractionEventArgs e) { if (active) { if (curentIndex == 0) btns[curentIndex].Select(); btns[curentIndex].UnSelct(); if (selectDirction == SelectDriction.Vertical) { if (e.touchpadAxis.y > 0.7f) { curentIndex--; if (curentIndex < 0) { curentIndex = CheckActiveBtnCount() - 1; } } else if (e.touchpadAxis.y < -0.7f) { curentIndex++; if (curentIndex >= CheckActiveBtnCount()) { curentIndex = 0; } } } else if (selectDirction == SelectDriction.Horizontal) { if (e.touchpadAxis.x < -0.7f) { curentIndex--; if (curentIndex < 0) { curentIndex = CheckActiveBtnCount() - 1; } } else if (e.touchpadAxis.x > 0.7f) { curentIndex++; if (curentIndex >= CheckActiveBtnCount()) { curentIndex = 0; } } } btns[curentIndex].Select(); } } public void TouPadPressedHandleConfirm(object sender, ControllerInteractionEventArgs e) { if (!active) return; if (e.touchpadAxis.x > 0.7f) { if (currentButtonItem != null) currentButtonItem.Cancellation(); currentButtonItem = btns[curentIndex]; currentButtonItem.Click(); } } public void TriggerPressedHandle(object sender, ControllerInteractionEventArgs e) { if (!active) return; if (currentButtonItem != null) currentButtonItem.Cancellation(); currentButtonItem = btns[curentIndex]; currentButtonItem.Click(); } private Vector2 GetTouchPadAxi(VRTK_ControllerReference contronllerReference) { return VRTK_SDK_Bridge.GetControllerAxis(SDK_BaseController.ButtonTypes.Touchpad, contronllerReference); } public void UnSelect() { foreach (var item in btns) item.UnSelct(); } public void SetItemState(int _index, bool _isClick = false) { btns[curentIndex].UnSelct(); curentIndex = _index; currentButtonItem = btns[curentIndex]; btns[curentIndex].Select(); if (_isClick == true) { Debug.Log(_index + " " + btns[_index].name); btns[curentIndex].Click(); } } private int CheckActiveBtnCount() { int tmpCount = 0; for (int i = 0; i < btns.Length; i++) { if (btns[i].gameObject.activeSelf) tmpCount++; } return tmpCount; } public void OnEnable() { RegisterHandEvent(); } public void OnDisable() { UnRegisterHandEvent(); } private void RegisterHandEvent() { if (active) { VRTK_ControllerEvents controllerEvent = GetHandEvent(); if (controllerEvent != null) { controllerEvent.TouchpadPressed += TouPadPressedHandle; //VRDeviceManager.instance.left_HandleEvent.TouchpadPressed += TouPadPressedHandleConfirm; controllerEvent.TriggerPressed += TriggerPressedHandle; } } } private void UnRegisterHandEvent() { if (active) { VRTK_ControllerEvents controllerEvent = GetHandEvent(); if (controllerEvent != null) { controllerEvent.TouchpadPressed -= TouPadPressedHandle; //VRDeviceManager.instance.left_HandleEvent.TouchpadPressed += TouPadPressedHandleConfirm; controllerEvent.TriggerPressed -= TriggerPressedHandle; } } } private VRTK_ControllerEvents GetHandEvent() { GameObject handObj; if (hand == RenfenceHand.LeftHand) handObj = VRTK_DeviceFinder.GetControllerLeftHand(); else handObj = VRTK_DeviceFinder.GetControllerLeftHand(); VRTK_ControllerEvents controllerEvent = null; if (handObj != null) controllerEvent = handObj.GetComponent(); return controllerEvent; } }