BaseNearInteractionTouchable.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using UnityEngine;
  7. namespace SC.XR.Unity.Module_InputSystem {
  8. /// <summary>
  9. /// Type of Events to receive from a PokePointer.
  10. /// </summary>
  11. public enum TouchableEventType {
  12. Touch,
  13. Pointer,
  14. }
  15. public enum NormalType {
  16. X,
  17. Y,
  18. Z,
  19. NX,
  20. NY,
  21. NZ,
  22. }
  23. /// <summary>
  24. /// Base class for all NearInteractionTouchables.
  25. /// </summary>
  26. /// <remarks>
  27. public abstract class BaseNearInteractionTouchable : MonoBehaviour {
  28. [SerializeField]
  29. protected TouchableEventType eventsToReceive = TouchableEventType.Touch;
  30. /// <summary>
  31. /// The type of event to receive.
  32. /// </summary>
  33. public TouchableEventType EventsToReceive { get => eventsToReceive; set => eventsToReceive = value; }
  34. public NormalType NormalType = NormalType.NZ;
  35. public Vector3 Normal {
  36. get {
  37. if(NormalType == NormalType.NZ) {
  38. return -transform.forward;
  39. } else if(NormalType == NormalType.Z) {
  40. return transform.forward;
  41. } else if(NormalType == NormalType.X) {
  42. return transform.right;
  43. } else if(NormalType == NormalType.NX) {
  44. return -transform.right;
  45. } else if(NormalType == NormalType.Y) {
  46. return transform.up;
  47. } else {
  48. return -transform.up;
  49. }
  50. }
  51. }
  52. [SerializeField]
  53. private Transform center;
  54. public Transform Center {
  55. get {
  56. return center != null ? center : transform;
  57. }
  58. }
  59. [Tooltip("Distance in front of the surface at which you will receive a touch completed event")]
  60. [SerializeField]
  61. protected float debounceThreshold = 0.01f;
  62. /// <summary>
  63. /// Distance in front of the surface at which you will receive a touch completed event.
  64. /// </summary>
  65. /// <remarks>
  66. /// When the touchable is active and the pointer distance becomes greater than +DebounceThreshold (i.e. in front of the surface),
  67. /// then the Touch Completed event is raised and the touchable object is released by the pointer.
  68. /// </remarks>
  69. public float DebounceThreshold { get => debounceThreshold; set => debounceThreshold = value; }
  70. protected virtual void OnValidate() {
  71. debounceThreshold = Math.Max(debounceThreshold, 0);
  72. }
  73. public abstract float DistanceToTouchable(Vector3 samplePoint, out Vector3 normal);
  74. }
  75. }