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

UNITY UI Scroll through items one by one

Discussion in 'UGUI & TextMesh Pro' started by Danirey, Jan 30, 2015.

  1. shibi2017

    shibi2017

    Joined:
    Jan 18, 2018
    Posts:
    153
    7 years later we still need this.
     
  2. rocbot01

    rocbot01

    Joined:
    Sep 2, 2021
    Posts:
    1
    This work for vetical snapping: Be caution that you need to adjust the distance and distReposition value for you own project


    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class ScrollRectSnap : MonoBehaviour
    8. {
    9.     public RectTransform panel;
    10.     public Button[] bttn;
    11.     public RectTransform center;
    12.  
    13.     private bool dragging = false;
    14.     private int bttnDistance;
    15.     private int minButtomNum;
    16.     private int bttnLength;
    17.  
    18.     public float[] distance;
    19.  
    20.     public float[] distReposition;
    21.  
    22.     // Cache references to the Button and Text components of each button
    23.     private RectTransform[] buttonRectTransforms;
    24.     private Text[] buttonTexts;
    25.     private Image[] buttonImages;
    26.  
    27.     private void Start()
    28.     {
    29.         bttnLength = bttn.Length;
    30.  
    31.         distance = new float[bttnLength];
    32.         distReposition = new float[bttnLength];
    33.  
    34.         buttonRectTransforms = new RectTransform[bttnLength];
    35.         buttonTexts = new Text[bttnLength];
    36.         buttonImages = new Image[bttnLength];
    37.  
    38.         for (int i = 0; i < bttnLength; i++)
    39.         {
    40.             buttonRectTransforms[i] = bttn[i].GetComponent<RectTransform>();
    41.             buttonTexts[i] = bttn[i].GetComponentInChildren<Text>();
    42.             buttonImages[i] = bttn[i].GetComponent<Image>();
    43.         }
    44.  
    45.         bttnDistance =
    46.             (int)
    47.             Mathf
    48.                 .Abs(buttonRectTransforms[1].anchoredPosition.y -
    49.                 buttonRectTransforms[0].anchoredPosition.y);
    50.     }
    51.  
    52.     private void Update()
    53.     {
    54.         for (int i = 0; i < bttnLength; i++)
    55.         {
    56.             distReposition[i] = buttonRectTransforms[i].position.y - center.GetComponent<RectTransform>().position.y;
    57.             distance[i] = Mathf.Abs(distReposition[i]); //Caution
    58.  
    59.             // Calculate the target alpha value for the button
    60.             float targetAlpha = 0.0f;
    61.             Vector2 targetScale = new Vector2(0.7f, 0.7f);
    62.  
    63.             if (distance[i] <= 50.0f)
    64.             {
    65.                 // If the button is within 300 units of the center, set the target alpha value to 1
    66.                 targetAlpha = 1.0f;
    67.                 targetScale = Vector2.one;
    68.             }
    69.             else if (distance[i] <= 350.0f)
    70.             {
    71.                 // If the button is within 600 units of the center, set the target alpha value to 0.5
    72.                 targetAlpha = 0.4f;
    73.                 targetScale = new Vector2(0.7f, 0.7f);
    74.             }
    75.  
    76.             // Calculate the current alpha value for the button using lerp
    77.             float currentAlpha = Mathf.Lerp(buttonTexts[i].color.a, targetAlpha, Time.deltaTime * 10.0f);
    78.  
    79.             Vector2 currentScale = Vector2.Lerp(buttonRectTransforms[i].localScale, targetScale, Time.deltaTime * 10.0f);
    80.  
    81.             // Set the alpha value of the button's text and image
    82.             Color textColor = buttonTexts[i].color;
    83.             textColor.a = currentAlpha;
    84.             buttonTexts[i].color = textColor;
    85.  
    86.             Color imageColor = buttonImages[i].color;
    87.             imageColor.a = currentAlpha;
    88.             buttonImages[i].color = imageColor;
    89.  
    90.             buttonRectTransforms[i].localScale = currentScale;
    91.  
    92.             // Check if the button is out of view and reposition it if necessary
    93.             if (distReposition[i] > 700)
    94.             {
    95.                 // Move the button to the bottom of the panel
    96.                 buttonRectTransforms[i].anchoredPosition += new Vector2(0, -bttnLength * bttnDistance);
    97.             }
    98.             else if (distReposition[i] < -700)
    99.             {
    100.                 // Move the button to the top of the panel
    101.                 buttonRectTransforms[i].anchoredPosition += new Vector2(0, bttnLength * bttnDistance);
    102.             }
    103.         }
    104.  
    105.  
    106.         float minDistance = Mathf.Min(distance);
    107.  
    108.         for (int a = 0; a < bttn.Length; a++)
    109.         {
    110.             if(minDistance == distance[a])
    111.             {
    112.                 minButtomNum = a;
    113.             }
    114.         }
    115.  
    116.         if(!dragging)
    117.         {
    118.             //LerpToBttn(minButtomNum * bttnDistance); //negative or positive value
    119.             LerpToBttn(-bttn[minButtomNum].GetComponent<RectTransform>().anchoredPosition.y);
    120.         }
    121.     }
    122.  
    123.     void LerpToBttn(float position)
    124.     {
    125.         float newY = Mathf.Lerp(panel.anchoredPosition.y, position, Time.deltaTime * 15f);
    126.         Vector2 newPosition = new Vector2(panel.anchoredPosition.x, newY);
    127.  
    128.         panel.anchoredPosition = newPosition;
    129.     }
    130.  
    131.     public void StartDrag()
    132.     {
    133.         dragging = true;
    134.     }
    135.  
    136.     public void EndDrag()
    137.     {
    138.         dragging = false;
    139.     }
    140. }
    141.