ButtonChangeScro.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. using VRTK;
  7. public enum SelectDriction
  8. {
  9. //水平切换
  10. Horizontal,
  11. //数值切换
  12. Vertical
  13. }
  14. /// <summary>
  15. /// 引用手
  16. /// </summary>
  17. public enum RenfenceHand
  18. {
  19. RightHand,
  20. LeftHand
  21. }
  22. public class ButtonChangeScro : MonoBehaviour
  23. {
  24. public RenfenceHand hand = RenfenceHand.LeftHand;
  25. /// <summary>
  26. /// 选择方向
  27. /// </summary>
  28. public SelectDriction selectDirction;
  29. [Header("功能激活")]
  30. public bool active = true;
  31. /// <summary>
  32. /// 要切换到页面按钮
  33. /// </summary>
  34. public ButtonItem[] btns;
  35. /// <summary>
  36. /// 目前正在操作的按钮的下标
  37. /// </summary>
  38. public int curentIndex;
  39. public ButtonItem currentButtonItem;
  40. public void Start()
  41. {
  42. InitData();
  43. }
  44. public void InitData()
  45. {
  46. curentIndex = 0;
  47. if (btns != null && btns.Length > 0)
  48. {
  49. currentButtonItem = btns[curentIndex];
  50. currentButtonItem.Select();
  51. }
  52. }
  53. /// <summary>
  54. /// 触控板按下
  55. /// </summary>
  56. /// <param name="sender"></param>
  57. /// <param name="e"></param>
  58. public void TouPadPressedHandle(object sender, ControllerInteractionEventArgs e)
  59. {
  60. if (active)
  61. {
  62. if (curentIndex == 0) btns[curentIndex].Select();
  63. btns[curentIndex].UnSelct();
  64. if (selectDirction == SelectDriction.Vertical)
  65. {
  66. if (e.touchpadAxis.y > 0.7f)
  67. {
  68. curentIndex--;
  69. if (curentIndex < 0)
  70. {
  71. curentIndex = CheckActiveBtnCount() - 1;
  72. }
  73. }
  74. else if (e.touchpadAxis.y < -0.7f)
  75. {
  76. curentIndex++;
  77. if (curentIndex >= CheckActiveBtnCount())
  78. {
  79. curentIndex = 0;
  80. }
  81. }
  82. }
  83. else if (selectDirction == SelectDriction.Horizontal)
  84. {
  85. if (e.touchpadAxis.x < -0.7f)
  86. {
  87. curentIndex--;
  88. if (curentIndex < 0)
  89. {
  90. curentIndex = CheckActiveBtnCount() - 1;
  91. }
  92. }
  93. else if (e.touchpadAxis.x > 0.7f)
  94. {
  95. curentIndex++;
  96. if (curentIndex >= CheckActiveBtnCount())
  97. {
  98. curentIndex = 0;
  99. }
  100. }
  101. }
  102. btns[curentIndex].Select();
  103. }
  104. }
  105. public void TouPadPressedHandleConfirm(object sender, ControllerInteractionEventArgs e)
  106. {
  107. if (!active) return;
  108. if (e.touchpadAxis.x > 0.7f)
  109. {
  110. if (currentButtonItem != null) currentButtonItem.Cancellation();
  111. currentButtonItem = btns[curentIndex];
  112. currentButtonItem.Click();
  113. }
  114. }
  115. public void TriggerPressedHandle(object sender, ControllerInteractionEventArgs e)
  116. {
  117. if (!active) return;
  118. if (currentButtonItem != null) currentButtonItem.Cancellation();
  119. currentButtonItem = btns[curentIndex];
  120. currentButtonItem.Click();
  121. }
  122. private Vector2 GetTouchPadAxi(VRTK_ControllerReference contronllerReference)
  123. {
  124. return VRTK_SDK_Bridge.GetControllerAxis(SDK_BaseController.ButtonTypes.Touchpad, contronllerReference);
  125. }
  126. public void UnSelect()
  127. {
  128. foreach (var item in btns) item.UnSelct();
  129. }
  130. public void SetItemState(int _index, bool _isClick = false)
  131. {
  132. btns[curentIndex].UnSelct();
  133. curentIndex = _index;
  134. currentButtonItem = btns[curentIndex];
  135. btns[curentIndex].Select();
  136. if (_isClick == true)
  137. {
  138. Debug.Log(_index + " " + btns[_index].name);
  139. btns[curentIndex].Click();
  140. }
  141. }
  142. private int CheckActiveBtnCount()
  143. {
  144. int tmpCount = 0;
  145. for (int i = 0; i < btns.Length; i++)
  146. {
  147. if (btns[i].gameObject.activeSelf) tmpCount++;
  148. }
  149. return tmpCount;
  150. }
  151. public void OnEnable()
  152. {
  153. RegisterHandEvent();
  154. }
  155. public void OnDisable()
  156. {
  157. UnRegisterHandEvent();
  158. }
  159. private void RegisterHandEvent()
  160. {
  161. if (active)
  162. {
  163. VRTK_ControllerEvents controllerEvent = GetHandEvent();
  164. if (controllerEvent != null)
  165. {
  166. controllerEvent.TouchpadPressed += TouPadPressedHandle;
  167. //VRDeviceManager.instance.left_HandleEvent.TouchpadPressed += TouPadPressedHandleConfirm;
  168. controllerEvent.TriggerPressed += TriggerPressedHandle;
  169. }
  170. }
  171. }
  172. private void UnRegisterHandEvent()
  173. {
  174. if (active)
  175. {
  176. VRTK_ControllerEvents controllerEvent = GetHandEvent();
  177. if (controllerEvent != null)
  178. {
  179. controllerEvent.TouchpadPressed -= TouPadPressedHandle;
  180. //VRDeviceManager.instance.left_HandleEvent.TouchpadPressed += TouPadPressedHandleConfirm;
  181. controllerEvent.TriggerPressed -= TriggerPressedHandle;
  182. }
  183. }
  184. }
  185. private VRTK_ControllerEvents GetHandEvent()
  186. {
  187. GameObject handObj;
  188. if (hand == RenfenceHand.LeftHand) handObj = VRTK_DeviceFinder.GetControllerLeftHand();
  189. else handObj = VRTK_DeviceFinder.GetControllerLeftHand();
  190. VRTK_ControllerEvents controllerEvent = null;
  191. if (handObj != null) controllerEvent = handObj.GetComponent<VRTK_ControllerEvents>();
  192. return controllerEvent;
  193. }
  194. }