123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- using VRTK;
- using ChivaXR.Op;
- using QFramework;
- public enum HandType { right,left }
- public enum ClickType
- {
- 连击,
- 单击
- }
- public class ExamHandleManager : MonoBehaviour
- {
- #region 提示模块
- [BoxGroup("提示信息绑定", CenterLabel = true)]
- [LabelText("手柄")]
- public HandType promptHandType = HandType.right;
- [BoxGroup("提示信息绑定", CenterLabel = true)]
- [LabelText("手柄按键")]
- public SDK_BaseController.ButtonTypes promptButtonType = SDK_BaseController.ButtonTypes.ButtonTwo;
- [BoxGroup("提示信息绑定", CenterLabel = true)]
- [LabelText("手柄按键触发类型")]
- public SDK_BaseController.ButtonPressTypes promptButtonPressType = SDK_BaseController.ButtonPressTypes.PressDown;
- [BoxGroup("提示信息绑定", CenterLabel = true)]
- [LabelText("点击类型")]
- public ClickType clickType;
- [ShowIf("clickType",Value = ClickType.连击)]
- [BoxGroup("提示信息绑定", CenterLabel = true)]
- [LabelText("点击次数")]
- public int needClickCount = 1;
- private int promptClikCount;
- /// <summary>
- /// 提示
- /// </summary>
- private void PromptContronl()
- {
- VRTK_ControllerReference handReference = null;
- switch (promptHandType)
- {
- case HandType.right:
- handReference = VRTK_DeviceFinder.GetControllerReferenceRightHand();
- break;
- case HandType.left:
- handReference = VRTK_DeviceFinder.GetControllerReferenceLeftHand();
- break;
- default:
- break;
- }
- if (VRTK_SDK_Bridge.GetControllerButtonState(promptButtonType, promptButtonPressType, handReference))
- {
- switch (clickType)
- {
- case ClickType.连击:
- if (promptClikCount == 0) StartCoroutine(StartPromptCount());
- promptClikCount++;
- if (promptClikCount == needClickCount)
- {
- StopCoroutine(StartPromptCount());
- promptClikCount = 0;
- ExamManagerForVR.instance.OpenPrompt();
- }
- break;
- case ClickType.单击:
- ExamManagerForVR.instance.OpenPrompt();
- break;
- default:
- break;
- }
- }
- }
- private IEnumerator StartPromptCount(float time = 0.5f)
- {
- yield return new WaitForSeconds(time);
- promptClikCount = 0;
- }
- #endregion
- #region 打开结束考试UI
- [BoxGroup("结束考试UI绑定", CenterLabel = true)]
- [LabelText("手柄")]
- public HandType endExamHandType = HandType.right;
- [BoxGroup("结束考试UI绑定", CenterLabel = true)]
- [LabelText("手柄按键")]
- public SDK_BaseController.ButtonTypes endExamButtonType = SDK_BaseController.ButtonTypes.Trigger;
- [BoxGroup("结束考试UI绑定", CenterLabel = true)]
- [LabelText("手柄按键触发类型")]
- public SDK_BaseController.ButtonPressTypes endExamButtonPressType = SDK_BaseController.ButtonPressTypes.PressDown;
- [BoxGroup("结束考试UI绑定", CenterLabel = true)]
- [LabelText("点击类型")]
- public ClickType endExamClickType;
- [ShowIf("endExamClickType", Value = ClickType.连击)]
- [BoxGroup("结束考试UI绑定", CenterLabel = true)]
- [LabelText("点击次数")]
- public int endExamNeedClickCount = 1;
- private int endExamClikCount = 0;
- private void EndExamContronl()
- {
- VRTK_ControllerReference handReference = null;
- switch (endExamHandType)
- {
- case HandType.right:
- handReference = VRTK_DeviceFinder.GetControllerReferenceRightHand();
- break;
- case HandType.left:
- handReference = VRTK_DeviceFinder.GetControllerReferenceLeftHand();
- break;
- default:
- break;
- }
- if (VRTK_SDK_Bridge.GetControllerButtonState(endExamButtonType, endExamButtonPressType, handReference))
- {
- switch (endExamClickType)
- {
- case ClickType.连击:
- if (endExamClikCount == 0) StartCoroutine(StartEndExamCount());
- endExamClikCount++;
- if (endExamClikCount == endExamNeedClickCount)
- {
- StopCoroutine(StartEndExamCount());
- endExamClikCount = 0;
- OpenExamResultPanel();
- }
- break;
- case ClickType.单击:
- OpenExamResultPanel();
- break;
- default:
- break;
- }
- }
- }
- private IEnumerator StartEndExamCount(float time = 0.8f)
- {
- yield return new WaitForSeconds(time);
- endExamClikCount = 0;
- }
- private void OpenExamResultPanel()
- {
- ExamResultPanel examResultPanel = FindObjectOfType<ExamResultPanel>();
- if (examResultPanel != null)
- {
- if (!examResultPanel.gameObject.activeSelf)
- {
- examResultPanel.gameObject.SetActive(true);
- }
-
- return;
- }
- EndExamPanel endExamPanel = FindObjectOfType<EndExamPanel>();
- if (endExamPanel == null)
- {
- ResMgr.Init();
- var resLoader = ResLoader.Allocate();
- GameObject uiPrefab = resLoader.LoadSync<GameObject>("EndExamCanvas");
- if (uiPrefab != null)
- {
- Instantiate(uiPrefab).transform.SetAsLastSibling();
- }
- }
- else
- {
- endExamPanel.SetSelfPositionAndDisableCameraMove();
- }
- }
- #endregion
- private void Update()
- {
- PromptContronl();
- EndExamContronl();
- }
- }
|