AutoClick.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. namespace SC.XR.Unity.Module_InputSystem {
  5. public class AutoClick : PointerHandler {
  6. public float autoClickTime = 3;
  7. Coroutine coroutineAddKey;
  8. bool isAddKeyFinish = false;
  9. public override void OnPointerEnter(PointerEventData eventData) {
  10. base.OnPointerEnter(eventData);
  11. StartCountDown((eventData as SCPointEventData), autoClickTime);
  12. }
  13. public override void OnPointerDown(PointerEventData eventData) {
  14. base.OnPointerDown(eventData);
  15. StopCountDown((eventData as SCPointEventData));
  16. }
  17. public override void OnPointerExit(PointerEventData eventData) {
  18. base.OnPointerExit(eventData);
  19. StopCountDown((eventData as SCPointEventData));
  20. }
  21. void StartCountDown(SCPointEventData scData, float time) {
  22. if(scData == null)
  23. return;
  24. StopCountDown(scData);
  25. coroutineAddKey = StartCoroutine(AddKey(InputKeyCode.Enter, InputKeyState.DOWN, scData,time));
  26. scData.inputDevicePartBase.detectorBase.pointerBase.cursorBase.StartGazeAnimation(time);
  27. }
  28. void StopCountDown(SCPointEventData scData) {
  29. if(scData == null)
  30. return;
  31. if(coroutineAddKey != null)
  32. StopCoroutine(coroutineAddKey);
  33. scData.inputDevicePartBase.detectorBase.pointerBase.cursorBase.StopGazeAnimation();
  34. if(isAddKeyFinish) {
  35. isAddKeyFinish = false;
  36. StartCoroutine(AddKey(InputKeyCode.Enter, InputKeyState.UP, scData,0.5f));
  37. }
  38. }
  39. IEnumerator AddKey(InputKeyCode keycode, InputKeyState state, SCPointEventData scData, float time) {
  40. yield return new WaitForSeconds(time);
  41. scData.inputDevicePartBase.inputDataBase.inputKeys.InputDataAddKey(keycode, state);
  42. isAddKeyFinish = true;
  43. }
  44. }
  45. }