using System; using System.Collections.Generic; using UnityEngine.Events; using UnityEngine.Serialization; namespace UnityEngine.EventSystems { [AddComponentMenu("Event/Event Trigger")] /// /// Receives events from the EventSystem and calls registered functions for each event. /// /// /// The EventTrigger can be used to specify functions you wish to be called for each EventSystem event. /// You can assign multiple functions to a single event and whenever the EventTrigger receives that event it will call those functions in the order they were provided. /// /// NOTE: Attaching this component to a GameObject will make that object intercept ALL events, and no events will propagate to parent objects. /// /// /// There are two ways to intercept events: You could extend EventTrigger, and override the functions for the events you are interested in intercepting; as shown in this example: /// /// using UnityEngine; /// using UnityEngine.EventSystems; /// /// public class EventTriggerExample : EventTrigger /// { /// public override void OnBeginDrag(PointerEventData data) /// { /// Debug.Log("OnBeginDrag called."); /// } /// /// public override void OnCancel(BaseEventData data) /// { /// Debug.Log("OnCancel called."); /// } /// /// public override void OnDeselect(BaseEventData data) /// { /// Debug.Log("OnDeselect called."); /// } /// /// public override void OnDrag(PointerEventData data) /// { /// Debug.Log("OnDrag called."); /// } /// /// public override void OnDrop(PointerEventData data) /// { /// Debug.Log("OnDrop called."); /// } /// /// public override void OnEndDrag(PointerEventData data) /// { /// Debug.Log("OnEndDrag called."); /// } /// /// public override void OnInitializePotentialDrag(PointerEventData data) /// { /// Debug.Log("OnInitializePotentialDrag called."); /// } /// /// public override void OnMove(AxisEventData data) /// { /// Debug.Log("OnMove called."); /// } /// /// public override void OnPointerClick(PointerEventData data) /// { /// Debug.Log("OnPointerClick called."); /// } /// /// public override void OnPointerDown(PointerEventData data) /// { /// Debug.Log("OnPointerDown called."); /// } /// /// public override void OnPointerEnter(PointerEventData data) /// { /// Debug.Log("OnPointerEnter called."); /// } /// /// public override void OnPointerExit(PointerEventData data) /// { /// Debug.Log("OnPointerExit called."); /// } /// /// public override void OnPointerUp(PointerEventData data) /// { /// Debug.Log("OnPointerUp called."); /// } /// /// public override void OnScroll(PointerEventData data) /// { /// Debug.Log("OnScroll called."); /// } /// /// public override void OnSelect(BaseEventData data) /// { /// Debug.Log("OnSelect called."); /// } /// /// public override void OnSubmit(BaseEventData data) /// { /// Debug.Log("OnSubmit called."); /// } /// /// public override void OnUpdateSelected(BaseEventData data) /// { /// Debug.Log("OnUpdateSelected called."); /// } /// } /// /// or you can specify individual delegates: /// /// using UnityEngine; /// using UnityEngine.EventSystems; /// /// /// public class EventTriggerDelegateExample : MonoBehaviour /// { /// void Start() /// { /// EventTrigger trigger = GetComponent(); /// EventTrigger.Entry entry = new EventTrigger.Entry(); /// entry.eventID = EventTriggerType.PointerDown; /// entry.callback.AddListener((data) => { OnPointerDownDelegate((PointerEventData)data); }); /// trigger.triggers.Add(entry); /// } /// /// public void OnPointerDownDelegate(PointerEventData data) /// { /// Debug.Log("OnPointerDownDelegate called."); /// } /// } /// /// public class EventTrigger : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler, IPointerClickHandler, IInitializePotentialDragHandler, IBeginDragHandler, IDragHandler, IEndDragHandler, IDropHandler, IScrollHandler, IUpdateSelectedHandler, ISelectHandler, IDeselectHandler, IMoveHandler, ISubmitHandler, ICancelHandler { [Serializable] /// /// UnityEvent class for Triggers. /// public class TriggerEvent : UnityEvent {} [Serializable] /// /// An Entry in the EventSystem delegates list. /// /// /// It stores the callback and which event type should this callback be fired. /// public class Entry { /// /// What type of event is the associated callback listening for. /// public EventTriggerType eventID = EventTriggerType.PointerClick; /// /// The desired TriggerEvent to be Invoked. /// public TriggerEvent callback = new TriggerEvent(); } [FormerlySerializedAs("delegates")] [SerializeField] private List m_Delegates; [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] [Obsolete("Please use triggers instead (UnityUpgradable) -> triggers", true)] public List delegates { get { return triggers; } set { triggers = value; } } protected EventTrigger() {} /// /// All the functions registered in this EventTrigger /// public List triggers { get { if (m_Delegates == null) m_Delegates = new List(); return m_Delegates; } set { m_Delegates = value; } } private void Execute(EventTriggerType id, BaseEventData eventData) { for (int i = 0, imax = triggers.Count; i < imax; ++i) { var ent = triggers[i]; if (ent.eventID == id && ent.callback != null) ent.callback.Invoke(eventData); } } /// /// Called by the EventSystem when the pointer enters the object associated with this EventTrigger. /// public virtual void OnPointerEnter(PointerEventData eventData) { Execute(EventTriggerType.PointerEnter, eventData); } /// /// Called by the EventSystem when the pointer exits the object associated with this EventTrigger. /// public virtual void OnPointerExit(PointerEventData eventData) { Execute(EventTriggerType.PointerExit, eventData); } /// /// Called by the EventSystem every time the pointer is moved during dragging. /// public virtual void OnDrag(PointerEventData eventData) { Execute(EventTriggerType.Drag, eventData); } /// /// Called by the EventSystem when an object accepts a drop. /// public virtual void OnDrop(PointerEventData eventData) { Execute(EventTriggerType.Drop, eventData); } /// /// Called by the EventSystem when a PointerDown event occurs. /// public virtual void OnPointerDown(PointerEventData eventData) { Execute(EventTriggerType.PointerDown, eventData); } /// /// Called by the EventSystem when a PointerUp event occurs. /// public virtual void OnPointerUp(PointerEventData eventData) { Execute(EventTriggerType.PointerUp, eventData); } /// /// Called by the EventSystem when a Click event occurs. /// public virtual void OnPointerClick(PointerEventData eventData) { Execute(EventTriggerType.PointerClick, eventData); } /// /// Called by the EventSystem when a Select event occurs. /// public virtual void OnSelect(BaseEventData eventData) { Execute(EventTriggerType.Select, eventData); } /// /// Called by the EventSystem when a new object is being selected. /// public virtual void OnDeselect(BaseEventData eventData) { Execute(EventTriggerType.Deselect, eventData); } /// /// Called by the EventSystem when a new Scroll event occurs. /// public virtual void OnScroll(PointerEventData eventData) { Execute(EventTriggerType.Scroll, eventData); } /// /// Called by the EventSystem when a Move event occurs. /// public virtual void OnMove(AxisEventData eventData) { Execute(EventTriggerType.Move, eventData); } /// /// Called by the EventSystem when the object associated with this EventTrigger is updated. /// public virtual void OnUpdateSelected(BaseEventData eventData) { Execute(EventTriggerType.UpdateSelected, eventData); } /// /// Called by the EventSystem when a drag has been found, but before it is valid to begin the drag. /// public virtual void OnInitializePotentialDrag(PointerEventData eventData) { Execute(EventTriggerType.InitializePotentialDrag, eventData); } /// /// Called before a drag is started. /// public virtual void OnBeginDrag(PointerEventData eventData) { Execute(EventTriggerType.BeginDrag, eventData); } /// /// Called by the EventSystem once dragging ends. /// public virtual void OnEndDrag(PointerEventData eventData) { Execute(EventTriggerType.EndDrag, eventData); } /// /// Called by the EventSystem when a Submit event occurs. /// public virtual void OnSubmit(BaseEventData eventData) { Execute(EventTriggerType.Submit, eventData); } /// /// Called by the EventSystem when a Cancel event occurs. /// public virtual void OnCancel(BaseEventData eventData) { Execute(EventTriggerType.Cancel, eventData); } } }