Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 _Xertz_ · Oct 21, 2018 at 05:15 AM · uiraycastbutton trigger eventsscrolling

How do I have clickable buttons in a scroll rect?

I found a similar question that addresses the same problem but I couldn't quite understand what the dude did to solve his one.

Basically, I have a scroll rect with buttons inside. I tried scrolling, however, when my mouse is over a button the thing doesn't scroll. When it is between the buttons, (not over anything), it scrolls perfectly. I've added a viewport panel above so that I can scroll anywhere but then that makes it so that the buttons don't work.

I've tried a few things but I can't figure it out. Can anyone help?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
4

Answer by Johandea · Oct 21, 2018 at 02:38 PM

There might be a better way of doing this, but I've solved a similar problem for myself with the eventSystem.

On your button: add a script implementing the IBeginDragHandler, IDragHandler, IEndDragHandler.

Within IBeginDragHandler, IDragHandler and IEndDragHandler pass on the event to your scrollRect with

 ExecuteEvents.Execute(scrollRect.gameObject, pointerEventData, ExecuteEvents.beginDragHandler);
 ExecuteEvents.Execute(scrollRect.gameObject, pointerEventData, ExecuteEvents.dragHandler);
 ExecuteEvents.Execute(scrollRect.gameObject, pointerEventData, ExecuteEvents.endDragHandler);

respectively. This makes your button send the same event (i.e. you clicking and dragging it) the button received to the scrollrect, making it scroll.

If you need to you can add some logic before initializing the first ExecuteEvents. In my case I made it so you have to drag it past a certain threshold before passing it on.

Below is a draft for how it could look. Untested as is

 using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 public class yourScript : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
 {
     public ScrollRect yourScrollRect;
 
     private bool passingEvent = false;
 
     public void OnBeginDrag(PointerEventData pointerEventData)
     {
         // If you only need to pass the drag through use
         ExecuteEvents.Execute(yourScrollRect.gameObject, pointerEventData, ExecuteEvents.beginDragHandler);
         passingEvent = true;
     }
 
     public void OnDrag(PointerEventData pointerEventData)
     {
         if (passingEvent) // Don't send dragHandler before beginDragHandler has been called. It gives unwanted results...
         {
             ExecuteEvents.Execute(yourScrollRect.gameObject, pointerEventData, ExecuteEvents.dragHandler);
         }
     }
 
     public void OnEndDrag(PointerEventData pointerEventData)
     {
         ExecuteEvents.Execute(yourScrollRect.gameObject, pointerEventData, ExecuteEvents.endDragHandler);
         passingEvent = false;
     }
 }

And if you need logic to check whether the event should be passed on use something like this:

 using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 public class yourScript : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
 {
     public ScrollRect yourScrollRect;
 
     private Vector3 mousePosOnDragStart;
     private bool passingEvent = false;
 
     public void OnBeginDrag(PointerEventData pointerEventData)
     {
         mousePosOnDragStart = Input.mousePosition;
         // Or something else you need to do at the start of the drag.
     }
 
     public void OnDrag(PointerEventData pointerEventData)
     {
         if ((Input.mousePosition - mousePosOnDragStart).sqrMagnitude > 1) // Checks if mouse has moved further than 1. Use your on logic here
         {
             ExecuteEvents.Execute(yourScrollRect.gameObject, pointerEventData, ExecuteEvents.beginDragHandler);
             passingEvent = true;
         }
 
         if (passingEvent) // Don't send dragHandler before beginDragHandler has been called. It gives unwanted results...
         {
             ExecuteEvents.Execute(yourScrollRect.gameObject, pointerEventData, ExecuteEvents.dragHandler);
         }
     }
 
     public void OnEndDrag(PointerEventData pointerEventData)
     {
         ExecuteEvents.Execute(yourScrollRect.gameObject, pointerEventData, ExecuteEvents.endDragHandler);
         passingEvent = false;
     }
 }




Comment
Add comment · Show 2 · 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 petzichila · Sep 18, 2020 at 05:33 PM 0
Share

Thanks, your answer solved my problem!

In my case, I had to use IScrollHandler instead of the ones you suggested, because I'm using a mouse wheel.

avatar image radiantboy · Apr 08 at 01:58 AM 0
Share

excellent thank you. I used OnScroll, and ended up with this which works a treat

 public ScrollRect myVerticalScrollRect;
     public void OnScroll(PointerEventData eventData)
     {
         _("OnScroll - passing on scroll message so buttons dont steal it");
         //in order to have mousewheel scrolling and clickable buttons we need to intercept the scroll and pass it or it wont work!
         //this is because buttons steal the focus and stop the scrolling.
         ExecuteEvents.Execute(myVerticalScrollRect.gameObject, eventData, ExecuteEvents.scrollHandler);
     }

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

190 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 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 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 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 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 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 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 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 make a crosshair move with the player 1 Answer

physics.raycast troubles with onClick UI Events 2 Answers

UI Text showing weird string value? 1 Answer

How to make text that scrolls horizontally? 2 Answers

Raycast from 2D canvas object to world space 1 Answer


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