InteractableUnityEventWrapper.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using UnityEngine;
  2. using UnityEngine.Assertions;
  3. using UnityEngine.Events;
  4. namespace Rokid.UXR.Interaction
  5. {
  6. /// <summary>
  7. /// This component makes it possible to connect Interactables in the
  8. /// inspector to Unity Events that are broadcast on state changes.
  9. /// </summary>
  10. public class InteractableUnityEventWrapper : MonoBehaviour
  11. {
  12. [SerializeField, Interface(typeof(IInteractableView))]
  13. private MonoBehaviour _interactableView;
  14. private IInteractableView InteractableView;
  15. [SerializeField]
  16. private UnityEvent _whenHover;
  17. [SerializeField]
  18. private UnityEvent _whenUnhover;
  19. [SerializeField]
  20. private UnityEvent _whenSelect;
  21. [SerializeField]
  22. private UnityEvent _whenUnselect;
  23. [SerializeField]
  24. private UnityEvent _whenInteractorViewAdded;
  25. [SerializeField]
  26. private UnityEvent _whenInteractorViewRemoved;
  27. [SerializeField]
  28. private UnityEvent _whenSelectingInteractorViewAdded;
  29. [SerializeField]
  30. private UnityEvent _whenSelectingInteractorViewRemoved;
  31. #region Properties
  32. public UnityEvent WhenHover => _whenHover;
  33. public UnityEvent WhenUnhover => _whenUnhover;
  34. public UnityEvent WhenSelect => _whenSelect;
  35. public UnityEvent WhenUnselect => _whenUnselect;
  36. public UnityEvent WhenInteractorViewAdded => _whenInteractorViewAdded;
  37. public UnityEvent WhenInteractorViewRemoved => _whenInteractorViewRemoved;
  38. public UnityEvent WhenSelectingInteractorViewAdded => _whenSelectingInteractorViewAdded;
  39. public UnityEvent WhenSelectingInteractorViewRemoved => _whenSelectingInteractorViewRemoved;
  40. #endregion
  41. protected bool _started = false;
  42. protected virtual void Awake()
  43. {
  44. InteractableView = _interactableView as IInteractableView;
  45. }
  46. protected virtual void Start()
  47. {
  48. this.BeginStart(ref _started);
  49. Assert.IsNotNull(InteractableView);
  50. this.EndStart(ref _started);
  51. }
  52. protected virtual void OnEnable()
  53. {
  54. if (_started)
  55. {
  56. InteractableView.WhenStateChanged += HandleStateChanged;
  57. InteractableView.WhenInteractorViewAdded += HandleInteractorViewAdded;
  58. InteractableView.WhenInteractorViewRemoved += HandleInteractorViewRemoved;
  59. InteractableView.WhenSelectingInteractorViewAdded += HandleSelectingInteractorViewAdded;
  60. InteractableView.WhenSelectingInteractorViewRemoved += HandleSelectingInteractorViewRemoved;
  61. }
  62. }
  63. protected virtual void OnDisable()
  64. {
  65. if (_started)
  66. {
  67. InteractableView.WhenStateChanged -= HandleStateChanged;
  68. InteractableView.WhenStateChanged -= HandleStateChanged;
  69. InteractableView.WhenInteractorViewAdded -= HandleInteractorViewAdded;
  70. InteractableView.WhenInteractorViewRemoved -= HandleInteractorViewRemoved;
  71. InteractableView.WhenSelectingInteractorViewAdded -= HandleSelectingInteractorViewAdded;
  72. InteractableView.WhenSelectingInteractorViewRemoved -= HandleSelectingInteractorViewRemoved;
  73. }
  74. }
  75. private void HandleStateChanged(InteractableStateChangeArgs args)
  76. {
  77. switch (args.NewState)
  78. {
  79. case InteractableState.Normal:
  80. if (args.PreviousState == InteractableState.Hover)
  81. {
  82. _whenUnhover.Invoke();
  83. }
  84. break;
  85. case InteractableState.Hover:
  86. if (args.PreviousState == InteractableState.Normal)
  87. {
  88. _whenHover.Invoke();
  89. }
  90. else if (args.PreviousState == InteractableState.Select)
  91. {
  92. _whenUnselect.Invoke();
  93. }
  94. break;
  95. case InteractableState.Select:
  96. if (args.PreviousState == InteractableState.Hover)
  97. {
  98. if (_whenSelect != null)
  99. _whenSelect.Invoke();
  100. }
  101. break;
  102. }
  103. }
  104. private void HandleInteractorViewAdded(IInteractorView interactorView)
  105. {
  106. WhenInteractorViewAdded.Invoke();
  107. }
  108. private void HandleInteractorViewRemoved(IInteractorView interactorView)
  109. {
  110. WhenInteractorViewRemoved.Invoke();
  111. }
  112. private void HandleSelectingInteractorViewAdded(IInteractorView interactorView)
  113. {
  114. WhenSelectingInteractorViewAdded.Invoke();
  115. }
  116. private void HandleSelectingInteractorViewRemoved(IInteractorView interactorView)
  117. {
  118. WhenSelectingInteractorViewRemoved.Invoke();
  119. }
  120. }
  121. }