Is there a way to "pass on" UI events.

I have been experimenting with the new UI, and I keep getting into the same issue. Foreground UI elements blocking triggers/events tu underlying objects.

For example, I currently have a scroll rect, inside that a content area with objects that register pointerEnter and pointerExit triggers (for highlights). Adding these triggers will “capture” and block the scrollwheel events the scroll rect needs. Any way to “pass on” those events to the scroll rect? Am I missing something?

You can simply add those events to foreground objects too. So they will work as if foreground objects aren’t there.

Edit: My comment seemed a bit confusing to me so i felt a need to make clarification: you don’t need to write a new event for each event trigger. You can use same event for multiple triggers.

This is how I solved passing on any PointerEvent type to the next Raycast-blocking item underneath. It’s easy to implement some check for a specific Tag, Component etc. if you have many blocking objects stacked…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class PassOnPointerEvent : MonoBehaviour, IPointerDownHandler, IDragHandler, IBeginDragHandler, IPointerUpHandler, IEndDragHandler
{
    GameObject newTarget;

    public void OnPointerDown(PointerEventData eventData)
    {
        List<RaycastResult> raycastResults = new List<RaycastResult>();
        EventSystem.current.RaycastAll(eventData, raycastResults);
        newTarget = raycastResults[1].gameObject; //Array item 1 should be the one next underneath, handy to implement for-loop with check here if necessary.
        print($"Passing on click to {newTarget}"); //Just make sure you caught the right object

        ExecuteEvents.Execute(newTarget, eventData, ExecuteEvents.pointerDownHandler);
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        ExecuteEvents.Execute(newTarget, eventData, ExecuteEvents.pointerUpHandler);
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        ExecuteEvents.Execute(newTarget, eventData, ExecuteEvents.beginDragHandler);
    }

    public void OnDrag(PointerEventData eventData)
    {
        ExecuteEvents.Execute(newTarget, eventData, ExecuteEvents.dragHandler);
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        ExecuteEvents.Execute(newTarget, eventData, ExecuteEvents.endDragHandler);
    }  
}

Hope it can help someone!

I found the Scroll Rect wasn’t receiving it’s events if a container object had an Event Trigger component. It didn’t matter if the MouseDown, OnDrag (or whichever events the Scroll Rect is using) were being consumed - the Event Trigger sucks all the events dry and doesn’t seem to let anything through.

I was using an asset that made heavy use of the Event Trigger, and didn’t want to rewrite that. So I solved this by decompiling the Event Trigger and making my own version that included only the events I needed. Then it was simple to change the asset’s references from “EventTrigger” to “MyEventTrigger”

Code follows. Notice most of the event interfaces are commented out.

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Serialization;

