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

How to Snap the Element in Scroll Bar.

Discussion in 'Scripting' started by Alexander21, Aug 10, 2016.

  1. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    300
    Hi All

    I am new bie to unity. In my project i have a scroll bar and scroll bar contains five buttons.

    Button1, Button2, Button3, Button4, Button5

    I have displayed. No problem in that. In my project i have to snapping the elements.

    if i pressed the Button2. It should come to position to Button1. If i click the button it should go the previous element position .

    How can i do it...
     
  2. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    300
    Hi All I have used the below code for snapping the elements

    MyScrollRect.horizontalNormalizedPosition = Mathf.Lerp(MyScrollRect.horizontalNormalizedPosition, 10, 20*MyScrollRect.elasticity*Time.deltaTime);

    It is moving. But after that i have found in difficulty in scrolling........ I have found tough in scrolling. Not smoothness.

    is other any way to
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148


    (also there is a sub forum specifically for canvas UI scripting http://forum.unity3d.com/forums/unity-ui.60/, you'll find details around the UI extension project in there which has quite a few tweaks to scrollsrects in particular)
     
    DonLoquacious likes this.
  4. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    300
    Thanks Lefty Righty for your code...Actually is there is any way to do the work simply . That means In four content.

    if i touch the content. it should go for the previous content position..

    for eg . A B C D

    If i touch B . the B Should go to the position of A

    if i touch C the C should go the position of B.

    is there any simple code available . using any rect transform like that. I dont know. could any one help me.
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    oh ok, so the scrollrect is superfluous to the design your talking about? you just want to reorder the buttons within their containing element (that happens to be a scrollrect?).

    immediately in one frame or in an animated slide/drag?

    are you using a layout component to order the buttons? if so just setting the sibling index of the buttons would immediately snap them to their new order.


    If you want to interactively drag elements around within the container and have the rest of the elements slide around / reorder in an animated fashion to accommodate that (like reordering apps on a phone etc.)... well that is in no way "simple".
     
  6. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    300
    Thanks Lefty Right for your help. Actually i am using Layout Component to Order the Buttons. I have instantiate the five buttons. In that i have to snapping the element to the previous one. When i click the button it should go to the previous button position,.
    I am not doing animated fashion. Just simple thing........

    Could any one can give the example for that?
     
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  8. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    300
    Thanks Lefty Right for your Code. i have used the code. But the Items are not moving to the previous position

    if(buttonNo==4)
    Grid3.transform.SetSiblingIndex(0);

    if(buttonNo==3)
    Grid3.transform.SetSiblingIndex(1);

    The items are not moving to the previous position. I dont know why . could any one help me.
     
    Last edited: Aug 31, 2016
  9. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    that snippet looks a little odd... but perhaps something like

    Code (csharp):
    1.  
    2. // assumption: transform is the button being clicked
    3. transform.SetSiblingIndex(Mathf.Clamp(transform.GetSiblingIndex()-1, 0, transform.parent.childCount()-1);
    4.  
     
  10. Alexander21

    Alexander21

    Joined:
    Dec 14, 2015
    Posts:
    300
    Thanks Lefty Righty for your Code. I have tried your code. But it is not worked.... Any other ideas welcome.........
     
  11. Bushido_Interactive

    Bushido_Interactive

    Joined:
    Mar 9, 2017
    Posts:
    4