UGUI—UIHoldPressButton

作者:追风剑情 发布于:2022-11-7 15:19 分类:Unity3d

  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Events;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.Serialization;
  6. /// <summary>
  7. /// 长按按钮
  8. /// </summary>
  9. public class UIHoldPressButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler
  10. {
  11. [Tooltip("持续按下x秒")]
  12. public float holdSecond = 3;
  13.  
  14. [Serializable]
  15. public class OnHoldPressedEvent : UnityEvent { }
  16. [FormerlySerializedAs("onHoldPressed")]
  17. [SerializeField]
  18. private OnHoldPressedEvent m_OnHoldPressed = new OnHoldPressedEvent();
  19.  
  20. public void OnPointerDown(PointerEventData eventData)
  21. {
  22. Invoke("OnHoldPressed", holdSecond);
  23. }
  24.  
  25. public void OnPointerUp(PointerEventData eventData)
  26. {
  27. CancelInvoke();
  28. }
  29.  
  30. public void OnPointerExit(PointerEventData eventData)
  31. {
  32. CancelInvoke();
  33. }
  34.  
  35. // 触发长按事件
  36. private void OnHoldPressed()
  37. {
  38. m_OnHoldPressed?.Invoke();
  39. }
  40. }

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号