Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Nested Scrollrect?

Discussion in 'UGUI & TextMesh Pro' started by ssawyer, Sep 15, 2014.

  1. alti

    alti

    Joined:
    Jan 8, 2014
    Posts:
    94
    Here's an easy to understand script:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3. using UnityEngine.UI;
    4.  
    5. public class ScrollRectOffVertical : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler{
    6.  
    7.     public ScrollRect sr, parentSR;
    8.     public bool isVerticalDrag;
    9.  
    10.     private void Start(){
    11.         parentSR = transform.parent.parent.parent.GetComponent<ScrollRect>();
    12.         sr = GetComponent<ScrollRect>();
    13.     }
    14.  
    15.     public void OnBeginDrag(PointerEventData eventData){
    16.         Vector2 dragDelta = eventData.delta;
    17.         isVerticalDrag = Mathf.Abs(dragDelta.y) > Mathf.Abs(dragDelta.x);
    18.         if (isVerticalDrag){
    19.             sr.enabled = false;
    20.             parentSR.OnBeginDrag(eventData);
    21.         }
    22.     }
    23.  
    24.     public void OnDrag(PointerEventData eventData){
    25.         parentSR.OnDrag(eventData);
    26.     }
    27.  
    28.     public void OnEndDrag(PointerEventData eventData){
    29.         if(isVerticalDrag){
    30.             parentSR.OnEndDrag(eventData);
    31.         }
    32.         isVerticalDrag = false;
    33.         sr.enabled = true;
    34.     }
    35. }
    36.  
    Just drop it on each of your nested (child) ScrollRects.

    Btw, the scrollrect vars are public but can be private. Either remove the hardcoded way I found my scroll rects and drag and drop your own references, or alter the code in Start. If your scroll rects are structured normally it should work.
     
  2. Mr_yilanci

    Mr_yilanci

    Joined:
    Nov 11, 2019
    Posts:
    7
    @CaptainSchnittchen

    Captain, you saved my time... you saved our time


    If a statue were to be made for you, concrete wouldn't be enough for your testicles.
     
  3. vadrian0

    vadrian0

    Joined:
    Sep 6, 2017
    Posts:
    1
    It's fantastic!
    Thank you very much!
     
  4. BVGuled

    BVGuled

    Joined:
    Nov 25, 2017
    Posts:
    5
    Hurrayyyy thank you sooooo much that solved many peoples headache, thank you once again @CaptainSchnittchen