FloatHoverUI.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. // Only used to detect whether it is a floating UI
  5. namespace Rokid.UXR.Interaction
  6. {
  7. public class FloatHoverUI : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
  8. {
  9. [SerializeField]
  10. private bool EnableFloat = true;
  11. private List<PointerEventData> saveEventDatas = new List<PointerEventData>();
  12. public void OnPointerEnter(PointerEventData eventData)
  13. {
  14. if (EnableFloat)
  15. {
  16. saveEventDatas.Add(eventData);
  17. RKPointerLisener.OnGraphicPointerEnter?.Invoke(eventData);
  18. }
  19. }
  20. public void OnPointerExit(PointerEventData eventData)
  21. {
  22. if (EnableFloat)
  23. {
  24. RKPointerLisener.OnGraphicPointerExit?.Invoke(eventData);
  25. saveEventDatas.Remove(eventData);
  26. }
  27. }
  28. // public void OnPointerMove(PointerEventData eventData)
  29. // {
  30. // if (EnableFloat)
  31. // {
  32. // RKPointerLisener.OnGraphicPointerHover?.Invoke(eventData);
  33. // }
  34. // }
  35. private void OnDisable()
  36. {
  37. if (EnableFloat && saveEventDatas.Count > 0)
  38. {
  39. foreach (var data in saveEventDatas)
  40. {
  41. RKPointerLisener.OnGraphicPointerExit?.Invoke(data);
  42. }
  43. saveEventDatas.Clear();
  44. }
  45. }
  46. private void OnDestroy()
  47. {
  48. if (EnableFloat && saveEventDatas.Count > 0)
  49. {
  50. foreach (var data in saveEventDatas)
  51. {
  52. RKPointerLisener.OnGraphicPointerExit?.Invoke(data);
  53. }
  54. saveEventDatas.Clear();
  55. }
  56. }
  57. }
  58. }