BaseNearInteractionTouchable.cs 2.7 KB

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