RKButton.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. using UnityEngine.Events;
  4. namespace Rokid.UXR.Interaction {
  5. /// <summary>
  6. /// 不考虑拖拽阈值的按钮
  7. /// </summary>
  8. public class RKButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler, IPointerEnterHandler
  9. {
  10. /// <summary>
  11. /// 点击事件
  12. /// </summary>
  13. [SerializeField]
  14. public float clickTime = 1f;
  15. /// <summary>
  16. /// 长按时间
  17. /// </summary>
  18. [SerializeField]
  19. public float longPressTime = 2f;
  20. [SerializeField, Tooltip("pointerDown->pointerExit过程会触发ButtonClick")]
  21. public bool exitTriggerClick = false;
  22. private float elapedTime;
  23. private bool pointerDown;
  24. private bool pointerUp;
  25. private bool pointerExit;
  26. public UnityEvent<PointerEventData> onPointerClick = new UnityEvent<PointerEventData>();
  27. public UnityEvent<PointerEventData> onPointerDown = new UnityEvent<PointerEventData>();
  28. public UnityEvent<PointerEventData> onLongClick = new UnityEvent<PointerEventData>();
  29. private PointerEventData eventData;
  30. private Vector3 pointerDownPos;
  31. private Vector3 pointerExitPos;
  32. private float result;
  33. public void OnPointerDown(PointerEventData eventData)
  34. {
  35. this.eventData = eventData;
  36. pointerDownPos = new Vector3(eventData.pointerCurrentRaycast.worldPosition.x,
  37. eventData.pointerCurrentRaycast.worldPosition.y, eventData.pointerCurrentRaycast.worldPosition.z);
  38. RKLog.Info("====RKButton==== pointerDownPos:" + pointerDownPos);
  39. pointerDown = true;
  40. pointerUp = false;
  41. onPointerDown?.Invoke(eventData);
  42. }
  43. public void OnPointerUp(PointerEventData eventData)
  44. {
  45. pointerUp = true;
  46. }
  47. public void OnPointerEnter(PointerEventData eventData)
  48. {
  49. pointerExit = false;
  50. }
  51. public void OnPointerExit(PointerEventData eventData)
  52. {
  53. pointerExitPos = new Vector3(eventData.pointerCurrentRaycast.worldPosition.x,
  54. eventData.pointerCurrentRaycast.worldPosition.y, eventData.pointerCurrentRaycast.worldPosition.z);
  55. RKLog.Debug("====RKButton==== pointerExitPos:" + pointerExitPos);
  56. Vector3 exitForward = pointerExitPos - pointerDownPos;
  57. result = Vector3.Dot(exitForward.normalized, transform.forward);
  58. pointerExit = true;
  59. }
  60. private void OnDisable()
  61. {
  62. result = 0;
  63. pointerDownPos = Vector3.zero;
  64. pointerExitPos = Vector3.zero;
  65. elapedTime = 0;
  66. }
  67. private void Update()
  68. {
  69. if (pointerDown)
  70. {
  71. elapedTime += Time.deltaTime;
  72. }
  73. if (pointerDown && pointerUp && pointerExit == false)
  74. {
  75. pointerDown = false;
  76. pointerUp = false;
  77. pointerExit = false;
  78. if (elapedTime < clickTime)
  79. {
  80. onPointerClick?.Invoke(eventData);
  81. }
  82. if (clickTime > longPressTime)
  83. {
  84. onLongClick?.Invoke(eventData);
  85. }
  86. eventData = null;
  87. elapedTime = 0;
  88. }
  89. if (pointerExit)
  90. {
  91. if (exitTriggerClick && pointerDown && elapedTime < clickTime && result != 0)
  92. {
  93. RKLog.Debug("====RKButton==== dot.result:" + result);
  94. onPointerClick?.Invoke(eventData);
  95. }
  96. if (clickTime > longPressTime)
  97. {
  98. onLongClick?.Invoke(eventData);
  99. }
  100. pointerDown = false;
  101. pointerUp = false;
  102. eventData = null;
  103. elapedTime = 0;
  104. }
  105. }
  106. }
  107. }