ExamHandleManager.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using VRTK;
  6. using ChivaXR.Op;
  7. using QFramework;
  8. public enum HandType { right,left }
  9. public enum ClickType
  10. {
  11. 连击,
  12. 单击
  13. }
  14. public class ExamHandleManager : MonoBehaviour
  15. {
  16. #region 提示模块
  17. [BoxGroup("提示信息绑定", CenterLabel = true)]
  18. [LabelText("手柄")]
  19. public HandType promptHandType = HandType.right;
  20. [BoxGroup("提示信息绑定", CenterLabel = true)]
  21. [LabelText("手柄按键")]
  22. public SDK_BaseController.ButtonTypes promptButtonType = SDK_BaseController.ButtonTypes.ButtonTwo;
  23. [BoxGroup("提示信息绑定", CenterLabel = true)]
  24. [LabelText("手柄按键触发类型")]
  25. public SDK_BaseController.ButtonPressTypes promptButtonPressType = SDK_BaseController.ButtonPressTypes.PressDown;
  26. [BoxGroup("提示信息绑定", CenterLabel = true)]
  27. [LabelText("点击类型")]
  28. public ClickType clickType;
  29. [ShowIf("clickType",Value = ClickType.连击)]
  30. [BoxGroup("提示信息绑定", CenterLabel = true)]
  31. [LabelText("点击次数")]
  32. public int needClickCount = 1;
  33. private int promptClikCount;
  34. /// <summary>
  35. /// 提示
  36. /// </summary>
  37. private void PromptContronl()
  38. {
  39. VRTK_ControllerReference handReference = null;
  40. switch (promptHandType)
  41. {
  42. case HandType.right:
  43. handReference = VRTK_DeviceFinder.GetControllerReferenceRightHand();
  44. break;
  45. case HandType.left:
  46. handReference = VRTK_DeviceFinder.GetControllerReferenceLeftHand();
  47. break;
  48. default:
  49. break;
  50. }
  51. if (VRTK_SDK_Bridge.GetControllerButtonState(promptButtonType, promptButtonPressType, handReference))
  52. {
  53. switch (clickType)
  54. {
  55. case ClickType.连击:
  56. if (promptClikCount == 0) StartCoroutine(StartPromptCount());
  57. promptClikCount++;
  58. if (promptClikCount == needClickCount)
  59. {
  60. StopCoroutine(StartPromptCount());
  61. promptClikCount = 0;
  62. ExamManagerForVR.instance.OpenPrompt();
  63. }
  64. break;
  65. case ClickType.单击:
  66. ExamManagerForVR.instance.OpenPrompt();
  67. break;
  68. default:
  69. break;
  70. }
  71. }
  72. }
  73. private IEnumerator StartPromptCount(float time = 0.5f)
  74. {
  75. yield return new WaitForSeconds(time);
  76. promptClikCount = 0;
  77. }
  78. #endregion
  79. #region 打开结束考试UI
  80. [BoxGroup("结束考试UI绑定", CenterLabel = true)]
  81. [LabelText("手柄")]
  82. public HandType endExamHandType = HandType.right;
  83. [BoxGroup("结束考试UI绑定", CenterLabel = true)]
  84. [LabelText("手柄按键")]
  85. public SDK_BaseController.ButtonTypes endExamButtonType = SDK_BaseController.ButtonTypes.Trigger;
  86. [BoxGroup("结束考试UI绑定", CenterLabel = true)]
  87. [LabelText("手柄按键触发类型")]
  88. public SDK_BaseController.ButtonPressTypes endExamButtonPressType = SDK_BaseController.ButtonPressTypes.PressDown;
  89. [BoxGroup("结束考试UI绑定", CenterLabel = true)]
  90. [LabelText("点击类型")]
  91. public ClickType endExamClickType;
  92. [ShowIf("endExamClickType", Value = ClickType.连击)]
  93. [BoxGroup("结束考试UI绑定", CenterLabel = true)]
  94. [LabelText("点击次数")]
  95. public int endExamNeedClickCount = 1;
  96. private int endExamClikCount = 0;
  97. private void EndExamContronl()
  98. {
  99. VRTK_ControllerReference handReference = null;
  100. switch (endExamHandType)
  101. {
  102. case HandType.right:
  103. handReference = VRTK_DeviceFinder.GetControllerReferenceRightHand();
  104. break;
  105. case HandType.left:
  106. handReference = VRTK_DeviceFinder.GetControllerReferenceLeftHand();
  107. break;
  108. default:
  109. break;
  110. }
  111. if (VRTK_SDK_Bridge.GetControllerButtonState(endExamButtonType, endExamButtonPressType, handReference))
  112. {
  113. switch (endExamClickType)
  114. {
  115. case ClickType.连击:
  116. if (endExamClikCount == 0) StartCoroutine(StartEndExamCount());
  117. endExamClikCount++;
  118. if (endExamClikCount == endExamNeedClickCount)
  119. {
  120. StopCoroutine(StartEndExamCount());
  121. endExamClikCount = 0;
  122. OpenExamResultPanel();
  123. }
  124. break;
  125. case ClickType.单击:
  126. OpenExamResultPanel();
  127. break;
  128. default:
  129. break;
  130. }
  131. }
  132. }
  133. private IEnumerator StartEndExamCount(float time = 0.8f)
  134. {
  135. yield return new WaitForSeconds(time);
  136. endExamClikCount = 0;
  137. }
  138. private void OpenExamResultPanel()
  139. {
  140. ExamResultPanel examResultPanel = FindObjectOfType<ExamResultPanel>();
  141. if (examResultPanel != null)
  142. {
  143. if (!examResultPanel.gameObject.activeSelf)
  144. {
  145. examResultPanel.gameObject.SetActive(true);
  146. }
  147. return;
  148. }
  149. EndExamPanel endExamPanel = FindObjectOfType<EndExamPanel>();
  150. if (endExamPanel == null)
  151. {
  152. ResMgr.Init();
  153. var resLoader = ResLoader.Allocate();
  154. GameObject uiPrefab = resLoader.LoadSync<GameObject>("EndExamCanvas");
  155. if (uiPrefab != null)
  156. {
  157. Instantiate(uiPrefab).transform.SetAsLastSibling();
  158. }
  159. }
  160. else
  161. {
  162. endExamPanel.SetSelfPositionAndDisableCameraMove();
  163. }
  164. }
  165. #endregion
  166. private void Update()
  167. {
  168. PromptContronl();
  169. EndExamContronl();
  170. }
  171. }