GrabInteractable.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.Events;
  4. namespace Rokid.UXR.Interaction
  5. {
  6. public class GrabInteractable : MonoBehaviour, IHandHoverBegin, IHandHoverEnd, IGrabbedToHand, IReleasedFromHand
  7. {
  8. // [Tooltip("Hide the whole hand on grabbed and show on release")]
  9. // public bool hideHandOnGrabbed = true;
  10. [Tooltip("Specify whether you want to snap to the hand's object attachment point, or just the raw hand")]
  11. public bool useHandObjectAttachmentPoint = true;
  12. public bool attachEaseIn = false;
  13. [HideInInspector]
  14. public AnimationCurve snapAttachEaseInCurve = AnimationCurve.EaseInOut(0.0f, 0.0f, 1.0f, 1.0f);
  15. public float snapAttachEaseInTime = 0.15f;
  16. public bool snapAttachEaseInCompleted = false;
  17. [Tooltip("Set whether or not you want this interactible to highlight when hovering over it")]
  18. public bool changeScaleOnHover = true;
  19. [Tooltip("Higher is better")]
  20. public int hoverPriority = 0;
  21. [System.NonSerialized]
  22. public Hand grabbedToHand;
  23. [System.NonSerialized]
  24. public List<Hand> hoveringHands = new List<Hand>();
  25. [Tooltip("hoverScale/normalScale")]
  26. public float rate = 1.2f;
  27. private Vector3 hoverScale;
  28. private Vector3 normalScale;
  29. public UnityEvent OnPickUp = new UnityEvent();
  30. public UnityEvent OnDropDown = new UnityEvent();
  31. public UnityEvent OnHeldUpdate = new UnityEvent();
  32. public UnityEvent OnHoverBegin = new UnityEvent();
  33. public UnityEvent OnHoverEnd = new UnityEvent();
  34. public Hand hoveringHand
  35. {
  36. get
  37. {
  38. if (hoveringHands.Count > 0)
  39. return hoveringHands[0];
  40. return null;
  41. }
  42. }
  43. public bool isDestroying { get; protected set; }
  44. #region UnityEvent
  45. protected virtual void Start()
  46. {
  47. if (changeScaleOnHover)
  48. {
  49. normalScale = transform.localScale;
  50. hoverScale = normalScale * rate;
  51. }
  52. }
  53. private void OnDestroy()
  54. {
  55. isDestroying = true;
  56. if (grabbedToHand != null)
  57. {
  58. grabbedToHand.ReleaseObject(this.gameObject, false);
  59. }
  60. }
  61. #endregion
  62. #region HandEvent
  63. public void OnGrabbedToHand(Hand hand)
  64. {
  65. grabbedToHand = hand;
  66. SetScale(normalScale);
  67. OnPickUp?.Invoke();
  68. }
  69. public void OnReleasedFromHand(Hand hand)
  70. {
  71. grabbedToHand = null;
  72. OnDropDown.Invoke();
  73. }
  74. public void OnHandHoverBegin(Hand hand)
  75. {
  76. hoveringHands.Add(hand);
  77. SetScale(hoverScale);
  78. OnHoverBegin?.Invoke();
  79. }
  80. public void OnHandHoverEnd(Hand hand)
  81. {
  82. hoveringHands.Remove(hand);
  83. SetScale(normalScale);
  84. OnHoverEnd?.Invoke();
  85. }
  86. private void SetScale(Vector3 scale)
  87. {
  88. if (changeScaleOnHover)
  89. {
  90. transform.localScale = scale;
  91. }
  92. }
  93. #endregion
  94. }
  95. }