namespace UnityEngine.EventSystems
{
    /// <summary>
    /// 
    /// <para>
    /// Receives events from the EventSystem and calls registered functions for each event.
    /// </para>
    /// 
    /// </summary>
    [AddComponentMenu("Event/Event Trigger")]
    public class MyEventTrigger : MonoBehaviour, 
        //IEventSystemHandler, 
        IPointerEnterHandler, 
        IPointerExitHandler, 
        //IPointerDownHandler, 
        //IPointerUpHandler, 
        IPointerClickHandler
        //IBeginDragHandler, 
        //IInitializePotentialDragHandler, 
        //IDragHandler, 
        //IEndDragHandler, 
        //IDropHandler, 
        //IScrollHandler, 
        //IUpdateSelectedHandler, 
        //ISelectHandler, 
        //IDeselectHandler, 
        //IMoveHandler, 
        //ISubmitHandler, 
        //ICancelHandler
    {
        [SerializeField]
        [FormerlySerializedAs("delegates")]
        private List<MyEventTrigger.Entry> m_Delegates;
        /// <summary>
        /// 
        /// <para>
        /// All the functions registered in this EventTrigger.
        /// </para>
        /// 
        /// </summary>
        [Obsolete("Please use triggers instead (UnityUpgradable) -> triggers", true)]
        public List<MyEventTrigger.Entry> delegates;

        /// <summary>
        /// 
        /// <para>
        /// All the functions registered in this EventTrigger.
        /// </para>
        /// 
        /// </summary>
        public List<MyEventTrigger.Entry> triggers
        {
            get
            {
                if (this.m_Delegates == null)
                    this.m_Delegates = new List<MyEventTrigger.Entry>();
                return this.m_Delegates;
            }
            set
            {
                this.m_Delegates = value;
            }
        }

        protected MyEventTrigger()
        {
        }

        private void Execute(EventTriggerType id, BaseEventData eventData)
        {
            int index = 0;
            for (int count = this.triggers.Count; index < count; ++index)
            {
                MyEventTrigger.Entry entry = this.triggers[index];
                if (entry.eventID == id && entry.callback != null)
                    entry.callback.Invoke(eventData);
            }
        }

        /// <summary>
        /// 
        /// <para>
        /// See [[IPointerEnterHandler.OnPointerEnter]].
        /// </para>
        /// 
        /// </summary>
        /// <param name="eventData"/>
        public virtual void OnPointerEnter(PointerEventData eventData)
        {
            this.Execute(EventTriggerType.PointerEnter, (BaseEventData)eventData);
        }

        /// <summary>
        /// 
        /// <para>
        /// See [[IPointerExitHandler.OnPointerExit]].
        /// </para>
        /// 
        /// </summary>
        /// <param name="eventData"/>
        public virtual void OnPointerExit(PointerEventData eventData)
        {
            this.Execute(EventTriggerType.PointerExit, (BaseEventData)eventData);
        }

        /// <summary>
        /// 
        /// <para>
        /// See [[IDragHandler.OnDrag]].
        /// </para>
        /// 
        /// </summary>
        /// <param name="eventData"/>
        public virtual void OnDrag(PointerEventData eventData)
        {
            this.Execute(EventTriggerType.Drag, (BaseEventData)eventData);
        }

        /// <summary>
        /// 
        /// <para>
        /// See [[IDropHandler.OnDrop]].
        /// </para>
        /// 
        /// </summary>
        /// <param name="eventData"/>
        public virtual void OnDrop(PointerEventData eventData)
        {
            this.Execute(EventTriggerType.Drop, (BaseEventData)eventData);
        }

        /// <summary>
        /// 
        /// <para>
        /// See [[IPointerDownHandler.OnPointerDown]].
        /// </para>
        /// 
        /// </summary>
        /// <param name="eventData"/>
        public virtual void OnPointerDown(PointerEventData eventData)
        {
            this.Execute(EventTriggerType.PointerDown, (BaseEventData)eventData);
        }

        /// <summary>
        /// 
        /// <para>
        /// See [[IPointerUpHandler.OnPointerUp]].
        /// </para>
        /// 
        /// </summary>
        /// <param name="eventData"/>
        public virtual void OnPointerUp(PointerEventData eventData)
        {
            this.Execute(EventTriggerType.PointerUp, (BaseEventData)eventData);
        }

        /// <summary>
        /// 
        /// <para>
        /// See [[IPointerClickHandler.OnPointerClick]].
        /// </para>
        /// 
        /// </summary>
        /// <param name="eventData"/>
        public virtual void OnPointerClick(PointerEventData eventData)
        {
            this.Execute(EventTriggerType.PointerClick, (BaseEventData)eventData);
        }

        /// <summary>
        /// 
        /// <para>
        /// See [[ISelectHandler.OnSelect]].
        /// </para>
        /// 
        /// </summary>
        /// <param name="eventData"/>
        public virtual void OnSelect(BaseEventData eventData)
        {
            this.Execute(EventTriggerType.Select, eventData);
        }

        /// <summary>
        /// 
        /// <para>
        /// See [[IDeselectHandler.OnDeselect]].
        /// </para>
        /// 
        /// </summary>
        /// <param name="eventData"/>
        public virtual void OnDeselect(BaseEventData eventData)
        {
            this.Execute(EventTriggerType.Deselect, eventData);
        }

        /// <summary>
        /// 
        /// <para>
        /// See [[IScrollHandler.OnScroll]].
        /// </para>
        /// 
        /// </summary>
        /// <param name="eventData"/>
        public virtual void OnScroll(PointerEventData eventData)
        {
            this.Execute(EventTriggerType.Scroll, (BaseEventData)eventData);
        }

        /// <summary>
        /// 
        /// <para>
        /// See [[IMoveHandler.OnMove]].
        /// </para>
        /// 
        /// </summary>
        /// <param name="eventData"/>
        public virtual void OnMove(AxisEventData eventData)
        {
            this.Execute(EventTriggerType.Move, (BaseEventData)eventData);
        }

        /// <summary>
        /// 
        /// <para>
        /// See [[IUpdateSelectedHandler.OnUpdateSelected]].
        /// </para>
        /// 
        /// </summary>
        /// <param name="eventData"/>
        public virtual void OnUpdateSelected(BaseEventData eventData)
        {
            this.Execute(EventTriggerType.UpdateSelected, eventData);
        }

        /// <summary>
        /// 
        /// <para>
        /// Called by a [[BaseInputModule]] when a drag has been found but before it is valid to begin the drag.
        /// </para>
        /// 
        /// </summary>
        /// <param name="eventData"/>
        public virtual void OnInitializePotentialDrag(PointerEventData eventData)
        {
            this.Execute(EventTriggerType.InitializePotentialDrag, (BaseEventData)eventData);
        }

        /// <summary>
        /// 
        /// <para>
        /// See [[IBeginDragHandler.OnBeginDrag]].
        /// </para>
        /// 
        /// </summary>
        /// <param name="eventData"/>
        public virtual void OnBeginDrag(PointerEventData eventData)
        {
            this.Execute(EventTriggerType.BeginDrag, (BaseEventData)eventData);
        }

        /// <summary>
        /// 
        /// <para>
        /// See See [[IBeginDragHandler.OnEndDrag]].
        /// </para>
        /// 
        /// </summary>
        /// <param name="eventData"/>
        public virtual void OnEndDrag(PointerEventData eventData)
        {
            this.Execute(EventTriggerType.EndDrag, (BaseEventData)eventData);
        }

        /// <summary>
        /// 
        /// <para>
        /// See [[ISubmitHandler.OnSubmit]].
        /// </para>
        /// 
        /// </summary>
        /// <param name="eventData"/>
        public virtual void OnSubmit(BaseEventData eventData)
        {
            this.Execute(EventTriggerType.Submit, eventData);
        }

        /// <summary>
        /// 
        /// <para>
        /// See [[ICancelHandler.OnCancel]].
        /// </para>
        /// 
        /// </summary>
        /// <param name="eventData"/>
        public virtual void OnCancel(BaseEventData eventData)
        {
            this.Execute(EventTriggerType.Cancel, eventData);
        }

        /// <summary>
        /// 
        /// <para>
        /// UnityEvent class for Triggers.
        /// </para>
        /// 
        /// </summary>
        [Serializable]
        public class TriggerEvent : UnityEvent<BaseEventData>
        {
        }

        /// <summary>
        /// 
        /// <para>
        /// An Entry in the EventSystem delegates list.
        /// </para>
        /// 
        /// </summary>
        [Serializable]
        public class Entry
        {
            /// <summary>
            /// 
            /// <para>
            /// What type of event is the associated callback listening for.
            /// </para>
            /// 
            /// </summary>
            public EventTriggerType eventID = EventTriggerType.PointerClick;
            /// <summary>
            /// 
            /// <para>
            /// The desired [[UnityEvent]] to be Invoked.
            /// </para>
            /// 
            /// </summary>
            public MyEventTrigger.TriggerEvent callback = new MyEventTrigger.TriggerEvent();
        }
    }
}