123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- using VRTK;
- public enum SelectDriction
- {
- //水平切换
- Horizontal,
- //数值切换
- Vertical
- }
- /// <summary>
- /// 引用手
- /// </summary>
- public enum RenfenceHand
- {
- RightHand,
- LeftHand
- }
- public class ButtonChangeScro : MonoBehaviour
- {
- public RenfenceHand hand = RenfenceHand.LeftHand;
- /// <summary>
- /// 选择方向
- /// </summary>
- public SelectDriction selectDirction;
- [Header("功能激活")]
- public bool active = true;
- /// <summary>
- /// 要切换到页面按钮
- /// </summary>
- public ButtonItem[] btns;
- /// <summary>
- /// 目前正在操作的按钮的下标
- /// </summary>
- 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();
- }
- }
- /// <summary>
- /// 触控板按下
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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<VRTK_ControllerEvents>();
- return controllerEvent;
- }
- }
|