InteractiveScreen.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using NRKernal;
  2. using System;
  3. using UnityEngine;
  4. public class InteractiveScreen : MonoBehaviour
  5. {
  6. public event Action<TouchState> onUpdateTouchState;
  7. public struct TouchState
  8. {
  9. public Vector2 touchPoint;
  10. public Int64 timeStamp;
  11. public bool isTouching;
  12. public override string ToString()
  13. {
  14. return string.Format("touchPoint:{0} isTouching:{1}", touchPoint.ToString("F2"), isTouching);
  15. }
  16. }
  17. public TouchState touchState;
  18. void Update()
  19. {
  20. if (NRInput.GetButtonDown(ControllerButton.TRIGGER))
  21. {
  22. touchState.isTouching = true;
  23. }
  24. if (NRInput.GetButtonUp(ControllerButton.TRIGGER))
  25. {
  26. touchState.isTouching = false;
  27. }
  28. var handControllerAnchor = NRInput.DomainHand == ControllerHandEnum.Left ? ControllerAnchorEnum.LeftLaserAnchor : ControllerAnchorEnum.RightLaserAnchor;
  29. Transform laserAnchor = NRInput.AnchorsHelper.GetAnchor(NRInput.RaycastMode == RaycastModeEnum.Gaze ? ControllerAnchorEnum.GazePoseTrackerAnchor : handControllerAnchor);
  30. RaycastHit hitResult;
  31. if (Physics.Raycast(new Ray(laserAnchor.transform.position, laserAnchor.transform.forward), out hitResult, 10))
  32. {
  33. if (hitResult.collider != null && hitResult.collider is BoxCollider)
  34. {
  35. BoxCollider collider = hitResult.collider as BoxCollider;
  36. float scale_x, scale_y;
  37. scale_x = collider.transform.lossyScale.x * collider.size.x;
  38. scale_y = collider.transform.lossyScale.y * collider.size.y;
  39. Vector2 touchPoint = new Vector2(hitResult.point.x / scale_x + 0.5f, 0.5f - hitResult.point.y / scale_y);
  40. touchState.touchPoint = touchPoint;
  41. touchState.timeStamp = (Int64)(NRTools.GetTimeStampNanos());
  42. if (NRInput.GetButton(ControllerButton.TRIGGER) || NRInput.GetButtonUp(ControllerButton.TRIGGER))
  43. {
  44. onUpdateTouchState?.Invoke(touchState);
  45. }
  46. }
  47. }
  48. }
  49. }