Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Socapex · Jun 06, 2015 at 10:15 PM · uitriggersscrollviewevents

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?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Socapex · Jun 07, 2015 at 08:04 PM 0
Share

Or, any way to bypass this? If I implement the PointerEnter in script, will it stop intercepting the scroll events? Or is this a bug? It looks like it, but there is reasonable explanation (mouse raycasts now only affect the foreground element).

avatar image Socapex · Jun 08, 2015 at 06:07 PM 0
Share

So, according to this thread, http://forum.unity3d.com/threads/child-objects-blocking-scrollrect-from-scrolling.311555/#post-2041564, implementing the methods yourself works. BUT, I need to have an EventTrigger component even when I do that, or else when-ever I recieve a PointerDown trigger, I immediately get a PointerUp (if I don't add a component).

Please help, this is making my game unplayable :'(

avatar image Socapex · Jul 23, 2015 at 08:59 PM 0
Share

Just a FYI, implementing the methods still blocks scroll-wheel events, thus making scroll rects unusable...

avatar image Fattie · Jan 19, 2016 at 07:39 PM 0
Share

here's an amazing related solution http://forum.unity3d.com/threads/nested-scrollrect.268551/

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Wiki

Answer by starikcetin · Jun 08, 2015 at 06:15 PM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Socapex · Jun 21, 2015 at 12:58 AM 1
Share

I'm not sure I understand what you mean. As a concrete example, filling a scrollrect with objects that have OnPointerEnter events disables scrolling, because the foreground object captures EVERYTHING. Now I know this is super advanced stuff, putting objects with a hover image in a scrolling window, but someone at Unity must have thought of this!? Sorry for sarcasm couldn't resist XD

All jokes aside, are you suggesting I add a scroll event listener to the foreground objects, without any callback?

avatar image
1

Answer by WalterBoyd · Jan 19, 2016 at 07:39 PM

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();
         }
     }
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NesCroft · Feb 07, 2019 at 11:56 PM 0
Share

your code comments are painful on my eyes

avatar image
1

Answer by jonaslindberg · May 06, 2020 at 07:25 PM

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to vertical drag button inside horizontal scrollrect 2 Answers

Changing ScrollView content from the script 0 Answers

My text is invisible if I put it under the Content object under a Scroll View 1 Answer

Call AddListener on sibling components from MonoBehaviour.Reset 0 Answers

Slider inside table view cell,Slider inside a TSTableViewCell 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges