EventTriggerListener.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. namespace Rokid.MRC
  6. {
  7. /// <summary>
  8. /// 事件触发工具类
  9. /// </summary>
  10. public class EventTriggerListener : UnityEngine.EventSystems.EventTrigger
  11. {
  12. public delegate void VoidDelegate(GameObject go,PointerEventData eventData=null);
  13. public VoidDelegate onClick;
  14. public VoidDelegate onDown;
  15. public VoidDelegate onEnter;
  16. public VoidDelegate onExit;
  17. public VoidDelegate onUp;
  18. public VoidDelegate onSelect;
  19. public VoidDelegate onUpdateSelect;
  20. public VoidDelegate onDrag;
  21. public VoidDelegate onDragBegin;
  22. public VoidDelegate onDragEnd;
  23. public VoidDelegate onScroll;
  24. static public EventTriggerListener Get(GameObject go)
  25. {
  26. EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
  27. if (listener == null) listener = go.AddComponent<EventTriggerListener>();
  28. return listener;
  29. }
  30. public override void OnPointerClick(PointerEventData eventData)
  31. {
  32. if (onClick != null) onClick(gameObject, eventData);
  33. }
  34. public override void OnPointerDown(PointerEventData eventData)
  35. {
  36. if (onDown != null) onDown(gameObject, eventData);
  37. }
  38. public override void OnPointerEnter(PointerEventData eventData)
  39. {
  40. if (onEnter != null) onEnter(gameObject,eventData);
  41. }
  42. public override void OnPointerExit(PointerEventData eventData)
  43. {
  44. if (onExit != null) onExit(gameObject,eventData);
  45. }
  46. public override void OnPointerUp(PointerEventData eventData)
  47. {
  48. if (onUp != null) onUp(gameObject, eventData);
  49. }
  50. public override void OnSelect(BaseEventData eventData)
  51. {
  52. if (onSelect != null) onSelect(gameObject, (PointerEventData)eventData);
  53. }
  54. public override void OnUpdateSelected(BaseEventData eventData)
  55. {
  56. if (onUpdateSelect != null) onUpdateSelect(gameObject, (PointerEventData)eventData);
  57. }
  58. public override void OnBeginDrag(PointerEventData eventData)
  59. {
  60. if(onDragBegin != null)
  61. onDragBegin(gameObject, eventData);
  62. }
  63. public override void OnDrag(PointerEventData eventData)
  64. {
  65. if(onDrag != null)
  66. onDrag(gameObject, eventData);
  67. }
  68. public override void OnEndDrag(PointerEventData eventData)
  69. {
  70. if(onDragEnd != null)
  71. onDragEnd(gameObject, eventData);
  72. }
  73. public override void OnScroll(PointerEventData eventData)
  74. {
  75. if(onScroll != null)
  76. onScroll(gameObject, eventData);
  77. }
  78. }
  79. }