Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Nested Scrollrect?

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

  1. antpaw

    antpaw

    Joined:
    Oct 25, 2016
    Posts:
    8
  2. RoguePrimitive

    RoguePrimitive

    Joined:
    Sep 11, 2017
    Posts:
    3
    I got worried that can I implement this script into my project... HAHA! :) Easy and super-good! Thank you @CaptainSchnittchen
     
  3. A_Eniwent_S

    A_Eniwent_S

    Joined:
    Oct 23, 2018
    Posts:
    4
    Hi there.

    Please, help me.
    If I have something like this, where should I put this script and how can I configure it?

    Placing the script on page 2 or its content does not work.



     

    Attached Files:

  4. sandolkakos

    sandolkakos

    Joined:
    Jun 3, 2009
    Posts:
    189
    Are you using the script from @CaptainSchnittchen ?
     
  5. A_Eniwent_S

    A_Eniwent_S

    Joined:
    Oct 23, 2018
    Posts:
    4
    Hi. Yes, script from CaptainSchnittchen.
     
  6. sandolkakos

    sandolkakos

    Joined:
    Jun 3, 2009
    Posts:
    189
    You need only create new ScrollViews as children of your Default ScrollView, but on the children, you need to use the ScrollRectEx script (instead of the default ScrollRect script) because it will route the drag events to parent.

    In this example, the Parent has only Vertical movement while every child has only Horizontal movement:

    ScrollRectEx.PNG
     
    A_Eniwent_S likes this.
  7. A_Eniwent_S

    A_Eniwent_S

    Joined:
    Oct 23, 2018
    Posts:
    4
    Thank you very much! Everything worked out!
     
    sandolkakos likes this.
  8. sandolkakos

    sandolkakos

    Joined:
    Jun 3, 2009
    Posts:
    189
    You're welcome ;)
     
  9. cp-

    cp-

    Joined:
    Mar 29, 2018
    Posts:
    15
    Here's a version not creating all those anonymous functions but using the ExecuteEvents API provided by Unity.

    To me it seems to be the more "correct" way of implementing events propagating upwards. Also it may be more GC-friendly.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using System;
    5. using UnityEngine.EventSystems;
    6.  
    7. public class ScrollRectEx : ScrollRect
    8. {
    9.     private bool routeToParent = false;
    10.  
    11.     /// <summary>
    12.     /// Always route initialize potential drag event to parents
    13.     /// </summary>
    14.     public override void OnInitializePotentialDrag(PointerEventData eventData)
    15.     {
    16.         ExecuteEvents.ExecuteHierarchy(transform.parent.gameObject, eventData, ExecuteEvents.initializePotentialDrag);
    17.         base.OnInitializePotentialDrag(eventData);
    18.     }
    19.  
    20.     /// <summary>
    21.     /// Drag event
    22.     /// </summary>
    23.     public override void OnDrag(UnityEngine.EventSystems.PointerEventData eventData)
    24.     {
    25.         if (routeToParent)
    26.             ExecuteEvents.ExecuteHierarchy(transform.parent.gameObject, eventData, ExecuteEvents.dragHandler);
    27.         else
    28.             base.OnDrag(eventData);
    29.     }
    30.  
    31.     /// <summary>
    32.     /// Begin drag event
    33.     /// </summary>
    34.     public override void OnBeginDrag(UnityEngine.EventSystems.PointerEventData eventData)
    35.     {
    36.         if (!horizontal && Math.Abs(eventData.delta.x) > Math.Abs(eventData.delta.y))
    37.             routeToParent = true;
    38.         else if (!vertical && Math.Abs(eventData.delta.x) < Math.Abs(eventData.delta.y))
    39.             routeToParent = true;
    40.         else
    41.             routeToParent = false;
    42.  
    43.         if (routeToParent)
    44.             ExecuteEvents.ExecuteHierarchy(transform.parent.gameObject, eventData, ExecuteEvents.beginDragHandler);
    45.         else
    46.             base.OnBeginDrag(eventData);
    47.     }
    48.  
    49.     /// <summary>
    50.     /// End drag event
    51.     /// </summary>
    52.     public override void OnEndDrag(UnityEngine.EventSystems.PointerEventData eventData)
    53.     {
    54.         if (routeToParent)
    55.             ExecuteEvents.ExecuteHierarchy(transform.parent.gameObject, eventData, ExecuteEvents.endDragHandler);
    56.         else
    57.             base.OnEndDrag(eventData);
    58.         routeToParent = false;
    59.     }
    60. }
     
  10. zulqarnain26

    zulqarnain26

    Joined:
    Feb 12, 2018
    Posts:
    7
  11. unity_1cewWbGw8rRVxw

    unity_1cewWbGw8rRVxw

    Joined:
    Jun 6, 2018
    Posts:
    5
    Thank You @CaptainSchnittchen you are a genius.
    I don't know how to thank you.
    Phenomenal work.
    Can not Thank you enough.
     
  12. zulqarnain26

    zulqarnain26

    Joined:
    Feb 12, 2018
    Posts:
    7
  13. Elledan3101

    Elledan3101

    Joined:
    May 24, 2013
    Posts:
    2
    your code still awesome. Even in 2k19! Thanks!
     
  14. flatsplay

    flatsplay

    Joined:
    Feb 6, 2019
    Posts:
    2
  15. Colin_MacLeod

    Colin_MacLeod

    Joined:
    Feb 11, 2014
    Posts:
    168
  16. atulyextel_26

    atulyextel_26

    Joined:
    Feb 18, 2019
    Posts:
    1
    Can you tell me how to do that plz
     
  17. sandolkakos

    sandolkakos

    Joined:
    Jun 3, 2009
    Posts:
    189
  18. LAKSHAYMAVIA

    LAKSHAYMAVIA

    Joined:
    Aug 28, 2018
    Posts:
    26
    First of all, thank you for creating this post. With the script of @cp- which is inspired by @CaptainSchnittchen. I manage to create nested scroll rect. I am attaching the Image and video so that others can achieve something like this if they want to.
    Asset use:- simple scroll (Free from asset Store) + Above scroll Rect Ex Script + Dotween(Free from asset Store).

    https://imgur.com/a/09VYwBd

     
  19. Monofab

    Monofab

    Joined:
    Mar 10, 2015
    Posts:
    5
    Thank you really, you saved my life :)
     
  20. Erikoinen

    Erikoinen

    Joined:
    Nov 10, 2014
    Posts:
    67
    @KGC Thanks a ton!
     
  21. Firestorm200

    Firestorm200

    Joined:
    Sep 29, 2013
    Posts:
    2
  22. DerLasseHenrich

    DerLasseHenrich

    Joined:
    Aug 31, 2017
    Posts:
    3
  23. nhatkhiem

    nhatkhiem

    Joined:
    Nov 7, 2014
    Posts:
    1
    @CaptainSchnittchen even 2020 is coming, that's nice solution. Thankyou very much and Merry Christmas!
     
  24. Kimuradfv

    Kimuradfv

    Joined:
    Apr 30, 2019
    Posts:
    1
    Thank you so much! this solved the problem instantly!
     
  25. twice7713

    twice7713

    Joined:
    Apr 24, 2018
    Posts:
    21
    Thank you very much, @CaptainSchnittchen
     
  26. mitaywalle

    mitaywalle

    Joined:
    Jul 1, 2013
    Posts:
    121
  27. Polo07

    Polo07

    Joined:
    Sep 10, 2018
    Posts:
    6
    Five years on... still awesome! Copied the code and it worked like a charm. You rock @CaptainSchnittchen.

    Although I had an even more complicated sructure, where I had scrollrect->scrollrect->scrollrect so had to intercept your while loop and break it where I wanted. But this was sleek!!
     
    SimonDarksideJ likes this.
  28. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,649
    FYI, ScrollRectEx and also the ScrollRectConflictManager have been part of the awesome Unity UI Extensions project (link in sig) for a long time. We recently had another big release for 2019 that's work checking out.

    Although seeing some other interesting proposed updates, so I'm going to have to review and see what if anything can be improved.
     
  29. Jumeuan

    Jumeuan

    Joined:
    Mar 14, 2017
    Posts:
    23
    This is working like a charm, try it!
     
unityunity