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 delta sniper · Oct 24, 2014 at 01:47 AM · 4.6scroller

4.6 Two Scroll Rects stacked one need to go horizontal one needs to go vertical.

So, I have two scroll rects one essentially takes up my entire screen and can only scroll content horizontally. The other one is a list of items that scrolls vertically. They both work fine as long as i am not in spot that raycasts onto both of them.

The problem is if i am in the vertical scroll rect and try to move horizontally it just slightly moves my vertical one (cause its damn near impossible to not move a tad bit vertically). what i want is no matter what if i scroll horizontally first then i want that scroll rect to take over otherwise if i move vertically first then that one should be the one that moves.

An approach i am thinking about doing is simply inhering the IBeginDragHandler, IDragHandler and attemp to finagle something together in the hopes i can get it to work

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
6
Best Answer

Answer by delta sniper · Oct 24, 2014 at 03:32 AM

So, I have solved my own problem. I did use the Interface method above but the implementation was less then 100 lines so i don't think its a horrible solution but here it is in case others are in need.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 using System;
 
 [RequireComponent(typeof(ScrollRect))]
 public class SecondScrollRect : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler
 {
     public ScrollRect OtherScrollRect;
     private ScrollRect _myScrollRect;
     //This tracks if the other one should be scrolling instead of the current one.
     private bool scrollOther;
     //This tracks wether the other one should scroll horizontally or vertically.
     private bool scrollOtherHorizontally;
 
     void Awake()
     {
         //Get the current scroll rect so we can disable it if the other one is scrolling
         _myScrollRect = this.GetComponent<ScrollRect>();
         //If the current scroll Rect has the vertical checked then the other one will be scrolling horizontally.
         scrollOtherHorizontally = _myScrollRect.vertical;
         //Check some attributes to let the user know if this wont work as expected
         if (scrollOtherHorizontally)
         {
               if(_myScrollRect.horizontal)
                   Debug.Log("You have added the SecondScrollRect to a scroll view that already has both directions selected");
               if (!OtherScrollRect.horizontal)
                   Debug.Log("The other scroll rect doesnt support scrolling horizontally");
         }
         else if (!OtherScrollRect.vertical)
         {
             Debug.Log("The other scroll rect doesnt support scrolling vertically");
         }
     }
     //IBeginDragHandler
     public void OnBeginDrag(PointerEventData eventData)
     {
         //Get the absolute values of the x and y differences so we can see which one is bigger and scroll the other scroll rect accordingly
         float horizontal = Mathf.Abs(eventData.position.x - eventData.pressPosition.x);
         float vertical = Mathf.Abs(eventData.position.y - eventData.pressPosition.y);
         if (scrollOtherHorizontally)
         {
             if (horizontal > vertical)
             {
                 scrollOther = true;
                 //disable the current scroll rect so it doesnt move.
                 _myScrollRect.enabled = false;
                 OtherScrollRect.OnBeginDrag(eventData);
             }
         }
         else if (vertical > horizontal)
         {
             scrollOther = true;
             //disable the current scroll rect so it doesnt move.
             _myScrollRect.enabled = false;
             OtherScrollRect.OnBeginDrag(eventData);
         }
     }
     //IEndDragHandler
     public void OnEndDrag(PointerEventData eventData)
     {
         if (scrollOther)
         {
             scrollOther = false;
             _myScrollRect.enabled = true;
             OtherScrollRect.OnEndDrag(eventData);
         }
     }
     //IDragHandler
     public void OnDrag(PointerEventData eventData)
     {
         if (scrollOther)
         {
             OtherScrollRect.OnDrag(eventData);
         }
     }
 }
 
Comment
Add comment · Show 9 · 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 dhollin · Oct 09, 2015 at 09:11 AM 0
Share

Hey @delta sniper, just wanted to say thank you for this script, it works perfectly!

avatar image srinivassunil · Feb 27, 2016 at 03:11 PM 0
Share

Perfect solution works like charm!

avatar image Gwom · Dec 14, 2016 at 12:57 PM 0
Share

Great solution, thanks for posting, combined with the UnityEngine.UI.Extensions HorizontalScrollSnap and works like a charm!

avatar image tannaz-rashidi · Aug 23, 2017 at 05:03 AM 0
Share

@delta sniper ohhh my goddd, thanks a lot,U save my life,i spent 3days on this problem and now U make me happy, i'm free now :D Thanks again ;-)

avatar image Darkmoon_Indie · Aug 23, 2018 at 04:51 PM 0
Share

Hi DeltaSniper, thank you very much for the script. It really works , you just saved lot of my time.

Show more comments

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

12 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

Related Questions

2D Autoscroller Glitches 0 Answers

IPointerClickHandler error 2 Answers

How to change Normal color, Highlighted color etc. in 4.6 buttons with code 2 Answers

Who i can rotate new UI Image element? 2 Answers

Unity 4.6 Outlining an object GUI 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