• 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 /
  • Help Room /
avatar image
Question by marucu · Feb 17, 2015 at 10:47 AM · uieventeventslayoutscroll

[Solved] Scroll not working when elements inside have click events

I'm using the new Unity UI for a mobile game and in the menu I have a Scroll Rect with a Vertical Layout Group inside, with Text elements inside the group. Like t$$anonymous$$s:

alt text

The t$$anonymous$$ng is that the scroll works fine until I set pointer events to the text elements (Pointer Up, Pointer Click, etc), then when I swipe above an element I t$$anonymous$$nk the UI is "paying attention" to the event manager i.e. checking if I release my finger to trigger Pointer Up event and therefore it does not scroll. But it still scrolls if I click outside a Text element but inside the Vertical Layout Group.

So I don't know if there's a way to make the group scroll even if the elements have t$$anonymous$$s events.

captura-de-pantalla-2015-02-16-a-las-210150.png (10.3 kB)
Comment
Daoqi
ja_twoj_dom_trupy_szatan
omrip32
IgorAherne
bilalakil
TIMO4KA
Vagonn
jkotzian
roointan
dingqun2020

People who like this

10 Show 0
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

4 Replies

· Add your reply
  • Sort: 
avatar image

Answer by marucu · Feb 22, 2015 at 02:00 PM

I found what was happening! The point is that if you use Event Trigger it will eat all other inputs because it implements all the EventSystem interfaces (and the OnDrag event used by the scroll rect was eclipsed).

  namespace UnityEngine.EventSystems
  {
      [AddComponentMenu("Event/Event Trigger")]
      public class EventTrigger : MonoBehaviour, IEventSystemHandler, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler, IPointerClickHandler, IDragHandler, IDropHandler, IScrollHandler, IUpdateSelectedHandler, ISelectHandler, IDeselectHandler, IMoveHandler
      {
          [Serializable]
          public class TriggerEvent : UnityEvent<BaseEventData>
          {
          }
  ... blah, blah, blah
 }
 }

So, the solution, avoid using Event Trigger, instead, implement the interface you need for the purpose. My example, instead of adding OnPointerDown and OnPointerUp using the Event Trigger I added t$$anonymous$$s script to the object:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;
 
 public class Click : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
 
     public void OnPointerDown (PointerEventData eventData) {
         // Do action
     }
 
     public void OnPointerUp (PointerEventData eventData) {
         // Do action
     }
 }
 


Comment
Daoqi
ja_twoj_dom_trupy_szatan
Gizmmoo
KrlinM
drlecks
xxluky
dappledore
yapaysinek
flashmandv
canis
kn-narola
omrip32
Schnodahipfe
IgorAherne
abittman
And 40 more...

People who like this

55 Show 10 · 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 qiaosic · Dec 10, 2015 at 02:51 AM 0
Share

This totally solves my problem!

avatar image Gizmmoo · Jan 26, 2016 at 04:10 PM 0
Share

You the man!!

avatar image KrlinM · Feb 08, 2016 at 01:27 AM 0
Share

Amazing, thanks!

avatar image canis · Jun 21, 2016 at 06:42 AM 0
Share

Thanks @marucu, you save my day, I working on the nest scrollview passer, and your answer help me to understand a lot. https://www.youtube.com/watch?v=0CgG7WIkV0s

full setup article : http://www.clonefactor.com/wordpress/program/unity3d/1519/

avatar image Baktillus · Aug 22, 2016 at 02:56 PM 0
Share

I know this is old as hell, but can anyone tell me if its possible to assign the triggered actions in the inspector, just like in the Unity EventTrigger? Does it use a special editor script, or is there a way to implement this easily?

avatar image petesplace Baktillus · Nov 01, 2016 at 09:59 PM 2
Share

Try something like this

 public class Click : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
 {
     public UnityEvent PointerDown;
     public UnityEvent PointerUp;
 
     public void OnPointerDown(PointerEventData eventData)
     {
         if (PointerDown != null)
             PointerDown.Invoke();
     }
 
     public void OnPointerUp(PointerEventData eventData)
     {
         if (PointerUp != null)
             PointerUp.Invoke();
     }
 }
Show more comments
avatar image

Answer by tomihr2 · Dec 24, 2016 at 11:54 PM

When I try t$$anonymous$$s implementation:

 public class Click : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {

  public void OnPointerDown (PointerEventData eventData) {
      // Do action
  }
 
  public void OnPointerUp (PointerEventData eventData) {
      // Do action
  }

}

On mobile device OnPointerUp event is triggered almost same time as OnPointerDown, so can't do anyt$$anonymous$$ng with t$$anonymous$$s implementation, am I doing somet$$anonymous$$ng wrong?

Comment
sotrosh
TIMO4KA

People who like this

2 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 sotrosh · Mar 18, 2019 at 10:13 PM 0
Share

I have the same issue. Does anyone know how to fix it?

avatar image

Answer by viciousesque · Aug 20, 2016 at 12:29 AM

A simpler solution is to just use a button. I deleted the EventTrigger components and replaced them with simple Button components and t$$anonymous$$s allowed the Image component to remain clickable and propagated the scroll event up to the ScrollRect game object, so the scrolling action now works when users scroll over the image.

Comment

People who like this

0 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 Baktillus · Aug 22, 2016 at 09:50 AM 0
Share

Just a heads up: If you do this, you may run into problems with certain mobile devices. For example my UI is working perfectly in Editor and on a OnePlus Two, but a Samsung Galaxy S6 has huge issues recognising button clicks. For now it seems like @marucu 's solution works best.

avatar image

Answer by kknich · Jul 14, 2022 at 01:43 AM

If Marucu's solution didn't work out for you or you simply don't like the idea of getting rid of your Trigger Events, FEAR NOT MY BROTHER (or sister/wtv u want), I HAVE A SOLUTION!

In a nutshell, my solution was simply to detect if the user is hovering over said element using a boolean. Afterwards, I would simply apply any scroll inputs directly to the RectTransform of the whole scrollable container. You can multiply t$$anonymous$$s input by whatever you want.

I know it sounds dumb, but t$$anonymous$$nk about it, t$$anonymous$$nk about it deeply. Is it really?

Anyhow, here's a simple example script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class temp : MonoBehaviour
 {
     public RectTransform yourScrollboxContainer;
     public bool isHoveringOverUI = false;
     public int scrollSensitivity = 200;
 
     void Update()
     {
         if(isHoveringOverUI && Input.GetAxisRaw("Mouse ScrollWheel") != 0){
             yourScrollboxContainer.anchoredPosition = new Vector2(0, yourScrollboxContainer.anchoredPosition.y - Input.GetAxisRaw("Mouse ScrollWheel") * scrollSensitivity);
         }
     }
 
     //Set your PointerEnter event to t$$anonymous$$s
     public void TriggerEventPointerEnter(){
         isHoveringOverUI = true;
     }
 
     //Set your PointerExit event to t$$anonymous$$s
     public void TriggerEventPointerExit(){
         isHoveringOverUI = false;
     }
 }

I don't t know if t$$anonymous$$s is a little extreme, I honestly don't really care. It gets the job done, and gives you as much control as you want over the t$$anonymous$$ng. You're welcome ;)

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Is there anyway to make an input field stretch to fit a body of text? 0 Answers

Layout groups don't work 1 Answer

Create a Prefab directly inside a GameObject (not SetParent or transform.parent) 0 Answers

Instance variables and this == null in event handler 1 Answer

UI layout suddenly changed, not sure how to get it back 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