PointerInteractable.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Assertions;
  4. namespace Rokid.UXR.Interaction
  5. {
  6. public abstract class PointerInteractable<TInteractor, TInteractable> : Interactable<TInteractor, TInteractable>,
  7. IPointable
  8. where TInteractor : Interactor<TInteractor, TInteractable>
  9. where TInteractable : PointerInteractable<TInteractor, TInteractable>
  10. {
  11. [SerializeField, Interface(typeof(IPointableElement)), Optional]
  12. private MonoBehaviour _pointableElement;
  13. public IPointableElement PointableElement { get; private set; }
  14. public event Action<PointerEvent> WhenPointerEventRaised = delegate { };
  15. public void PublishPointerEvent(PointerEvent evt)
  16. {
  17. WhenPointerEventRaised(evt);
  18. }
  19. protected override void Awake()
  20. {
  21. base.Awake();
  22. if (_pointableElement != null)
  23. {
  24. PointableElement = _pointableElement as IPointableElement;
  25. }
  26. }
  27. protected override void Start()
  28. {
  29. if (_pointableElement != null)
  30. {
  31. Assert.IsNotNull(PointableElement);
  32. }
  33. }
  34. protected override void OnEnable()
  35. {
  36. base.OnEnable();
  37. if (PointableElement != null)
  38. {
  39. WhenPointerEventRaised += PointableElement.ProcessPointerEvent;
  40. }
  41. }
  42. protected override void OnDisable()
  43. {
  44. if (PointableElement != null)
  45. {
  46. WhenPointerEventRaised -= PointableElement.ProcessPointerEvent;
  47. }
  48. base.OnDisable();
  49. }
  50. }
  51. }